Skip to content

Commit 015acc2

Browse files
Provide RHS type hint when reporting operator error
1 parent 3d80dd9 commit 015acc2

20 files changed

+137
-58
lines changed

compiler/rustc_hir_typeck/src/op.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
776776
Ok(method)
777777
}
778778
None => {
779+
// This path may do some inference, so make sure we've really
780+
// doomed compilation so as to not accidentally stabilize new
781+
// inference or something here...
782+
self.tcx.sess.delay_span_bug(span, "this path really should be doomed...");
783+
// Guide inference for the RHS expression if it's provided --
784+
// this will allow us to better error reporting, at the expense
785+
// of making some error messages a bit more specific.
786+
if let Some((rhs_expr, rhs_ty)) = opt_rhs
787+
&& rhs_ty.is_ty_var()
788+
{
789+
self.check_expr_coercible_to_type(rhs_expr, rhs_ty, None);
790+
}
791+
779792
let (obligation, _) =
780793
self.obligation_for_method(cause, trait_did, lhs_ty, Some(input_types));
781794
// FIXME: This should potentially just add the obligation to the `FnCtxt`

tests/ui/binop/eq-arr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
struct X;
3+
//~^ HELP consider annotating `X` with `#[derive(PartialEq)]`
4+
let xs = [X, X, X];
5+
let eq = xs == [X, X, X];
6+
//~^ ERROR binary operation `==` cannot be applied to type `[X; 3]`
7+
}

tests/ui/binop/eq-arr.stderr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0369]: binary operation `==` cannot be applied to type `[X; 3]`
2+
--> $DIR/eq-arr.rs:5:17
3+
|
4+
LL | let eq = xs == [X, X, X];
5+
| -- ^^ --------- [X; 3]
6+
| |
7+
| [X; 3]
8+
|
9+
note: an implementation of `PartialEq` might be missing for `X`
10+
--> $DIR/eq-arr.rs:2:5
11+
|
12+
LL | struct X;
13+
| ^^^^^^^^ must implement `PartialEq`
14+
help: consider annotating `X` with `#[derive(PartialEq)]`
15+
|
16+
LL + #[derive(PartialEq)]
17+
LL | struct X;
18+
|
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0369`.

tests/ui/binop/eq-vec.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
#[derive(Debug)]
3+
enum Foo {
4+
//~^ HELP consider annotating `Foo` with `#[derive(PartialEq)]`
5+
Bar,
6+
Qux,
7+
}
8+
9+
let vec1 = vec![Foo::Bar, Foo::Qux];
10+
let vec2 = vec![Foo::Bar, Foo::Qux];
11+
assert_eq!(vec1, vec2);
12+
//~^ ERROR binary operation `==` cannot be applied to type `Vec<Foo>`
13+
}

