Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

error msg "Add/AddAssign (etc) is not implemented for the type _" poor for beginners #34297

Closed
pnkfelix opened this issue Jun 16, 2016 · 8 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics

Comments

@pnkfelix
Copy link
Member

Reduced version of code encountered during a tutorial:

fn main() {
    let x = 0;
    x += 1.0;
}

Compiler error message:

error: the trait bound `_: std::ops::AddAssign<_>` is not satisfied [--explain E0277]
 --> <anon>:3:5
3 |>     x += 1.0;
  |>     ^^^^^^^^
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:   <i64 as std::ops::AddAssign>
help: and 21 others

error: aborting due to previous error

Namely, the error message says nothing about i32 nor f64 nor the use of let x = 0;, so a beginner really has no place to go for help here.

Can we leverage the on_unimplemented functionality to provide better feedback in the case where one is mixing i32 and f64? (Or does it not have a way to put in a more specific message depending on the types involved in this case?)

There may be an issue for this elsewhere (we all know that our error messaging when type-inference goes to numeric-fallback), but the error message here was particularly bad, and I couldn't find an existing issue quickly, so I am filing this one to make sure this doesn't fall through the cracks.

@pnkfelix pnkfelix changed the title error msg Add/AddAssign (etc) is not implemented for the type _ poor for beginners error msg "Add/AddAssign (etc) is not implemented for the type _" poor for beginners Jun 16, 2016
@pnkfelix pnkfelix added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 16, 2016
@eddyb
Copy link
Member

eddyb commented Jun 17, 2016

I think we can print more than _, in cases where we don't have the expected/found long description.
Applying integer/floating-point defaults before printing errors might be useful, although they could be misleading in some cases. I'm not sure at which point the error is generated here.

integer type: std::ops::AddAssign<floating-point type> is already better, but we can combine this with #[on_unimplemented] to get floating-point type cannot be added to integer type.

We can also sneak in the literal which created the inference variable, but in the current ppaux setup that's a bit problematic (would have to put a mapping in TLS just to print such things, or redo ppaux).

@sanxiyn
Copy link
Member

sanxiyn commented Aug 4, 2016

After #35080, we now print the trait bound {integer}: std::ops::AddAssign<{float}> is not satisfied.

@DiThi
Copy link

DiThi commented Dec 21, 2016

As a beginner I stumbled upon this problem, but with integers, and a more complicated expression. The problem was much more clearly explained (what errors in the expression) when I replaced x += by x = x +

edit: Putting x += back didn't work. Here's the actual code: http://exercism.io/submissions/0186f3e21e974b8f817a9bad500cf2c3

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@estebank
Copy link
Contributor

Current output:

error[E0277]: the trait bound `{integer}: std::ops::AddAssign<{float}>` is not satisfied
 --> src/main.rs:3:7
  |
3 |     x += 1.0;
  |       ^^ no implementation for `{integer} += {float}`
  |
  = help: the trait `std::ops::AddAssign<{float}>` is not implemented for `{integer}`

We could maybe add some suggestions to cast from float to integer or vice versa.

@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Dec 6, 2017
@estebank
Copy link
Contributor

The casting suggestion could be added to *Assign traits' rustc_on_unimplemented attr once #44755 is implemented.

Manishearth added a commit to Manishearth/rust that referenced this issue Feb 7, 2018
…nikomatsakis

Add filtering options to `rustc_on_unimplemented`

- Add filtering options to `rustc_on_unimplemented` for local traits, filtering on `Self` and type arguments.
- Add a way to provide custom notes.
- Tweak binops text.
- Add filter to detect wether `Self` is local or belongs to another crate.
- Add filter to `Iterator` diagnostic for `&str`.

Partly addresses rust-lang#44755 with a different syntax, as a first approach. Fixes rust-lang#46216, fixes rust-lang#37522, CC rust-lang#34297, rust-lang#46806.
@estebank
Copy link
Contributor

estebank commented Feb 9, 2018

Current output:

error[E0277]: cannot add-assign `{float}` to `{integer}`
 --> src/main.rs:3:7
  |
3 |     x += 1.0;
  |       ^^ no implementation for `{integer} += {float}`
  |
  = help: the trait `std::ops::AddAssign<{float}>` is not implemented for `{integer}`

@estebank
Copy link
Contributor

CC #38564

@estebank
Copy link
Contributor

Closing in favor of #38564.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

7 participants