Skip to content

Commit fda2066

Browse files
authored
Rollup merge of rust-lang#59467 - hgallagher1993:local_branch, r=estebank
Better diagnostic for binary operation on BoxedValues Fixes rust-lang#59458
2 parents 647d09f + 4644c3a commit fda2066

25 files changed

+184
-88
lines changed

src/librustc_typeck/check/op.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
306306
if let Some(missing_trait) = missing_trait {
307307
if op.node == hir::BinOpKind::Add &&
308308
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
309-
rhs_ty, &mut err, true) {
309+
rhs_ty, &mut err, true, op) {
310310
// This has nothing here because it means we did string
311311
// concatenation (e.g., "Hello " += "World!"). This means
312312
// we don't want the note in the else clause to be emitted
@@ -327,10 +327,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
327327
err.emit();
328328
}
329329
IsAssign::No => {
330-
let mut err = struct_span_err!(self.tcx.sess, expr.span, E0369,
330+
let mut err = struct_span_err!(self.tcx.sess, op.span, E0369,
331331
"binary operation `{}` cannot be applied to type `{}`",
332332
op.node.as_str(),
333333
lhs_ty);
334+
335+
if !lhs_expr.span.eq(&rhs_expr.span) {
336+
err.span_label(lhs_expr.span, lhs_ty.to_string());
337+
err.span_label(rhs_expr.span, rhs_ty.to_string());
338+
}
339+
334340
let mut suggested_deref = false;
335341
if let Ref(_, mut rty, _) = lhs_ty.sty {
336342
if {
@@ -380,7 +386,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
380386
if let Some(missing_trait) = missing_trait {
381387
if op.node == hir::BinOpKind::Add &&
382388
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
383-
rhs_ty, &mut err, false) {
389+
rhs_ty, &mut err, false, op) {
384390
// This has nothing here because it means we did string
385391
// concatenation (e.g., "Hello " + "World!"). This means
386392
// we don't want the note in the else clause to be emitted
@@ -418,6 +424,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
418424
rhs_ty: Ty<'tcx>,
419425
err: &mut errors::DiagnosticBuilder<'_>,
420426
is_assign: bool,
427+
op: hir::BinOp,
421428
) -> bool {
422429
let source_map = self.tcx.sess.source_map();
423430
let msg = "`to_owned()` can be used to create an owned `String` \
@@ -431,7 +438,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
431438
(&Ref(_, l_ty, _), &Ref(_, r_ty, _))
432439
if l_ty.sty == Str && r_ty.sty == Str => {
433440
if !is_assign {
434-
err.span_label(expr.span,
441+
err.span_label(op.span,
435442
"`+` can't be used to concatenate two `&str` strings");
436443
match source_map.span_to_snippet(lhs_expr.span) {
437444
Ok(lstring) => err.span_suggestion(

src/test/ui/autoderef-full-lval.stderr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
error[E0369]: binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
2-
--> $DIR/autoderef-full-lval.rs:15:20
2+
--> $DIR/autoderef-full-lval.rs:15:24
33
|
44
LL | let z: isize = a.x + b.y;
5-
| ^^^^^^^^^
5+
| --- ^ --- std::boxed::Box<isize>
6+
| |
7+
| std::boxed::Box<isize>
68
|
79
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
810

911
error[E0369]: binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
10-
--> $DIR/autoderef-full-lval.rs:21:25
12+
--> $DIR/autoderef-full-lval.rs:21:33
1113
|
1214
LL | let answer: isize = forty.a + two.a;
13-
| ^^^^^^^^^^^^^^^
15+
| ------- ^ ----- std::boxed::Box<isize>
16+
| |
17+
| std::boxed::Box<isize>
1418
|
1519
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
1620

src/test/ui/binary-op-on-double-ref.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0369]: binary operation `%` cannot be applied to type `&&{integer}`
2-
--> $DIR/binary-op-on-double-ref.rs:4:9
2+
--> $DIR/binary-op-on-double-ref.rs:4:11
33
|
44
LL | x % 2 == 0
5-
| ^^^^^
5+
| - ^ - {integer}
6+
| |
7+
| &&{integer}
68
|
79
= help: `%` can be used on '{integer}', you can dereference `x`: `*x`
810

src/test/ui/binop/binop-bitxor-str.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0369]: binary operation `^` cannot be applied to type `std::string::String`
2-
--> $DIR/binop-bitxor-str.rs:3:21
2+
--> $DIR/binop-bitxor-str.rs:3:37
33
|
44
LL | fn main() { let x = "a".to_string() ^ "b".to_string(); }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| --------------- ^ --------------- std::string::String
6+
| |
7+
| std::string::String
68
|
79
= note: an implementation of `std::ops::BitXor` might be missing for `std::string::String`
810

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0369]: binary operation `*` cannot be applied to type `bool`
2-
--> $DIR/binop-mul-bool.rs:3:21
2+
--> $DIR/binop-mul-bool.rs:3:26
33
|
44
LL | fn main() { let x = true * false; }
5-
| ^^^^^^^^^^^^
5+
| ---- ^ ----- bool
6+
| |
7+
| bool
68
|
79
= note: an implementation of `std::ops::Mul` might be missing for `bool`
810

src/test/ui/binop/binop-typeck.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0369]: binary operation `+` cannot be applied to type `bool`
2-
--> $DIR/binop-typeck.rs:6:13
2+
--> $DIR/binop-typeck.rs:6:15
33
|
44
LL | let z = x + y;
5-
| ^^^^^
5+
| - ^ - {integer}
6+
| |
7+
| bool
68
|
79
= note: an implementation of `std::ops::Add` might be missing for `bool`
810

src/test/ui/fn/fn-compare-mismatch.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0369]: binary operation `==` cannot be applied to type `fn() {main::f}`
2-
--> $DIR/fn-compare-mismatch.rs:4:13
2+
--> $DIR/fn-compare-mismatch.rs:4:15
33
|
44
LL | let x = f == g;
5-
| ^^^^^^
5+
| - ^^ - fn() {main::g}
6+
| |
7+
| fn() {main::f}
68
|
79
= note: an implementation of `std::cmp::PartialEq` might be missing for `fn() {main::f}`
810

src/test/ui/for/for-loop-type-error.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0369]: binary operation `+` cannot be applied to type `()`
2-
--> $DIR/for-loop-type-error.rs:2:13
2+
--> $DIR/for-loop-type-error.rs:2:16
33
|
44
LL | let x = () + ();
5-
| ^^^^^^^
5+
| -- ^ -- ()
6+
| |
7+
| ()
68
|
79
= note: an implementation of `std::ops::Add` might be missing for `()`
810

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0369]: binary operation `+` cannot be applied to type `std::boxed::Box<isize>`
2-
--> $DIR/issue-14915.rs:6:20
2+
--> $DIR/issue-14915.rs:6:22
33
|
44
LL | println!("{}", x + 1);
5-
| ^^^^^
5+
| - ^ - {integer}
6+
| |
7+
| std::boxed::Box<isize>
68
|
79
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
810

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ LL | 1.create_a_type_error[
55
| ^^^^^^^^^^^^^^^^^^^
66

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

0 commit comments

Comments
 (0)