Skip to content

Better error message when : appears where = is supposed to appear #57828

Closed
@hsivonen

Description

@hsivonen

Consider this code:

pub fn foo(num: i32) -> i32 {
    let foo: i32::from_be(num);
}

Godbolt link

The compiler complains like this:

error[E0573]: expected type, found local variable `num`
 --> <source>:2:27
  |
2 |     let foo: i32::from_be(num);
  |                           ^^^ not a type

error: parenthesized parameters may only be used with a trait
 --> <source>:2:26
  |
2 |     let foo: i32::from_be(num);
  |                          ^^^^^
  |
  = note: #[deny(parenthesized_params_in_types_and_modules)] on by default
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>

error[E0223]: ambiguous associated type
 --> <source>:2:14
  |
2 |     let foo: i32::from_be(num);
  |              ^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<i32 as Trait>::from_be`

error[E0308]: mismatched types
 --> <source>:1:29
  |
1 |   pub fn foo(num: i32) -> i32 {
  |  _____________________________^
2 | |     let foo: i32::from_be(num);
3 | | }
  | |_^ expected i32, found ()
  |
  = note: expected type `i32`
             found type `()`

error: aborting due to 4 previous errors
Some errors occurred: E0223, E0308, E0573.
For more information about an error, try `rustc --explain E0223`.

It would be good if the compiler was able to suggest that you may want to replace : with = instead of just making suggestions based on the premise that : was right.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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

    Issue actions