1
Fork 0

Rollup merge of #78063 - camelid:improve-cannot-multiply-error, 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`
This commit is contained in:
Yuki Okushi 2020-10-21 13:59:39 +09:00 committed by GitHub
commit 89c98cd6b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 33 additions and 17 deletions

View file

@ -302,7 +302,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
true, true,
), ),
hir::BinOpKind::Mul => ( hir::BinOpKind::Mul => (
format!("cannot multiply `{}` to `{}`", rhs_ty, lhs_ty), format!("cannot multiply `{}` by `{}`", lhs_ty, rhs_ty),
Some("std::ops::Mul"), Some("std::ops::Mul"),
true, true,
), ),

View file

@ -302,7 +302,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
#[lang = "mul"] #[lang = "mul"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented( #[rustc_on_unimplemented(
message = "cannot multiply `{Rhs}` to `{Self}`", message = "cannot multiply `{Self}` by `{Rhs}`",
label = "no implementation for `{Self} * {Rhs}`" label = "no implementation for `{Self} * {Rhs}`"
)] )]
#[doc(alias = "*")] #[doc(alias = "*")]
@ -826,7 +826,7 @@ sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
#[lang = "mul_assign"] #[lang = "mul_assign"]
#[stable(feature = "op_assign_traits", since = "1.8.0")] #[stable(feature = "op_assign_traits", since = "1.8.0")]
#[rustc_on_unimplemented( #[rustc_on_unimplemented(
message = "cannot multiply-assign `{Rhs}` to `{Self}`", message = "cannot multiply-assign `{Self}` by `{Rhs}`",
label = "no implementation for `{Self} *= {Rhs}`" label = "no implementation for `{Self} *= {Rhs}`"
)] )]
#[doc(alias = "*")] #[doc(alias = "*")]

View file

@ -1,3 +1,3 @@
// error-pattern:cannot multiply `bool` to `bool` // error-pattern:cannot multiply `bool` by `bool`
fn main() { let x = true * false; } fn main() { let x = true * false; }

View file

@ -1,4 +1,4 @@
error[E0369]: cannot multiply `bool` to `bool` error[E0369]: cannot multiply `bool` by `bool`
--> $DIR/binop-mul-bool.rs:3:26 --> $DIR/binop-mul-bool.rs:3:26
| |
LL | fn main() { let x = true * false; } LL | fn main() { let x = true * false; }

View file

@ -0,0 +1,5 @@
fn foo(x: i32, y: f32) -> f32 {
x * y //~ ERROR cannot multiply `i32` by `f32`
}
fn main() {}

View file

@ -0,0 +1,11 @@
error[E0277]: cannot multiply `i32` by `f32`
--> $DIR/binop-mul-i32-f32.rs:2:7
|
LL | x * y
| ^ no implementation for `i32 * f32`
|
= help: the trait `Mul<f32>` is not implemented for `i32`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.

View file

