Skip to content

Improve wording of "cannot multiply" type error #78063

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

Merged
merged 1 commit into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
true,
),
hir::BinOpKind::Mul => (
format!("cannot multiply `{}` to `{}`", rhs_ty, lhs_ty),
format!("cannot multiply `{}` by `{}`", lhs_ty, rhs_ty),
Some("std::ops::Mul"),
true,
),
Expand Down
4 changes: 2 additions & 2 deletions library/core/src/ops/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
#[lang = "mul"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(
message = "cannot multiply `{Rhs}` to `{Self}`",
message = "cannot multiply `{Self}` by `{Rhs}`",
label = "no implementation for `{Self} * {Rhs}`"
)]
#[doc(alias = "*")]
Expand Down Expand Up @@ -826,7 +826,7 @@ sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
#[lang = "mul_assign"]
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[rustc_on_unimplemented(
message = "cannot multiply-assign `{Rhs}` to `{Self}`",
message = "cannot multiply-assign `{Self}` by `{Rhs}`",
label = "no implementation for `{Self} *= {Rhs}`"
)]
#[doc(alias = "*")]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/binop/binop-mul-bool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// error-pattern:cannot multiply `bool` to `bool`
// error-pattern:cannot multiply `bool` by `bool`

fn main() { let x = true * false; }
2 changes: 1 addition & 1 deletion src/test/ui/binop/binop-mul-bool.stderr
Original file line number Diff line number Diff line change
@@ -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
|
LL | fn main() { let x = true * false; }
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/binop/binop-mul-i32-f32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn foo(x: i32, y: f32) -> f32 {
x * y //~ ERROR cannot multiply `i32` by `f32`
}

fn main() {}
Comment on lines +1 to +5
Copy link
Member

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.

Copy link
Member Author

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 :)

Copy link
Member Author

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.

11 changes: 11 additions & 0 deletions src/test/ui/binop/binop-mul-i32-f32.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-28837.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {

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`

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-28837.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LL | a - 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
|
LL | a * a;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-35668.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn func<'a, T>(a: &'a [T]) -> impl Iterator<Item=&'a T> {
a.iter().map(|a| a*a)
//~^ ERROR cannot multiply `&T` to `&T`
//~^ ERROR cannot multiply `&T` by `&T`
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-35668.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: cannot multiply `&T` to `&T`
error[E0369]: cannot multiply `&T` by `&T`
--> $DIR/issue-35668.rs:2:23
|
LL | a.iter().map(|a| a*a)
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-3820.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ impl Thing {
fn main() {
let u = Thing {x: 2};
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}`
}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-3820.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: cannot multiply `{integer}` to `Thing`
error[E0369]: cannot multiply `Thing` by `{integer}`
--> $DIR/issue-3820.rs:14:15
|
LL | let w = u * 3;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/mismatched_types/binops.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() {
1 + Some(1); //~ ERROR cannot add `Option<{integer}>` to `{integer}`
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`
5 < String::new(); //~ ERROR can't compare `{integer}` with `String`
6 == Ok(1); //~ ERROR can't compare `{integer}` with `std::result::Result<{integer}, _>`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/mismatched_types/binops.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LL | 2 as usize - Some(1);
|
= 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
|
LL | 3 * ();
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/pattern/pattern-tyvar-2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
enum Bar { T1((), Option<Vec<isize>>), T2, }

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() { }
2 changes: 1 addition & 1 deletion src/test/ui/pattern/pattern-tyvar-2.stderr
Original file line number Diff line number Diff line change
@@ -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
|
LL | fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3; } _ => { panic!(); } } }
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/traits/trait-resolution-in-overloaded-op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ trait MyMul<Rhs, Res> {
}

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() {}
Original file line number Diff line number Diff line change
@@ -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
|
LL | a * b
Expand Down