tests/ui/binop/eq-vec.stderr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0369]: binary operation `==` cannot be applied to type `Vec<Foo>`
2+
--> $DIR/eq-vec.rs:11:5
3+
|
4+
LL | assert_eq!(vec1, vec2);
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| Vec<Foo>
8+
| Vec<Foo>
9+
|
10+
note: an implementation of `PartialEq` might be missing for `Foo`
11+
--> $DIR/eq-vec.rs:3:5
12+
|
13+
LL | enum Foo {
14+
| ^^^^^^^^ must implement `PartialEq`
15+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: consider annotating `Foo` with `#[derive(PartialEq)]`
17+
|
18+
LL + #[derive(PartialEq)]
19+
LL | enum Foo {
20+
|
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0369`.

tests/ui/binop/issue-28837.stderr

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | a + a;
66
| |
77
| A
88
|
9-
note: an implementation of `Add<_>` might be missing for `A`
9+
note: an implementation of `Add` might be missing for `A`
1010
--> $DIR/issue-28837.rs:1:1
1111
|
1212
LL | struct A;
13-
| ^^^^^^^^ must implement `Add<_>`
13+
| ^^^^^^^^ must implement `Add`
1414
note: the trait `Add` must be implemented
1515
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
1616

@@ -22,11 +22,11 @@ LL | a - a;
2222
| |
2323
| A
2424
|
25-
note: an implementation of `Sub<_>` might be missing for `A`
25+
note: an implementation of `Sub` might be missing for `A`
2626
--> $DIR/issue-28837.rs:1:1
2727
|
2828
LL | struct A;
29-
| ^^^^^^^^ must implement `Sub<_>`
29+
| ^^^^^^^^ must implement `Sub`
3030
note: the trait `Sub` must be implemented
3131
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
3232

@@ -38,11 +38,11 @@ LL | a * a;
3838
| |
3939
| A
4040
|
41-
note: an implementation of `Mul<_>` might be missing for `A`
41+
note: an implementation of `Mul` might be missing for `A`
4242
--> $DIR/issue-28837.rs:1:1
4343
|
4444
LL | struct A;
45-
| ^^^^^^^^ must implement `Mul<_>`
45+
| ^^^^^^^^ must implement `Mul`
4646
note: the trait `Mul` must be implemented
4747
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
4848

@@ -54,11 +54,11 @@ LL | a / a;
5454
| |
5555
| A
5656
|
57-
note: an implementation of `Div<_>` might be missing for `A`
57+
note: an implementation of `Div` might be missing for `A`
5858
--> $DIR/issue-28837.rs:1:1
5959
|
6060
LL | struct A;
61-
| ^^^^^^^^ must implement `Div<_>`
61+
| ^^^^^^^^ must implement `Div`
6262
note: the trait `Div` must be implemented
6363
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
6464

@@ -70,11 +70,11 @@ LL | a % a;
7070
| |
7171
| A
7272
|
73-
note: an implementation of `Rem<_>` might be missing for `A`
73+
note: an implementation of `Rem` might be missing for `A`
7474
--> $DIR/issue-28837.rs:1:1
7575
|
7676
LL | struct A;
77-
| ^^^^^^^^ must implement `Rem<_>`
77+
| ^^^^^^^^ must implement `Rem`
7878
note: the trait `Rem` must be implemented
7979
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
8080

@@ -86,11 +86,11 @@ LL | a & a;
8686
| |
8787
| A
8888
|
89-
note: an implementation of `BitAnd<_>` might be missing for `A`
89+
note: an implementation of `BitAnd` might be missing for `A`
9090
--> $DIR/issue-28837.rs:1:1
9191
|
9292
LL | struct A;
93-
| ^^^^^^^^ must implement `BitAnd<_>`
93+
| ^^^^^^^^ must implement `BitAnd`
9494
note: the trait `BitAnd` must be implemented
9595
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
9696

@@ -102,11 +102,11 @@ LL | a | a;
102102
| |
103103
| A
104104
|
105-
note: an implementation of `BitOr<_>` might be missing for `A`
105+
note: an implementation of `BitOr` might be missing for `A`
106106
--> $DIR/issue-28837.rs:1:1
107107
|
108108
LL | struct A;
109-
| ^^^^^^^^ must implement `BitOr<_>`
109+
| ^^^^^^^^ must implement `BitOr`
110110
note: the trait `BitOr` must be implemented
111111
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
112112

@@ -118,11 +118,11 @@ LL | a << a;
118118
| |
119119
| A
120120
|
121-
note: an implementation of `Shl<_>` might be missing for `A`
121+
note: an implementation of `Shl` might be missing for `A`
122122
--> $DIR/issue-28837.rs:1:1
123123
|
124124
LL | struct A;
125-
| ^^^^^^^^ must implement `Shl<_>`
125+
| ^^^^^^^^ must implement `Shl`
126126
note: the trait `Shl` must be implemented
127127
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
128128

@@ -134,11 +134,11 @@ LL | a >> a;
134134
| |
135135
| A
136136
|
137-
note: an implementation of `Shr<_>` might be missing for `A`
137+
note: an implementation of `Shr` might be missing for `A`
138138
--> $DIR/issue-28837.rs:1:1
139139
|
140140
LL | struct A;
141-
| ^^^^^^^^ must implement `Shr<_>`
141+
| ^^^^^^^^ must implement `Shr`
142142
note: the trait `Shr` must be implemented
143143
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
144144

@@ -150,11 +150,11 @@ LL | a == a;
150150
| |
151151
| A
152152
|
153-
note: an implementation of `PartialEq<_>` might be missing for `A`
153+
note: an implementation of `PartialEq` might be missing for `A`
154154
--> $DIR/issue-28837.rs:1:1
155155
|
156156
LL | struct A;
157-
| ^^^^^^^^ must implement `PartialEq<_>`
157+
| ^^^^^^^^ must implement `PartialEq`
158158
help: consider annotating `A` with `#[derive(PartialEq)]`
159159
|
160160
LL + #[derive(PartialEq)]
@@ -169,11 +169,11 @@ LL | a != a;
169169
| |
170170
| A
171171
|
172-
note: an implementation of `PartialEq<_>` might be missing for `A`
172+
note: an implementation of `PartialEq` might be missing for `A`
173173
--> $DIR/issue-28837.rs:1:1
174174
|
175175
LL | struct A;
176-
| ^^^^^^^^ must implement `PartialEq<_>`
176+
| ^^^^^^^^ must implement `PartialEq`
177177
help: consider annotating `A` with `#[derive(PartialEq)]`
178178
|
179179
LL + #[derive(PartialEq)]
@@ -188,11 +188,11 @@ LL | a < a;
188188
| |
189189
| A
190190
|
191-
note: an implementation of `PartialOrd<_>` might be missing for `A`
191+
note: an implementation of `PartialOrd` might be missing for `A`
192192
--> $DIR/issue-28837.rs:1:1
193193
|
194194
LL | struct A;
195-
| ^^^^^^^^ must implement `PartialOrd<_>`
195+
| ^^^^^^^^ must implement `PartialOrd`
196196
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
197197
|
198198
LL + #[derive(PartialEq, PartialOrd)]
@@ -207,11 +207,11 @@ LL | a <= a;
207207
| |
208208
| A
209209
|
210-
note: an implementation of `PartialOrd<_>` might be missing for `A`
210+
note: an implementation of `PartialOrd` might be missing for `A`
211211
--> $DIR/issue-28837.rs:1:1
212212
|
213213
LL | struct A;
214-
| ^^^^^^^^ must implement `PartialOrd<_>`
214+
| ^^^^^^^^ must implement `PartialOrd`
215215
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
216216
|
217217
LL + #[derive(PartialEq, PartialOrd)]
@@ -226,11 +226,11 @@ LL | a > a;
226226
| |
227227
| A
228228
|
229-
note: an implementation of `PartialOrd<_>` might be missing for `A`
229+
note: an implementation of `PartialOrd` might be missing for `A`
230230
--> $DIR/issue-28837.rs:1:1
231231
|
232232
LL | struct A;
233-
| ^^^^^^^^ must implement `PartialOrd<_>`
233+
| ^^^^^^^^ must implement `PartialOrd`
234234
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
235235
|
236236
LL + #[derive(PartialEq, PartialOrd)]
@@ -245,11 +245,11 @@ LL | a >= a;
245245
| |
246246
| A
247247
|
248-
note: an implementation of `PartialOrd<_>` might be missing for `A`
248+
note: an implementation of `PartialOrd` might be missing for `A`
249249
--> $DIR/issue-28837.rs:1:1
250250
|
251251
LL | struct A;
252-
| ^^^^^^^^ must implement `PartialOrd<_>`
252+
| ^^^^^^^^ must implement `PartialOrd`
253253
help: consider annotating `A` with `#[derive(PartialEq, PartialOrd)]`
254254
|
255255
LL + #[derive(PartialEq, PartialOrd)]

tests/ui/binop/issue-3820.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | let w = u * 3;
66
| |
77
| Thing
88
|
9-
note: an implementation of `Mul<_>` might be missing for `Thing`
9+
note: an implementation of `Mul<{integer}>` might be missing for `Thing`
1010
--> $DIR/issue-3820.rs:1:1
1111
|
1212
LL | struct Thing {
13-
| ^^^^^^^^^^^^ must implement `Mul<_>`
13+
| ^^^^^^^^^^^^ must implement `Mul<{integer}>`
1414
note: the trait `Mul` must be implemented
1515
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
1616

tests/ui/derives/derives-span-PartialEq-enum-struct-variant.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | #[derive(PartialEq)]
77
LL | x: Error
88
| ^^^^^^^^
99
|
10-
note: an implementation of `PartialEq<_>` might be missing for `Error`
10+
note: an implementation of `PartialEq` might be missing for `Error`
1111
--> $DIR/derives-span-PartialEq-enum-struct-variant.rs:4:1
1212
|
1313
LL | struct Error;
14-
| ^^^^^^^^^^^^ must implement `PartialEq<_>`
14+
| ^^^^^^^^^^^^ must implement `PartialEq`
1515
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
1616
help: consider annotating `Error` with `#[derive(PartialEq)]`
1717
|

tests/ui/derives/derives-span-PartialEq-enum.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | #[derive(PartialEq)]
77
LL | Error
88
| ^^^^^
99
|
10-
note: an implementation of `PartialEq<_>` might be missing for `Error`
10+
note: an implementation of `PartialEq` might be missing for `Error`
1111
--> $DIR/derives-span-PartialEq-enum.rs:4:1
1212
|
1313
LL | struct Error;
14-
| ^^^^^^^^^^^^ must implement `PartialEq<_>`
14+
| ^^^^^^^^^^^^ must implement `PartialEq`
1515
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
1616
help: consider annotating `Error` with `#[derive(PartialEq)]`
1717
|

tests/ui/derives/derives-span-PartialEq-struct.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | struct Struct {
77
LL | x: Error
88
| ^^^^^^^^
99
|
10-
note: an implementation of `PartialEq<_>` might be missing for `Error`
10+
note: an implementation of `PartialEq` might be missing for `Error`
1111
--> $DIR/derives-span-PartialEq-struct.rs:4:1
1212
|
1313
LL | struct Error;
14-
| ^^^^^^^^^^^^ must implement `PartialEq<_>`
14+
| ^^^^^^^^^^^^ must implement `PartialEq`
1515
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
1616
help: consider annotating `Error` with `#[derive(PartialEq)]`
1717
|

tests/ui/derives/derives-span-PartialEq-tuple-struct.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | struct Struct(
77
LL | Error
88
| ^^^^^
99
|
10-
note: an implementation of `PartialEq<_>` might be missing for `Error`
10+
note: an implementation of `PartialEq` might be missing for `Error`
1111
--> $DIR/derives-span-PartialEq-tuple-struct.rs:4:1
1212
|
1313
LL | struct Error;
14-
| ^^^^^^^^^^^^ must implement `PartialEq<_>`
14+
| ^^^^^^^^^^^^ must implement `PartialEq`
1515
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
1616
help: consider annotating `Error` with `#[derive(PartialEq)]`
1717
|

tests/ui/derives/deriving-no-inner-impl-error-message.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | struct E {
77
LL | x: NoCloneOrEq
88
| ^^^^^^^^^^^^^^
99
|
10-
note: an implementation of `PartialEq<_>` might be missing for `NoCloneOrEq`
10+
note: an implementation of `PartialEq` might be missing for `NoCloneOrEq`
1111
--> $DIR/deriving-no-inner-impl-error-message.rs:1:1
1212
|
1313
LL | struct NoCloneOrEq;
14-
| ^^^^^^^^^^^^^^^^^^ must implement `PartialEq<_>`
14+
| ^^^^^^^^^^^^^^^^^^ must implement `PartialEq`
1515
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
1616
help: consider annotating `NoCloneOrEq` with `#[derive(PartialEq)]`
1717
|

tests/ui/destructuring-assignment/note-unsupported.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ LL | S { x: a, y: b } += s;
4444
| |
4545
| cannot use `+=` on type `S`
4646
|
47-
note: an implementation of `AddAssign<_>` might be missing for `S`
47+
note: an implementation of `AddAssign` might be missing for `S`
4848
--> $DIR/note-unsupported.rs:1:1
4949
|
5050
LL | struct S { x: u8, y: u8 }
51-
| ^^^^^^^^ must implement `AddAssign<_>`
51+
| ^^^^^^^^ must implement `AddAssign`
5252
note: the trait `AddAssign` must be implemented
5353
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
5454

0 commit comments

Comments
 (0)