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

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

Closed
hsivonen opened this issue Jan 22, 2019 · 1 comment
Closed

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

hsivonen opened this issue Jan 22, 2019 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@hsivonen
Copy link
Member

hsivonen commented Jan 22, 2019

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.

@Centril Centril added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 22, 2019
@gnzlbg
Copy link
Contributor

gnzlbg commented Jan 22, 2019

In particular, the error message does appear to know that a type is expected, but something else is being provided. Chances are that if an user writes let ident: expr; what they meant is let ident = expr;.

@estebank estebank added 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. labels Oct 18, 2019
Centril added a commit to Centril/rust that referenced this issue Oct 26, 2019
Use heuristics to suggest assignment

When detecting a possible `=` -> `:` typo in a `let` binding, suggest
assigning instead of setting the type.

Partially address rust-lang#57828.
Centril added a commit to Centril/rust that referenced this issue Oct 27, 2019
Use heuristics to suggest assignment

When detecting a possible `=` -> `:` typo in a `let` binding, suggest
assigning instead of setting the type.

Partially address rust-lang#57828.
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 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

4 participants