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

Better diagnostic for binary operation on BoxedValues #59467

Merged
merged 2 commits into from
Mar 29, 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
15 changes: 11 additions & 4 deletions src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(missing_trait) = missing_trait {
if op.node == hir::BinOpKind::Add &&
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
rhs_ty, &mut err, true) {
rhs_ty, &mut err, true, op) {
// This has nothing here because it means we did string
// concatenation (e.g., "Hello " += "World!"). This means
// we don't want the note in the else clause to be emitted
Expand All @@ -327,10 +327,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
err.emit();
}
IsAssign::No => {
let mut err = struct_span_err!(self.tcx.sess, expr.span, E0369,
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);

if !lhs_expr.span.eq(&rhs_expr.span) {
err.span_label(lhs_expr.span, lhs_ty.to_string());
err.span_label(rhs_expr.span, rhs_ty.to_string());
}

let mut suggested_deref = false;
if let Ref(_, mut rty, _) = lhs_ty.sty {
if {
Expand Down Expand Up @@ -380,7 +386,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if let Some(missing_trait) = missing_trait {
if op.node == hir::BinOpKind::Add &&
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
rhs_ty, &mut err, false) {
rhs_ty, &mut err, false, op) {
// This has nothing here because it means we did string
// concatenation (e.g., "Hello " + "World!"). This means
// we don't want the note in the else clause to be emitted
Expand Down Expand Up @@ -418,6 +424,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
rhs_ty: Ty<'tcx>,
err: &mut errors::DiagnosticBuilder<'_>,
is_assign: bool,
op: hir::BinOp,
) -> bool {
let source_map = self.tcx.sess.source_map();
let msg = "`to_owned()` can be used to create an owned `String` \
Expand All @@ -431,7 +438,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
(&Ref(_, l_ty, _), &Ref(_, r_ty, _))
if l_ty.sty == Str && r_ty.sty == Str => {
if !is_assign {
err.span_label(expr.span,
err.span_label(op.span,
"`+` can't be used to concatenate two `&str` strings");
match source_map.span_to_snippet(lhs_expr.span) {
Ok(lstring) => err.span_suggestion(
Expand Down
12 changes: 8 additions & 4 deletions src/test/ui/autoderef-full-lval.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
error[E0369]: binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
--> $DIR/autoderef-full-lval.rs:15:20
--> $DIR/autoderef-full-lval.rs:15:24
|
LL | let z: isize = a.x + b.y;
| ^^^^^^^^^
| --- ^ --- std::boxed::Box<isize>
| |
| std::boxed::Box<isize>
|
= 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>`
--> $DIR/autoderef-full-lval.rs:21:25
--> $DIR/autoderef-full-lval.rs:21:33
|
LL | let answer: isize = forty.a + two.a;
| ^^^^^^^^^^^^^^^
| ------- ^ ----- std::boxed::Box<isize>
| |
| std::boxed::Box<isize>
|
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/binary-op-on-double-ref.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0369]: binary operation `%` cannot be applied to type `&&{integer}`
--> $DIR/binary-op-on-double-ref.rs:4:9
--> $DIR/binary-op-on-double-ref.rs:4:11
|
LL | x % 2 == 0
| ^^^^^
| - ^ - {integer}
| |
| &&{integer}
|
= help: `%` can be used on '{integer}', you can dereference `x`: `*x`

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/binop/binop-bitxor-str.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0369]: binary operation `^` cannot be applied to type `std::string::String`
--> $DIR/binop-bitxor-str.rs:3:21
--> $DIR/binop-bitxor-str.rs:3:37
|
LL | fn main() { let x = "a".to_string() ^ "b".to_string(); }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| --------------- ^ --------------- std::string::String
| |
| std::string::String
|
= note: an implementation of `std::ops::BitXor` might be missing for `std::string::String`

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/binop/binop-mul-bool.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0369]: binary operation `*` cannot be applied to type `bool`
--> $DIR/binop-mul-bool.rs:3:21
--> $DIR/binop-mul-bool.rs:3:26
|
LL | fn main() { let x = true * false; }
| ^^^^^^^^^^^^
| ---- ^ ----- bool
| |
| bool
|
= note: an implementation of `std::ops::Mul` might be missing for `bool`

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/binop/binop-typeck.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0369]: binary operation `+` cannot be applied to type `bool`
--> $DIR/binop-typeck.rs:6:13
--> $DIR/binop-typeck.rs:6:15
|
LL | let z = x + y;
| ^^^^^
| - ^ - {integer}
| |
| bool
|
= note: an implementation of `std::ops::Add` might be missing for `bool`

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/fn/fn-compare-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0369]: binary operation `==` cannot be applied to type `fn() {main::f}`
--> $DIR/fn-compare-mismatch.rs:4:13
--> $DIR/fn-compare-mismatch.rs:4:15
|
LL | let x = f == g;
| ^^^^^^
| - ^^ - fn() {main::g}
| |
| fn() {main::f}
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `fn() {main::f}`

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/for/for-loop-type-error.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0369]: binary operation `+` cannot be applied to type `()`
--> $DIR/for-loop-type-error.rs:2:13
--> $DIR/for-loop-type-error.rs:2:16
|
LL | let x = () + ();
| ^^^^^^^
| -- ^ -- ()
| |
| ()
|
= note: an implementation of `std::ops::Add` might be missing for `()`

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/issues/issue-14915.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0369]: binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
--> $DIR/issue-14915.rs:6:20
--> $DIR/issue-14915.rs:6:22
|
LL | println!("{}", x + 1);
| ^^^^^
| - ^ - {integer}
| |
| std::boxed::Box<isize>
|
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/issues/issue-24363.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ LL | 1.create_a_type_error[
| ^^^^^^^^^^^^^^^^^^^

error[E0369]: binary operation `+` cannot be applied to type `()`
--> $DIR/issue-24363.rs:3:9
--> $DIR/issue-24363.rs:3:11
|
LL | ()+()
| ^^^^^
| --^-- ()
| |
| ()
|
= note: an implementation of `std::ops::Add` might be missing for `()`

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

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

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

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

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

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

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

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

error[E0369]: binary operation `>>` cannot be applied to type `A`
--> $DIR/issue-28837.rs:22:5
--> $DIR/issue-28837.rs:22:7
|
LL | a >> a;
| ^^^^^^
| - ^^ - A
| |
| A
|
= note: an implementation of `std::ops::Shr` might be missing for `A`

error[E0369]: binary operation `==` cannot be applied to type `A`
--> $DIR/issue-28837.rs:24:5
--> $DIR/issue-28837.rs:24:7
|
LL | a == a;
| ^^^^^^
| - ^^ - A
| |
| A
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `A`

error[E0369]: binary operation `!=` cannot be applied to type `A`
--> $DIR/issue-28837.rs:26:5
--> $DIR/issue-28837.rs:26:7
|
LL | a != a;
| ^^^^^^
| - ^^ - A
| |
| A
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `A`

error[E0369]: binary operation `<` cannot be applied to type `A`
--> $DIR/issue-28837.rs:28:5
--> $DIR/issue-28837.rs:28:7
|
LL | a < a;
| ^^^^^
| - ^ - A
| |
| A
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`

error[E0369]: binary operation `<=` cannot be applied to type `A`
--> $DIR/issue-28837.rs:30:5
--> $DIR/issue-28837.rs:30:7
|
LL | a <= a;
| ^^^^^^
| - ^^ - A
| |
| A
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`

error[E0369]: binary operation `>` cannot be applied to type `A`
--> $DIR/issue-28837.rs:32:5
--> $DIR/issue-28837.rs:32:7
|
LL | a > a;
| ^^^^^
| - ^ - A
| |
| A
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`

error[E0369]: binary operation `>=` cannot be applied to type `A`
--> $DIR/issue-28837.rs:34:5
--> $DIR/issue-28837.rs:34:7
|
LL | a >= a;
| ^^^^^^
| - ^^ - A
| |
| A
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`

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

error[E0369]: binary operation `+` cannot be applied to type `i32`
--> $DIR/issue-31076.rs:15:13
--> $DIR/issue-31076.rs:15:18
|
LL | let y = 5i32 + 6i32;
| ^^^^^^^^^^^
| ---- ^ ---- i32
| |
| i32
|
= note: an implementation of `std::ops::Add` might be missing for `i32`

Expand Down
Loading