Skip to content

Give a better diagnostic for keywords with incorrect capitalization #77149

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

Open
jyn514 opened this issue Sep 24, 2020 · 10 comments
Open

Give a better diagnostic for keywords with incorrect capitalization #77149

jyn514 opened this issue Sep 24, 2020 · 10 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Sep 24, 2020

Notice the capitalization (playground):

fn f() {
    While let Some(x) = vec![].next() {
        //
    }
}
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found keyword `let`
 --> src/lib.rs:2:11
  |
2 |     While let Some(x) = vec![].next() {
  |           ^^^ expected one of 8 possible tokens

Instead it would be nice to say

error: `While` is not a keyword
 --> src/lib.rs:2:11
  |
2 |     While let Some(x) = vec![].next() {
  |     ^^^^^ help: try `while` instead
@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Sep 24, 2020
@camelid
Copy link
Member

camelid commented Sep 24, 2020

This is not just for while (playground):

fn main() {
    Let x = "hello";
}
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `x`
 --> src/main.rs:2:9
  |
2 |     Let x = "hello";
  |         ^ expected one of 8 possible tokens

Perhaps in general it might be nice to do some kind of Levenshtein distance for identifiers that might be intended to be keywords? Might be tricky to implement though, and probably not necessary.

@jyn514 Do you want to update the title since this would be nice to have for other keywords as well?

@jyn514 jyn514 changed the title Give a better diagnostic for While let Give a better diagnostic for keywords with incorrect capitalization Sep 24, 2020
@pickfire
Copy link
Contributor

pickfire commented Sep 25, 2020

I think there is a lot for this, like Fn main. But I don't know if this is worth it.

@jyn514
Copy link
Member Author

jyn514 commented Sep 25, 2020

If I didn't think it was worth it, I wouldn't have opened the issue.

@camelid camelid added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Sep 25, 2020
@jyn514
Copy link
Member Author

jyn514 commented Sep 25, 2020

@camelid FYI I don't think newcomer-roadblock is wrong per-se, but I opened this because I made the typo myself and was very confused for a second 😆

@camelid
Copy link
Member

camelid commented Sep 26, 2020

I don't know; I would consider it newcomer-roadblock. This is a pretty confusing error:

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `x`
 --> src/main.rs:2:9
  |
2 |     Let x = "hello";
  |         ^ expected one of 8 possible tokens

In general, Rust's parse errors can sometimes be a bit confusing (for me at least). It would just be nice if they were more semantic errors, rather than what token the parser expected and what it got. I think that format is more understandable to compiler developers than to regular programmers ;)

@hosseind75
Copy link
Contributor

guys, I really wanna do a related task to the parser, if you give me a little information that which part of the parser I should work on, I can start working on it

@pickfire
Copy link
Contributor

@hosseind75 I am also a bit now on this but my guess is that this is probably not a very easy task to begin with.

Or maybe check in compiler/rustc_parse/src/parser/expr.rs or somewhere around there? My guess is somewhere around parse_lit but maybe @estebank can give you a more detailed mentor instructions.

@hosseind75
Copy link
Contributor

sure I will check, thanks

@hosseind88
Copy link
Contributor

@estebank sorry can you give me a little instructions about how to fix this issue😅?
I want to start it but I need some details

@fmease
Copy link
Member

fmease commented Feb 29, 2024

Triage: No change.

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 lexing & parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants