Skip to content

>= typo gives unclear error message #98128

Open
@macmv

Description

@macmv

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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

    Issue actions