Skip to content

Unergonomic structured suggestions in rustc #47927

Open
@oli-obk

Description

@oli-obk
Contributor

While checking span_helps that could be span_suggestion I noticed that rustc contains lots of code similar to

                    match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
                        Ok(s) => {
                            err.span_suggestion(self.cast_span,
                                                "try casting to a reference instead",
                                                format!("&{}{}", mtstr, s));
                        }
                        Err(_) => {
                            span_help!(err, self.cast_span, "did you mean `&{}{}`?", mtstr, tstr)
                        }
                    }

where we try to get a snippet and if that fails, since we can't produce a nice suggestion, we produce a help message that contains a message.

We should probably provide a helper for that. First I thought that we could add a helper that does essentially the above without all the duplication, but with the new approximate suggestions (#47540) we can always produce the suggestion, but mark it as approximate if we need to use the fallback value.

I'd assume the above example would look something like this:

err.span_possibly_approximate_suggestion(
    self.cast_span, // span to replace
    "try casting to a reference instead", // message
    fcx.tcx.sess.codemap().span_to_snippet(self.cast_span).ok(), // optional snippet
    tstr, // default if snippet is none
    |snip| format!("&{}{}", mtstr, snip), // closure taking snippet and producing the replacement code
);

cc @Manishearth @nrc

Activity

zackmdavis

zackmdavis commented on Feb 1, 2018

@zackmdavis
Member

where we try to get a snippet [...] and if that fails

(This is probably a really dumb question, but this API had bugged me for a while, so just in case my intuition isn't going haywire here—)

If we can know that spans are valid, is there much reason span_to_snippet should fail? Are malformed codemaps and the like an expected contingency to return Err on, or a rare bug that we should .expect not to happen?

Manishearth

Manishearth commented on Feb 1, 2018

@Manishearth
Member

macros and stuff, usually. not all spans come from a contiguous region of code

added
C-cleanupCategory: PRs that clean code up or issues documenting cleanup.
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Feb 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-cleanupCategory: PRs that clean code up or issues documenting cleanup.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kennytm@oli-obk@zackmdavis@Manishearth

        Issue actions

          Unergonomic structured suggestions in rustc · Issue #47927 · rust-lang/rust