-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Improve wording of "cannot multiply" type error #78063
Conversation
r? @oli-obk (rust_highfive has picked a reviewer for you, use r? to override) |
cc9712a
to
9e1656e
Compare
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... |
fn foo(x: i32, y: f32) -> f32 { | ||
x * y //~ ERROR cannot multiply `i32` by `f32` | ||
} | ||
|
||
fn main() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need this new test case? This is tested in plenty of other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@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. |
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 compiler
compiler/rustc_typeck/src/check/op.rs: format!("cannot multiply `{}` by `{}`", lhs_ty, rhs_ty), |
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:
|
Wow, thank you! I didn't think to look in |
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
7b33ae6
Compare
use std::ops::Add;
fn incr<T:Add<i32>>(x: T) -> T::Output {
x + 1 // this is fine
}
fn main() {
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 |
@bors r+ |
📌 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: