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

>= typo gives unclear error message #98128

Open
macmv opened this issue Jun 15, 2022 · 3 comments
Open

>= typo gives unclear error message #98128

macmv opened this issue Jun 15, 2022 · 3 comments
Labels
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.

Comments

@macmv
Copy link

macmv commented Jun 15, 2022

Given the following code:

let is_greater = 5 => 3;

The current output is:

   Compiling playground v0.0.1 (/playground)
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `=>`
 --> src/main.rs:2:24
  |
2 |     let is_greater = 5 => 3;
  |                        ^^ expected one of `.`, `;`, `?`, `else`, or an operator

error: could not compile `playground` due to previous error

playground

This had me very confused for a surprising amount of time. It would be nice if the compiler would give a suggestion to replace => with >=. In my opinion, this would help a lot for someone typing quickly, and would make it clear that the symbols were in reverse. It might also lead to some false positives, but I think it would still be nice to have.

The error is actually worse for macros:

macro_rules! accept_expr {
    ( $expr:expr ) => { $expr }
}

fn main() {
    accept_expr!(5 => 3);
}

The current output for this macro is:

   Compiling playground v0.0.1 (/playground)
error: no rules expected the token `=>`
 --> src/main.rs:6:20
  |
1 | macro_rules! accept_expr {
  | ------------------------ when calling this macro
...
6 |     accept_expr!(5 => 3);
  |                    ^^ no rules expected this token in macro call

error: could not compile `playground` due to previous error

My suggestion it to simply add a help section to both of the above errors, which would suggest replacing the => token with >=.

@macmv macmv 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 15, 2022
@compiler-errors
Copy link
Member

compiler-errors commented Jun 15, 2022

This is complicated, given that => is parsed as its own token as opposed the equivalent typo =< for the lt operator. I'm not confident that fixing this (at least in the macro_rules! case, but possibly in arbitrary positions) is possible, because this is a valid macro:

macro_rules! foo {
    ($a:expr => $b:expr) => {};
}

@ChayimFriedman2
Copy link
Contributor

If this is possible, it would also be nice to mention closures there, since a => b is the closure syntax in many languages.

Dylan-DPC pushed a commit to Dylan-DPC/rust that referenced this issue Jun 19, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 19, 2022
Write help for unexpected `=>`. Fixes rust-lang#98128

rust-lang#98128 discusses what to do with the typo `=>` vs `>=`, or efforts to write a JavaScript-syntax closure in Rust (`(a, b) => (a + b)`). Both manifest as an unexpected `token::FatArrow` so the error message describes both. I think that only those who are very new to programming would actually need to be told how to spell `>=`, but I could understand someone misreading the existing error message and not realising what they'd typed. The opposite of this is:
```
(a, b) =< (a + b);
                 ^ expected one of `>` or `as`
```
which I think would be significantly more complex to detect because `=<` isn't a token, and less beneficial as I'm not aware of a language which actually uses that syntax for something.
@veera-sivarajan
Copy link
Contributor

Output on rustc 1.79.0:

error: expected one of `.`, `;`, `?`, `else`, or an operator, found `=>`
 --> <source>:3:24
  |
3 |     let is_greater = 5 => 3;
  |                        ^^ expected one of `.`, `;`, `?`, `else`, or an operator
  |
help: you might have meant to write a "greater than or equal to" comparison
  |
3 |     let is_greater = 5 >= 3;
  |                        ~~

error: aborting due to 1 previous error

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 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