Skip to content
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

Unify binop wording #67189

Merged
merged 7 commits into from
Dec 19, 2019
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
85 changes: 63 additions & 22 deletions src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,70 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.emit();
}
IsAssign::No => {
let (message, missing_trait) = match op.node {
hir::BinOpKind::Add => {
(format!("cannot add `{}` to `{}`", rhs_ty, lhs_ty),
Some("std::ops::Add"))
},
hir::BinOpKind::Sub => {
(format!("cannot substract `{}` from `{}`", rhs_ty, lhs_ty),
Some("std::ops::Sub"))
},
hir::BinOpKind::Mul => {
(format!("cannot multiply `{}` to `{}`", rhs_ty, lhs_ty),
Some("std::ops::Mul"))
},
hir::BinOpKind::Div => {
(format!("cannot divide `{}` by `{}`", lhs_ty, rhs_ty),
Some("std::ops::Div"))
},
hir::BinOpKind::Rem => {
(format!("cannot mod `{}` by `{}`", lhs_ty, rhs_ty),
Some("std::ops::Rem"))
},
hir::BinOpKind::BitAnd => {
(format!("no implementation for `{} & {}`", lhs_ty, rhs_ty),
Some("std::ops::BitAnd"))
},
hir::BinOpKind::BitXor => {
(format!("no implementation for `{} ^ {}`", lhs_ty, rhs_ty),
Some("std::ops::BitXor"))
},
hir::BinOpKind::BitOr => {
(format!("no implementation for `{} | {}`", lhs_ty, rhs_ty),
Some("std::ops::BitOr"))
},
hir::BinOpKind::Shl => {
(format!("no implementation for `{} << {}`", lhs_ty, rhs_ty),
Some("std::ops::Shl"))
},
hir::BinOpKind::Shr => {
(format!("no implementation for `{} >> {}`", lhs_ty, rhs_ty),
Some("std::ops::Shr"))
},
hir::BinOpKind::Eq |
hir::BinOpKind::Ne => {
(format!(
"binary operation `{}` cannot be applied to type `{}`",
op.node.as_str(), lhs_ty),
Some("std::cmp::PartialEq"))
},
hir::BinOpKind::Lt |
hir::BinOpKind::Le |
hir::BinOpKind::Gt |
hir::BinOpKind::Ge => {
(format!(
"binary operation `{}` cannot be applied to type `{}`",
op.node.as_str(), lhs_ty),
Some("std::cmp::PartialOrd"))
}
_ => (format!(
"binary operation `{}` cannot be applied to type `{}`",
op.node.as_str(), lhs_ty),
None)
};
let mut err = struct_span_err!(self.tcx.sess, op.span, E0369,
"binary operation `{}` cannot be applied to type `{}`",
op.node.as_str(),
lhs_ty);
"{}", message.as_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think .as_str() is needed here.


let mut involves_fn = false;
if !lhs_expr.span.eq(&rhs_expr.span) {
Expand Down Expand Up @@ -382,25 +442,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
}
let missing_trait = match op.node {
hir::BinOpKind::Add => Some("std::ops::Add"),
hir::BinOpKind::Sub => Some("std::ops::Sub"),
hir::BinOpKind::Mul => Some("std::ops::Mul"),
hir::BinOpKind::Div => Some("std::ops::Div"),
hir::BinOpKind::Rem => Some("std::ops::Rem"),
hir::BinOpKind::BitAnd => Some("std::ops::BitAnd"),
hir::BinOpKind::BitXor => Some("std::ops::BitXor"),
hir::BinOpKind::BitOr => Some("std::ops::BitOr"),
hir::BinOpKind::Shl => Some("std::ops::Shl"),
hir::BinOpKind::Shr => Some("std::ops::Shr"),
hir::BinOpKind::Eq |
hir::BinOpKind::Ne => Some("std::cmp::PartialEq"),
hir::BinOpKind::Lt |
hir::BinOpKind::Le |
hir::BinOpKind::Gt |
hir::BinOpKind::Ge => Some("std::cmp::PartialOrd"),
_ => None
};
if let Some(missing_trait) = missing_trait {
if op.node == hir::BinOpKind::Add &&
self.check_str_addition(
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/autoderef-full-lval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ fn main() {
let a: Clam = Clam{x: box 1, y: box 2};
let b: Clam = Clam{x: box 10, y: box 20};
let z: isize = a.x + b.y;
//~^ ERROR binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
//~^ ERROR cannot add `std::boxed::Box<isize>` to `std::boxed::Box<isize>`
println!("{}", z);
assert_eq!(z, 21);
let forty: Fish = Fish{a: box 40};
let two: Fish = Fish{a: box 2};
let answer: isize = forty.a + two.a;
//~^ ERROR binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
//~^ ERROR cannot add `std::boxed::Box<isize>` to `std::boxed::Box<isize>`
println!("{}", answer);
assert_eq!(answer, 42);
}
4 changes: 2 additions & 2 deletions src/test/ui/autoderef-full-lval.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
error[E0369]: cannot add `std::boxed::Box<isize>` to `std::boxed::Box<isize>`
--> $DIR/autoderef-full-lval.rs:15:24
|
LL | let z: isize = a.x + b.y;
Expand All @@ -8,7 +8,7 @@ LL | let z: isize = a.x + b.y;
|
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`

error[E0369]: binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
error[E0369]: cannot add `std::boxed::Box<isize>` to `std::boxed::Box<isize>`
--> $DIR/autoderef-full-lval.rs:21:33
|
LL | let answer: isize = forty.a + two.a;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/binary-op-on-double-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main() {
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
let vr = v.iter().filter(|x| {
x % 2 == 0
//~^ ERROR binary operation `%` cannot be applied to type `&&{integer}`
//~^ ERROR cannot mod `&&{integer}` by `{integer}`
});
println!("{:?}", vr);
}
2 changes: 1 addition & 1 deletion src/test/ui/binary-op-on-double-ref.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `%` cannot be applied to type `&&{integer}`
error[E0369]: cannot mod `&&{integer}` by `{integer}`
--> $DIR/binary-op-on-double-ref.rs:4:11
|
LL | x % 2 == 0
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/binop/binop-bitxor-str.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// error-pattern:`^` cannot be applied to type `std::string::String`
// error-pattern:no implementation for `std::string::String ^ std::string::String`

fn main() { let x = "a".to_string() ^ "b".to_string(); }
2 changes: 1 addition & 1 deletion src/test/ui/binop/binop-bitxor-str.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `^` cannot be applied to type `std::string::String`
error[E0369]: no implementation for `std::string::String ^ std::string::String`
--> $DIR/binop-bitxor-str.rs:3:37
|
LL | fn main() { let x = "a".to_string() ^ "b".to_string(); }
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 be applied to type `bool`
// error-pattern:cannot multiply `bool` to `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]: binary operation `*` cannot be applied to type `bool`
error[E0369]: cannot multiply `bool` to `bool`
--> $DIR/binop-mul-bool.rs:3:26
|
LL | fn main() { let x = true * false; }
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/binop/binop-typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ fn main() {
let x = true;
let y = 1;
let z = x + y;
//~^ ERROR binary operation `+` cannot be applied to type `bool`
//~^ ERROR cannot add `{integer}` to `bool`
}
2 changes: 1 addition & 1 deletion src/test/ui/binop/binop-typeck.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `+` cannot be applied to type `bool`
error[E0369]: cannot add `{integer}` to `bool`
--> $DIR/binop-typeck.rs:6:15
|
LL | let z = x + y;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/for/for-loop-type-error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn main() {
let x = () + (); //~ ERROR binary operation
let x = () + (); //~ ERROR cannot add `()` to `()`

// this shouldn't have a flow-on error:
for _ in x {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/for/for-loop-type-error.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `+` cannot be applied to type `()`
error[E0369]: cannot add `()` to `()`
--> $DIR/for-loop-type-error.rs:2:16
|
LL | let x = () + ();
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-14915.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ fn main() {
let x: Box<isize> = box 0;

println!("{}", x + 1);
//~^ ERROR binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
//~^ ERROR cannot add `{integer}` to `std::boxed::Box<isize>`
}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-14915.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
error[E0369]: cannot add `{integer}` to `std::boxed::Box<isize>`
--> $DIR/issue-14915.rs:6:22
|
LL | println!("{}", x + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-24363.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
1.create_a_type_error[ //~ `{integer}` is a primitive type and therefore doesn't have fields
()+() //~ ERROR binary operation `+` cannot be applied
()+() //~ ERROR cannot add
// ^ ensure that we typeck the inner expression ^
];
}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-24363.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
LL | 1.create_a_type_error[
| ^^^^^^^^^^^^^^^^^^^

error[E0369]: binary operation `+` cannot be applied to type `()`
error[E0369]: cannot add `()` to `()`
--> $DIR/issue-24363.rs:3:11
|
LL | ()+()
Expand Down
18 changes: 9 additions & 9 deletions src/test/ui/issues/issue-28837.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ struct A;
fn main() {
let a = A;

a + a; //~ ERROR binary operation `+` cannot be applied to type `A`
a + a; //~ ERROR cannot add `A` to `A`

a - a; //~ ERROR binary operation `-` cannot be applied to type `A`
a - a; //~ ERROR cannot substract `A` from `A`

a * a; //~ ERROR binary operation `*` cannot be applied to type `A`
a * a; //~ ERROR cannot multiply `A` to `A`

a / a; //~ ERROR binary operation `/` cannot be applied to type `A`
a / a; //~ ERROR cannot divide `A` by `A`

a % a; //~ ERROR binary operation `%` cannot be applied to type `A`
a % a; //~ ERROR cannot mod `A` by `A`

a & a; //~ ERROR binary operation `&` cannot be applied to type `A`
a & a; //~ ERROR no implementation for `A & A`

a | a; //~ ERROR binary operation `|` cannot be applied to type `A`
a | a; //~ ERROR no implementation for `A | A`

a << a; //~ ERROR binary operation `<<` cannot be applied to type `A`
a << a; //~ ERROR no implementation for `A << A`

a >> a; //~ ERROR binary operation `>>` cannot be applied to type `A`
a >> a; //~ ERROR no implementation for `A >> A`

a == a; //~ ERROR binary operation `==` cannot be applied to type `A`

Expand Down
18 changes: 9 additions & 9 deletions src/test/ui/issues/issue-28837.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `+` cannot be applied to type `A`
error[E0369]: cannot add `A` to `A`
--> $DIR/issue-28837.rs:6:7
|
LL | a + a;
Expand All @@ -8,7 +8,7 @@ LL | a + a;
|
= note: an implementation of `std::ops::Add` might be missing for `A`

error[E0369]: binary operation `-` cannot be applied to type `A`
error[E0369]: cannot substract `A` from `A`
--> $DIR/issue-28837.rs:8:7
|
LL | a - a;
Expand All @@ -18,7 +18,7 @@ LL | a - a;
|
= note: an implementation of `std::ops::Sub` might be missing for `A`

error[E0369]: binary operation `*` cannot be applied to type `A`
error[E0369]: cannot multiply `A` to `A`
--> $DIR/issue-28837.rs:10:7
|
LL | a * a;
Expand All @@ -28,7 +28,7 @@ LL | a * a;
|
= note: an implementation of `std::ops::Mul` might be missing for `A`

error[E0369]: binary operation `/` cannot be applied to type `A`
error[E0369]: cannot divide `A` by `A`
--> $DIR/issue-28837.rs:12:7
|
LL | a / a;
Expand All @@ -38,7 +38,7 @@ LL | a / a;
|
= note: an implementation of `std::ops::Div` might be missing for `A`

error[E0369]: binary operation `%` cannot be applied to type `A`
error[E0369]: cannot mod `A` by `A`
--> $DIR/issue-28837.rs:14:7
|
LL | a % a;
Expand All @@ -48,7 +48,7 @@ LL | a % a;
|
= note: an implementation of `std::ops::Rem` might be missing for `A`

error[E0369]: binary operation `&` cannot be applied to type `A`
error[E0369]: no implementation for `A & A`
--> $DIR/issue-28837.rs:16:7
|
LL | a & a;
Expand All @@ -58,7 +58,7 @@ LL | a & a;
|
= note: an implementation of `std::ops::BitAnd` might be missing for `A`

error[E0369]: binary operation `|` cannot be applied to type `A`
error[E0369]: no implementation for `A | A`
--> $DIR/issue-28837.rs:18:7
|
LL | a | a;
Expand All @@ -68,7 +68,7 @@ LL | a | a;
|
= note: an implementation of `std::ops::BitOr` might be missing for `A`

error[E0369]: binary operation `<<` cannot be applied to type `A`
error[E0369]: no implementation for `A << A`
--> $DIR/issue-28837.rs:20:7
|
LL | a << a;
Expand All @@ -78,7 +78,7 @@ LL | a << a;
|
= note: an implementation of `std::ops::Shl` might be missing for `A`

error[E0369]: binary operation `>>` cannot be applied to type `A`
error[E0369]: no implementation for `A >> A`
--> $DIR/issue-28837.rs:22:7
|
LL | a >> a;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-31076.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Add<i32> for i32 {}

fn main() {
let x = 5 + 6;
//~^ ERROR binary operation `+` cannot be applied to type `{integer}`
//~^ ERROR cannot add `{integer}` to `{integer}`
let y = 5i32 + 6i32;
//~^ ERROR binary operation `+` cannot be applied to type `i32`
//~^ ERROR cannot add `i32` to `i32`
}
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-31076.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `+` cannot be applied to type `{integer}`
error[E0369]: cannot add `{integer}` to `{integer}`
--> $DIR/issue-31076.rs:13:15
|
LL | let x = 5 + 6;
Expand All @@ -8,7 +8,7 @@ LL | let x = 5 + 6;
|
= note: an implementation of `std::ops::Add` might be missing for `{integer}`

error[E0369]: binary operation `+` cannot be applied to type `i32`
error[E0369]: cannot add `i32` to `i32`
--> $DIR/issue-31076.rs:15:18
|
LL | let y = 5i32 + 6i32;
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 binary operation `*` cannot be applied to type `&T`
//~^ ERROR cannot multiply `&T` to `&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]: binary operation `*` cannot be applied to type `&T`
error[E0369]: cannot multiply `&T` to `&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 binary operation `*` cannot be applied to type `Thing`
let w = u * 3; //~ ERROR cannot multiply `{integer}` to `Thing`
}
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]: binary operation `*` cannot be applied to type `Thing`
error[E0369]: cannot multiply `{integer}` to `Thing`
--> $DIR/issue-3820.rs:14:15
|
LL | let w = u * 3;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-40610.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ fn f(_: &[f32]) {}

fn main() {
() + f(&[1.0]);
//~^ ERROR binary operation `+` cannot be applied to type `()`
//~^ ERROR cannot add `()` to `()`
}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-40610.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0369]: binary operation `+` cannot be applied to type `()`
error[E0369]: cannot add `()` to `()`
--> $DIR/issue-40610.rs:4:8
|
LL | () + f(&[1.0]);
Expand Down
Loading