Skip to content

Commit 89c98cd

Browse files
authored
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`
2 parents f8bae8b + 7b33ae6 commit 89c98cd

18 files changed

+33
-17
lines changed

compiler/rustc_typeck/src/check/op.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
302302
true,
303303
),
304304
hir::BinOpKind::Mul => (
305-
format!("cannot multiply `{}` to `{}`", rhs_ty, lhs_ty),
305+
format!("cannot multiply `{}` by `{}`", lhs_ty, rhs_ty),
306306
Some("std::ops::Mul"),
307307
true,
308308
),

library/core/src/ops/arith.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
302302
#[lang = "mul"]
303303
#[stable(feature = "rust1", since = "1.0.0")]
304304
#[rustc_on_unimplemented(
305-
message = "cannot multiply `{Rhs}` to `{Self}`",
305+
message = "cannot multiply `{Self}` by `{Rhs}`",
306306
label = "no implementation for `{Self} * {Rhs}`"
307307
)]
308308
#[doc(alias = "*")]
@@ -826,7 +826,7 @@ sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
826826
#[lang = "mul_assign"]
827827
#[stable(feature = "op_assign_traits", since = "1.8.0")]
828828
#[rustc_on_unimplemented(
829-
message = "cannot multiply-assign `{Rhs}` to `{Self}`",
829+
message = "cannot multiply-assign `{Self}` by `{Rhs}`",
830830
label = "no implementation for `{Self} *= {Rhs}`"
831831
)]
832832
#[doc(alias = "*")]

src/test/ui/binop/binop-mul-bool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// error-pattern:cannot multiply `bool` to `bool`
1+
// error-pattern:cannot multiply `bool` by `bool`
22

33
fn main() { let x = true * false; }

src/test/ui/binop/binop-mul-bool.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0369]: cannot multiply `bool` to `bool`
1+
error[E0369]: cannot multiply `bool` by `bool`
22
--> $DIR/binop-mul-bool.rs:3:26
33
|
44
LL | fn main() { let x = true * false; }
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn foo(x: i32, y: f32) -> f32 {
2+
x * y //~ ERROR cannot multiply `i32` by `f32`
3+
}
4+
5+
fn main() {}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: cannot multiply `i32` by `f32`
2+
--> $DIR/binop-mul-i32-f32.rs:2:7
3+
|
4+
LL | x * y
5+
| ^ no implementation for `i32 * f32`
6+
|
7+
= help: the trait `Mul<f32>` is not implemented for `i32`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/issues/issue-28837.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77

88
a - a; //~ ERROR cannot subtract `A` from `A`
99

10-
a * a; //~ ERROR cannot multiply `A` to `A`
10+
a * a; //~ ERROR cannot multiply `A` by `A`
1111

1212
a / a; //~ ERROR cannot divide `A` by `A`
1313

src/test/ui/issues/issue-28837.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | a - a;
1818
|
1919
= note: an implementation of `std::ops::Sub` might be missing for `A`
2020

21-
error[E0369]: cannot multiply `A` to `A`
21+
error[E0369]: cannot multiply `A` by `A`
2222
--> $DIR/issue-28837.rs:10:7
2323
|
2424
LL | a * a;

src/test/ui/issues/issue-35668.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn func<'a, T>(a: &'a [T]) -> impl Iterator<Item=&'a T> {
22
a.iter().map(|a| a*a)
3-
//~^ ERROR cannot multiply `&T` to `&T`
3+
//~^ ERROR cannot multiply `&T` by `&T`
44
}
55

66
fn main() {

src/test/ui/issues/issue-35668.stderr

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

src/test/ui/issues/issue-3820.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ impl Thing {
1111
fn main() {
1212
let u = Thing {x: 2};
1313
let _v = u.mul(&3); // This is ok
14-
let w = u * 3; //~ ERROR cannot multiply `{integer}` to `Thing`
14+
let w = u * 3; //~ ERROR cannot multiply `Thing` by `{integer}`
1515
}

src/test/ui/issues/issue-3820.stderr

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

src/test/ui/mismatched_types/binops.rs

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

src/test/ui/mismatched_types/binops.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | 2 as usize - Some(1);
1414
|
1515
= help: the trait `Sub<Option<{integer}>>` is not implemented for `usize`
1616

17-
error[E0277]: cannot multiply `()` to `{integer}`
17+
error[E0277]: cannot multiply `{integer}` by `()`
1818
--> $DIR/binops.rs:4:7
1919
|
2020
LL | 3 * ();
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
enum Bar { T1((), Option<Vec<isize>>), T2, }
22

33
fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3; } _ => { panic!(); } } }
4-
//~^ ERROR cannot multiply `{integer}` to `Vec<isize>`
4+
//~^ ERROR cannot multiply `Vec<isize>` by `{integer}`
55

66
fn main() { }

src/test/ui/pattern/pattern-tyvar-2.stderr

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

src/test/ui/traits/trait-resolution-in-overloaded-op.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ trait MyMul<Rhs, Res> {
55
}
66

77
fn foo<T: MyMul<f64, f64>>(a: &T, b: f64) -> f64 {
8-
a * b //~ ERROR cannot multiply `f64` to `&T`
8+
a * b //~ ERROR cannot multiply `&T` by `f64`
99
}
1010

1111
fn main() {}

src/test/ui/traits/trait-resolution-in-overloaded-op.stderr

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

0 commit comments

Comments
 (0)