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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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; }
Lines changed: 5 additions & 0 deletions
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() {}
Lines changed: 11 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)

0 commit comments

Comments
 (0)