Skip to content

Error message provides suggested fix that does not compile #51972

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

Closed
dralley opened this issue Jul 1, 2018 · 3 comments
Closed

Error message provides suggested fix that does not compile #51972

dralley opened this issue Jul 1, 2018 · 3 comments

Comments

@dralley
Copy link
Contributor

dralley commented Jul 1, 2018

When I attempt to compile the code:

fn main() {
    let mut sum = 0i32;
    
    for i in 0..10 {
        sum += i.pow(2);
    }
    
    println!("{}", sum);
}

I get back an error message with a 'help' suggestion:

error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
 --> test.rs:5:18
  |
5 |         sum += i.pow(2);
  |                  ^^^
help: you must specify a type for this binding, like `i32`
  |
4 |     for i: i32 in 0..10 {
  |         ^^^^^^

error: aborting due to previous error

This is a great error message however the suggested code unfortunately does not work. Adding : i32 to the previous code sample as suggested will also fail with the error:

error: missing `in` in `for` loop
 --> test.rs:4:10
  |
4 |     for i: i32 in 0..10 {
  |          ^ help: try adding `in` here

error: expected expression, found `:`
 --> test.rs:4:10
  |
4 |     for i: i32 in 0..10 {
  |          ^ expected expression

error: aborting due to 2 previous errors

What does work, is changing 0..10 to 0i32..10. The rustc-suggested syntax does seem reasonable to me (as a newbie) so it's a little surprising that it's invalid.

rustc version:

rustc 1.27.0 (3eda71b00 2018-06-19)
binary: rustc
commit-hash: 3eda71b00ad48d7bf4eef4c443e7f611fd061418
commit-date: 2018-06-19
host: x86_64-unknown-linux-gnu
release: 1.27.0
LLVM version: 6.0
@dralley dralley changed the title Error message provides a "help" suggestion which does not compile Error message provides suggested fix that does not compile Jul 1, 2018
@zackmdavis
Copy link
Member

Duplicate of #51634; messaging improved in #51670. Nightly output:

error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
 --> 51972.rs:5:18
  |
4 |     for i in 0..10 {
  |         - you must specify a type for this binding, like `i32`
5 |         sum += i.pow(2);
  |                  ^^^

error: aborting due to previous error

(Although this doesn't emit the bogus suggestion, this could arguably still be improved; the actual fix is to specify the type on the range (0_i32..10), not the i.)

@dralley dralley closed this as completed Jul 2, 2018
@dralley
Copy link
Contributor Author

dralley commented Jul 2, 2018

@zackmdavis Thanks. Do you know if there is a reason why, theoretically, rust could not support this syntax eventually?

@zackmdavis
Copy link
Member

@dralley There's an unstable type_ascription feature that makes : Type notation work in a lot more places (although for-loop bindings don't seem to be one of them), but the tracking-issue discussion makes it looks like folks don't want to stabilize it in its current form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants