Skip to content

Error msg is obtuse when doing float_fallback += 1; (or any int literal) #23994

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
mdinger opened this issue Apr 3, 2015 · 9 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@mdinger
Copy link
Contributor

mdinger commented Apr 3, 2015

This may be WONTFIX but I'm reporting it in case it isn't. I'm not sure what the opinion on this is exactly anyway. The idea came from here. This error may come up very easily on rust-lang.org and so is likely very noticeable. This:

fn main() {
    let mut x = 0.0;
    x += 1;
}

Gives this error:

<anon>:3:5: 3:6 error: binary assignment operation `+=` cannot be applied to types `_` and `_` [E0368]
<anon>:3     x += 1;
             ^
error: aborting due to previous error
playpen: application terminated with error code 101

There are three issues, particularly to a new user (first time exploring rust-lang.org):

  • The distinction 0 and 0.0 may seem bizarre
  • Use of the mysterious type _ will be puzzling and makes the error confusing
  • Comparing _ and _ makes it worse.

It would be ideal if 1 could be inferred to the type of x in this case. This would also avoid this confusing error on the rust main landing page.

@daboross
Copy link
Contributor

daboross commented Apr 3, 2015

Just as a note, I think both types are _ because rustc doesn't know exactly what types they actually are. The 1 could be any number type really, from u8 to i64, and the 0.0 could be either f32 or f64. It should have a better way of describing this, yes, but I don't think it's exactly clear what exactly it should be saying instead of _. Since it doesn't know what either of them are, it is surprising that it doesn't assume the 1 to be an f32/f64 as well.

@pnkfelix pnkfelix changed the title Integers should be inferred when possible Float fallback failure for x += 1; Apr 3, 2015
@bluss
Copy link
Member

bluss commented Apr 3, 2015

Wishes for new features should go on the RFC repo, and the issue is already present: rust-lang/rfcs#260

@bluss
Copy link
Member

bluss commented Apr 3, 2015

Note: The well-defined default type for 0.0 is f64 in Rust (in case it is not inferred to a specific floating point type)

@pnkfelix
Copy link
Member

pnkfelix commented Apr 3, 2015

Nonetheless, the error message here is particularly bad ...

@pnkfelix
Copy link
Member

pnkfelix commented Apr 3, 2015

(but I agree with @bluss's assessment, and conclude that my title change is not quite right.)

@pnkfelix pnkfelix changed the title Float fallback failure for x += 1; Error message is particularly obtuse when doing float_fallback += 1; (or any int literal) Apr 3, 2015
@pnkfelix pnkfelix changed the title Error message is particularly obtuse when doing float_fallback += 1; (or any int literal) Error msg is obtuse when doing float_fallback += 1; (or any int literal) Apr 3, 2015
@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 3, 2015
@mdinger
Copy link
Contributor Author

mdinger commented Apr 3, 2015

@bluss It didn't occur to me this would be a feature. I thought it might have just been a bug or old design decision.

What a better error message would look like isn't clear which is why I didn't suggest it though.

@jaredly
Copy link

jaredly commented Aug 11, 2015

Instead of '_' it would be nice to have "integral type" (that language shows up elsewhere) and "float type"

@leoyvens
Copy link
Contributor

1.14 output:

error[E0277]: the trait bound `{float}: std::ops::AddAssign<{integer}>` is not satisfied
 --> <anon>:3:5
  |
3 |     x += 1;
  |     ^^^^^^ the trait `std::ops::AddAssign<{integer}>` is not implemented for `{float}`
  |
  = help: the following implementations were found:
  = help:   <f64 as std::ops::AddAssign>
  = help:   <f32 as std::ops::AddAssign>
  = help:   <u64 as std::ops::AddAssign>
  = help:   <std::time::Duration as std::ops::AddAssign>
  = help: and 24 others

This bug is fixed.

@bluss
Copy link
Member

bluss commented Jan 29, 2017

Thank you. New bugs can be filed if there are problems with the new error message.

@bluss bluss closed this as completed Jan 29, 2017
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
Projects
None yet
Development

No branches or pull requests

7 participants