@ -7,7 +7,7 @@ fn main() {
a - a; //~ ERROR cannot subtract `A` from `A` a - a; //~ ERROR cannot subtract `A` from `A`
a * a; //~ ERROR cannot multiply `A` to `A` a * a; //~ ERROR cannot multiply `A` by `A`
a / a; //~ ERROR cannot divide `A` by `A` a / a; //~ ERROR cannot divide `A` by `A`

View file

@ -18,7 +18,7 @@ LL | a - a;
| |
= note: an implementation of `std::ops::Sub` might be missing for `A` = note: an implementation of `std::ops::Sub` might be missing for `A`
error[E0369]: cannot multiply `A` to `A` error[E0369]: cannot multiply `A` by `A`
--> $DIR/issue-28837.rs:10:7 --> $DIR/issue-28837.rs:10:7
| |
LL | a * a; LL | a * a;

View file

@ -1,6 +1,6 @@
fn func<'a, T>(a: &'a [T]) -> impl Iterator<Item=&'a T> { fn func<'a, T>(a: &'a [T]) -> impl Iterator<Item=&'a T> {
a.iter().map(|a| a*a) a.iter().map(|a| a*a)
//~^ ERROR cannot multiply `&T` to `&T` //~^ ERROR cannot multiply `&T` by `&T`
} }
fn main() { fn main() {

View file

@ -1,4 +1,4 @@
error[E0369]: cannot multiply `&T` to `&T` error[E0369]: cannot multiply `&T` by `&T`
--> $DIR/issue-35668.rs:2:23 --> $DIR/issue-35668.rs:2:23
| |
LL | a.iter().map(|a| a*a) LL | a.iter().map(|a| a*a)

View file

@ -11,5 +11,5 @@ impl Thing {
fn main() { fn main() {
let u = Thing {x: 2}; let u = Thing {x: 2};
let _v = u.mul(&3); // This is ok let _v = u.mul(&3); // This is ok
let w = u * 3; //~ ERROR cannot multiply `{integer}` to `Thing` let w = u * 3; //~ ERROR cannot multiply `Thing` by `{integer}`
} }

View file

@ -1,4 +1,4 @@
error[E0369]: cannot multiply `{integer}` to `Thing` error[E0369]: cannot multiply `Thing` by `{integer}`
--> $DIR/issue-3820.rs:14:15 --> $DIR/issue-3820.rs:14:15
| |
LL | let w = u * 3; LL | let w = u * 3;

View file

@ -1,7 +1,7 @@
fn main() { fn main() {
1 + Some(1); //~ ERROR cannot add `Option<{integer}>` to `{integer}` 1 + Some(1); //~ ERROR cannot add `Option<{integer}>` to `{integer}`
2 as usize - Some(1); //~ ERROR cannot subtract `Option<{integer}>` from `usize` 2 as usize - Some(1); //~ ERROR cannot subtract `Option<{integer}>` from `usize`
3 * (); //~ ERROR cannot multiply `()` to `{integer}` 3 * (); //~ ERROR cannot multiply `{integer}` by `()`
4 / ""; //~ ERROR cannot divide `{integer}` by `&str` 4 / ""; //~ ERROR cannot divide `{integer}` by `&str`
5 < String::new(); //~ ERROR can't compare `{integer}` with `String` 5 < String::new(); //~ ERROR can't compare `{integer}` with `String`
6 == Ok(1); //~ ERROR can't compare `{integer}` with `std::result::Result<{integer}, _>` 6 == Ok(1); //~ ERROR can't compare `{integer}` with `std::result::Result<{integer}, _>`

View file

@ -14,7 +14,7 @@ LL | 2 as usize - Some(1);
| |
= help: the trait `Sub<Option<{integer}>>` is not implemented for `usize` = help: the trait `Sub<Option<{integer}>>` is not implemented for `usize`
error[E0277]: cannot multiply `()` to `{integer}` error[E0277]: cannot multiply `{integer}` by `()`
--> $DIR/binops.rs:4:7 --> $DIR/binops.rs:4:7
| |
LL | 3 * (); LL | 3 * ();

View file

@ -1,6 +1,6 @@
enum Bar { T1((), Option<Vec<isize>>), T2, } enum Bar { T1((), Option<Vec<isize>>), T2, }
fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3; } _ => { panic!(); } } } fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3; } _ => { panic!(); } } }
//~^ ERROR cannot multiply `{integer}` to `Vec<isize>` //~^ ERROR cannot multiply `Vec<isize>` by `{integer}`
fn main() { } fn main() { }

View file

@ -1,4 +1,4 @@
error[E0369]: cannot multiply `{integer}` to `Vec<isize>` error[E0369]: cannot multiply `Vec<isize>` by `{integer}`
--> $DIR/pattern-tyvar-2.rs:3:71 --> $DIR/pattern-tyvar-2.rs:3:71
| |
LL | fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3; } _ => { panic!(); } } } LL | fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3; } _ => { panic!(); } } }

View file

@ -5,7 +5,7 @@ trait MyMul<Rhs, Res> {
} }
fn foo<T: MyMul<f64, f64>>(a: &T, b: f64) -> f64 { fn foo<T: MyMul<f64, f64>>(a: &T, b: f64) -> f64 {
a * b //~ ERROR cannot multiply `f64` to `&T` a * b //~ ERROR cannot multiply `&T` by `f64`
} }
fn main() {} fn main() {}

View file

@ -1,4 +1,4 @@
error[E0369]: cannot multiply `f64` to `&T` error[E0369]: cannot multiply `&T` by `f64`
--> $DIR/trait-resolution-in-overloaded-op.rs:8:7 --> $DIR/trait-resolution-in-overloaded-op.rs:8:7
| |
LL | a * b LL | a * b