Skip to content

Commit fda2066

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

25 files changed

+184
-88
lines changed

src/librustc_typeck/check/op.rs

+11-4
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

+8-4
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

+4-2
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

+4-2
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

+4-2
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

+4-2
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

+4-2
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

+4-2
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

+4-2
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

+4-2
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

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

+60-30
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,150 @@
11
error[E0369]: binary operation `+` cannot be applied to type `A`
2-
--> $DIR/issue-28837.rs:6:5
2+
--> $DIR/issue-28837.rs:6:7
33
|
44
LL | a + a;
5-
| ^^^^^
5+
| - ^ - A
6+
| |
7+
| A
68
|
79
= note: an implementation of `std::ops::Add` might be missing for `A`
810

911
error[E0369]: binary operation `-` cannot be applied to type `A`
10-
--> $DIR/issue-28837.rs:8:5
12+
--> $DIR/issue-28837.rs:8:7
1113
|
1214
LL | a - a;
13-
| ^^^^^
15+
| - ^ - A
16+
| |
17+
| A
1418
|
1519
= note: an implementation of `std::ops::Sub` might be missing for `A`
1620

1721
error[E0369]: binary operation `*` cannot be applied to type `A`
18-
--> $DIR/issue-28837.rs:10:5
22+
--> $DIR/issue-28837.rs:10:7
1923
|
2024
LL | a * a;
21-
| ^^^^^
25+
| - ^ - A
26+
| |
27+
| A
2228
|
2329
= note: an implementation of `std::ops::Mul` might be missing for `A`
2430

2531
error[E0369]: binary operation `/` cannot be applied to type `A`
26-
--> $DIR/issue-28837.rs:12:5
32+
--> $DIR/issue-28837.rs:12:7
2733
|
2834
LL | a / a;
29-
| ^^^^^
35+
| - ^ - A
36+
| |
37+
| A
3038
|
3139
= note: an implementation of `std::ops::Div` might be missing for `A`
3240

3341
error[E0369]: binary operation `%` cannot be applied to type `A`
34-
--> $DIR/issue-28837.rs:14:5
42+
--> $DIR/issue-28837.rs:14:7
3543
|
3644
LL | a % a;
37-
| ^^^^^
45+
| - ^ - A
46+
| |
47+
| A
3848
|
3949
= note: an implementation of `std::ops::Rem` might be missing for `A`
4050

4151
error[E0369]: binary operation `&` cannot be applied to type `A`
42-
--> $DIR/issue-28837.rs:16:5
52+
--> $DIR/issue-28837.rs:16:7
4353
|
4454
LL | a & a;
45-
| ^^^^^
55+
| - ^ - A
56+
| |
57+
| A
4658
|
4759
= note: an implementation of `std::ops::BitAnd` might be missing for `A`
4860

4961
error[E0369]: binary operation `|` cannot be applied to type `A`
50-
--> $DIR/issue-28837.rs:18:5
62+
--> $DIR/issue-28837.rs:18:7
5163
|
5264
LL | a | a;
53-
| ^^^^^
65+
| - ^ - A
66+
| |
67+
| A
5468
|
5569
= note: an implementation of `std::ops::BitOr` might be missing for `A`
5670

5771
error[E0369]: binary operation `<<` cannot be applied to type `A`
58-
--> $DIR/issue-28837.rs:20:5
72+
--> $DIR/issue-28837.rs:20:7
5973
|
6074
LL | a << a;
61-
| ^^^^^^
75+
| - ^^ - A
76+
| |
77+
| A
6278
|
6379
= note: an implementation of `std::ops::Shl` might be missing for `A`
6480

6581
error[E0369]: binary operation `>>` cannot be applied to type `A`
66-
--> $DIR/issue-28837.rs:22:5
82+
--> $DIR/issue-28837.rs:22:7
6783
|
6884
LL | a >> a;
69-
| ^^^^^^
85+
| - ^^ - A
86+
| |
87+
| A
7088
|
7189
= note: an implementation of `std::ops::Shr` might be missing for `A`
7290

7391
error[E0369]: binary operation `==` cannot be applied to type `A`
74-
--> $DIR/issue-28837.rs:24:5
92+
--> $DIR/issue-28837.rs:24:7
7593
|
7694
LL | a == a;
77-
| ^^^^^^
95+
| - ^^ - A
96+
| |
97+
| A
7898
|
7999
= note: an implementation of `std::cmp::PartialEq` might be missing for `A`
80100

81101
error[E0369]: binary operation `!=` cannot be applied to type `A`
82-
--> $DIR/issue-28837.rs:26:5
102+
--> $DIR/issue-28837.rs:26:7
83103
|
84104
LL | a != a;
85-
| ^^^^^^
105+
| - ^^ - A
106+
| |
107+
| A
86108
|
87109
= note: an implementation of `std::cmp::PartialEq` might be missing for `A`
88110

89111
error[E0369]: binary operation `<` cannot be applied to type `A`
90-
--> $DIR/issue-28837.rs:28:5
112+
--> $DIR/issue-28837.rs:28:7
91113
|
92114
LL | a < a;
93-
| ^^^^^
115+
| - ^ - A
116+
| |
117+
| A
94118
|
95119
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`
96120

97121
error[E0369]: binary operation `<=` cannot be applied to type `A`
98-
--> $DIR/issue-28837.rs:30:5
122+
--> $DIR/issue-28837.rs:30:7
99123
|
100124
LL | a <= a;
101-
| ^^^^^^
125+
| - ^^ - A
126+
| |
127+
| A
102128
|
103129
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`
104130

105131
error[E0369]: binary operation `>` cannot be applied to type `A`
106-
--> $DIR/issue-28837.rs:32:5
132+
--> $DIR/issue-28837.rs:32:7
107133
|
108134
LL | a > a;
109-
| ^^^^^
135+
| - ^ - A
136+
| |
137+
| A
110138
|
111139
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`
112140

113141
error[E0369]: binary operation `>=` cannot be applied to type `A`
114-
--> $DIR/issue-28837.rs:34:5
142+
--> $DIR/issue-28837.rs:34:7
115143
|
116144
LL | a >= a;
117-
| ^^^^^^
145+
| - ^^ - A
146+
| |
147+
| A
118148
|
119149
= note: an implementation of `std::cmp::PartialOrd` might be missing for `A`
120150

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

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

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

0 commit comments

Comments
 (0)