Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error is not the best with mistyping Struct instead of struct #97793

Closed
passcod opened this issue Jun 6, 2022 · 5 comments · Fixed by #129899
Closed

Error is not the best with mistyping Struct instead of struct #97793

passcod opened this issue Jun 6, 2022 · 5 comments · Fixed by #129899
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@passcod
Copy link
Contributor

passcod commented Jun 6, 2022

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=0c755cbfa505e42fb58dd902968675e5

Struct Foo {
   hello: String,
}

The current output is:

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `Foo`
 --> src/main.rs:2:8
  |
2 | Struct Foo {
  |        ^^^ expected one of 8 possible tokens

Identical on stable and nightly (per playground).

It would be friendler to show some help about the proper casing of struct.

@passcod passcod added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 6, 2022
@TaKO8Ki TaKO8Ki self-assigned this Jun 6, 2022
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jun 6, 2022
@estebank
Copy link
Contributor

estebank commented Jun 6, 2022

@TaKO8Ki while you're at it, we probably should 1) create a kw::* to "concept string" map, and 2) do a levenshtein distance check between current and previous token against that map to see what you might have meant. That would make the report be better for more than just this one case.

@veera-sivarajan
Copy link
Contributor

@estebank I'm interested in implementing a fix for this but I don't know what's a "concept string" in this context.

Could you please explain more about it?

@passcod
Copy link
Contributor Author

passcod commented Jul 28, 2024

I think Esteban meant, expanded:

  1. create a mapping between the types in rustc_span::symbols::kw to static strings of these symbols (e.g. kw::Struct -> "struct") — I imagine this involves adding to the macro that generates this mod in the first place.
  2. when rustc encounters a token error:
    1. check the raw token (here, Struct) against the mapping, with a fuzzy-match algorithm like levenshtein distance
    2. if there's a close enough match (defined against a threshold that would probably need manual tuning to be maximally helpful), suggest that symbol
    3. otherwise do the current thing

That would not only catch Struct -> struct, but also all other miscasings, as well as other typos like strucc or ipml; you'd need to do some tuning so it doesn't suggest move when you type mode and meant mod, or maybe suggest both.

@veera-sivarajan
Copy link
Contributor

Thank you for the detailed explanation.

@rustbot claim

@rustbot rustbot assigned veera-sivarajan and unassigned TaKO8Ki Jul 30, 2024
@veera-sivarajan
Copy link
Contributor

Hi,

I thought of a slightly better but more complicated approach. In case you have any suggestions: https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Tracking.20Expected.20Keywords.20when.20Parsing/near/463517139

@bors bors closed this as completed in d6a4298 Sep 7, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 7, 2024
Rollup merge of rust-lang#129899 - veera-sivarajan:fix-97793-pr-final, r=chenyukang

Add Suggestions for Misspelled Keywords

Fixes rust-lang#97793

This PR detects misspelled keywords using two heuristics:

1. Lowercasing the unexpected identifier.
2. Using edit distance to find a keyword similar to the unexpected identifier.

However, it does not detect each and every misspelled keyword to
minimize false positives and ambiguities. More details about the
implementation can be found in the comments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants