Uh oh!
There was an error while loading. Please reload this page.
Improve wording of "cannot multiply" type error - #78063
Conversation
rust-highfive
commented
Oct 18, 2020
r? @oli-obk (rust_highfive has picked a reviewer for you, use r? to override) |
cc9712a to
9e1656eComparecamelid
commented
Oct 18, 2020
For some reason it only seems to be working for some types – the old error persists for others, even though I can't find any other spot in the compiler that generates this error. Maybe my build cache is corrupted or something... |
There was a problem hiding this comment.
Do you need this new test case? This is tested in plenty of other places.
There was a problem hiding this comment.
I couldn't find a place where a simple case of the error was tested, and I wanted a sanity check since this is behaving weirdly. But if you think I should remove it once we figure out what's going awry, I guess I could. I just wanted extra coverage while I figured it out :)
There was a problem hiding this comment.
I think I'm going to keep this test unless there are significant objections since this helped catch a spot that I didn't know to update.
camelid
commented
Oct 18, 2020
@estebank What could explain this test failure from CI? I've been scratching my head and I also asked @jyn514 but neither of us could think of what this could be. It seems the old error is being reported in some cases, and the new one in others. |
camelid
commented
Oct 18, 2020
I grepped through the whole compiler, but couldn't find anything even close to this error other than the spot I changed: $ grep 'cannot multiply' -r compilercompiler/rustc_typeck/src/check/op.rs: format!("cannot multiply `{}` by `{}`", lhs_ty, rhs_ty), |
SNCPlay42
commented
Oct 18, 2020
It's from the rust/library/core/src/ops/arith.rs Lines 304 to 309 in cbc42a0 |
There are two lines of code similar in the library directory: |
camelid
commented
Oct 18, 2020
Wow, thank you! I didn't think to look in |
camelid
commented
Oct 18, 2020
Why would the error be produced in the compiler and the standard library? |
For example, if you had this code:
fn foo(x: i32, y: f32) -> f32 {
x * y
}
You would get this error:
error[E0277]: cannot multiply `f32` to `i32`
--> src/lib.rs:2:7
|
2 | x * y
| ^ no implementation for `i32 * f32`
|
= help: the trait `Mul<f32>` is not implemented for `i32`
However, that's not usually how people describe multiplication. People
usually describe multiplication like how the division error words it:
error[E0277]: cannot divide `i32` by `f32`
--> src/lib.rs:2:7
|
2 | x / y
| ^ no implementation for `i32 / f32`
|
= help: the trait `Div<f32>` is not implemented for `i32`
So that's what this change does. It changes this:
error[E0277]: cannot multiply `f32` to `i32`
--> src/lib.rs:2:7
|
2 | x * y
| ^ no implementation for `i32 * f32`
|
= help: the trait `Mul<f32>` is not implemented for `i32`
To this:
error[E0277]: cannot multiply `i32` by `f32`
--> src/lib.rs:2:7
|
2 | x * y
| ^ no implementation for `i32 * f32`
|
= help: the trait `Mul<f32>` is not implemented for `i32`9e1656e to
7b33ae6CompareSNCPlay42
commented
Oct 18, 2020
use std::ops::Add;fnincr<T:Add<i32>>(x:T) -> T::Output{
x + 1// this is fine}fnmain(){incr(());//~ ERROR cannot add `i32` to `()`}It's an attribute placeable on library traits rather than hardcoded into the compiler because it's used on all sorts of stdlib traits (not just the binops) that might not even be lang items, e.g. We get that kind of error in your test because the diagnostics in |
estebank
commented
Oct 19, 2020
@bors r+ |
bors
commented
Oct 19, 2020
📌 Commit 7b33ae6 has been approved by |
…or, r=estebank
Improve wording of "cannot multiply" type error
For example, if you had this code:
fn foo(x: i32, y: f32) -> f32 {
x * y
}
You would get this error:
error[E0277]: cannot multiply `f32` to `i32`
--> src/lib.rs:2:7
|
2 | x * y
| ^ no implementation for `i32 * f32`
|
= help: the trait `Mul<f32>` is not implemented for `i32`
However, that's not usually how people describe multiplication. People
usually describe multiplication like how the division error words it:
error[E0277]: cannot divide `i32` by `f32`
--> src/lib.rs:2:7
|
2 | x / y
| ^ no implementation for `i32 / f32`
|
= help: the trait `Div<f32>` is not implemented for `i32`
So that's what this change does. It changes this:
error[E0277]: cannot multiply `f32` to `i32`
--> src/lib.rs:2:7
|
2 | x * y
| ^ no implementation for `i32 * f32`
|
= help: the trait `Mul<f32>` is not implemented for `i32`
To this:
error[E0277]: cannot multiply `i32` by `f32`
--> src/lib.rs:2:7
|
2 | x * y
| ^ no implementation for `i32 * f32`
|
= help: the trait `Mul<f32>` is not implemented for `i32`Rollup of 7 pull requests Successful merges: - rust-lang#77726 (Add Pin::static_ref, static_mut.) - rust-lang#78002 (Tweak "object unsafe" errors) - rust-lang#78056 (BTreeMap: split off most code of remove and split_off) - rust-lang#78063 (Improve wording of "cannot multiply" type error) - rust-lang#78094 (rustdoc: Show the correct source filename in page titles, without `.html`) - rust-lang#78101 (fix static_ptr_ty for foreign statics) - rust-lang#78118 (Inline const followups) Failed merges: r? `@ghost`
For example, if you had this code:
You would get this error:
However, that's not usually how people describe multiplication. People
usually describe multiplication like how the division error words it:
So that's what this change does. It changes this:
To this: