Skip to content

Commit 7b9ddbd

Browse files
committed
Show detailed expected/found types in error message when trait paths are the same
1 parent ca8078d commit 7b9ddbd

9 files changed

+87
-20
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2060,14 +2060,24 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20602060
expected: exp_found.expected.print_only_trait_path(),
20612061
found: exp_found.found.print_only_trait_path(),
20622062
};
2063-
self.expected_found_str(pretty_exp_found)
2063+
match self.expected_found_str(pretty_exp_found) {
2064+
Some((expected, found)) if expected == found => {
2065+
self.expected_found_str(exp_found)
2066+
}
2067+
ret => ret,
2068+
}
20642069
}
20652070
infer::PolyTraitRefs(exp_found) => {
20662071
let pretty_exp_found = ty::error::ExpectedFound {
20672072
expected: exp_found.expected.print_only_trait_path(),
20682073
found: exp_found.found.print_only_trait_path(),
20692074
};
2070-
self.expected_found_str(pretty_exp_found)
2075+
match self.expected_found_str(pretty_exp_found) {
2076+
Some((expected, found)) if expected == found => {
2077+
self.expected_found_str(exp_found)
2078+
}
2079+
ret => ret,
2080+
}
20712081
}
20722082
}
20732083
}

src/test/ui/error-codes/E0308-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | impl Eq for &dyn DynEq {}
55
| ^^ lifetime mismatch
66
|
7-
= note: expected trait `PartialEq`
8-
found trait `PartialEq`
7+
= note: expected trait `<&dyn DynEq as PartialEq>`
8+
found trait `<&(dyn DynEq + 'static) as PartialEq>`
99
note: the lifetime `'_` as defined on the impl at 9:13...
1010
--> $DIR/E0308-2.rs:9:13
1111
|

src/test/ui/issues/issue-20831-debruijn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ note: ...so that the types are compatible
1919
|
2020
LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) {
2121
| ^^^^^^^^^
22-
= note: expected `Publisher<'_>`
23-
found `Publisher<'_>`
22+
= note: expected `<MyStruct<'a> as Publisher<'_>>`
23+
found `<MyStruct<'_> as Publisher<'_>>`
2424

2525
error: aborting due to previous error
2626

src/test/ui/issues/issue-65230.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trait T {
2+
type U;
3+
fn f(&self) -> Self::U;
4+
}
5+
6+
struct X<'a>(&'a mut i32);
7+
8+
impl<'a> T for X<'a> {
9+
type U = &'a i32;
10+
fn f(&self) -> Self::U {
11+
self.0
12+
}
13+
//~^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'a`
14+
//
15+
// Return type of `f` has lifetime `'a` but it tries to return `self.0` which
16+
// has lifetime `'_`.
17+
}
18+
19+
fn main() {}

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

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
2+
--> $DIR/issue-65230.rs:10:28
3+
|
4+
LL | fn f(&self) -> Self::U {
5+
| ____________________________^
6+
LL | | self.0
7+
LL | | }
8+
| |_____^
9+
|
10+
note: first, the lifetime cannot outlive the anonymous lifetime defined on the method body at 10:10...
11+
--> $DIR/issue-65230.rs:10:10
12+
|
13+
LL | fn f(&self) -> Self::U {
14+
| ^^^^^
15+
note: ...so that reference does not outlive borrowed content
16+
--> $DIR/issue-65230.rs:11:9
17+
|
18+
LL | self.0
19+
| ^^^^^^
20+
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 8:6...
21+
--> $DIR/issue-65230.rs:8:6
22+
|
23+
LL | impl<'a> T for X<'a> {
24+
| ^^
25+
note: ...so that the types are compatible
26+
--> $DIR/issue-65230.rs:10:28
27+
|
28+
LL | fn f(&self) -> Self::U {
29+
| ____________________________^
30+
LL | | self.0
31+
LL | | }
32+
| |_____^
33+
= note: expected `<X<'a> as T>`
34+
found `<X<'_> as T>`
35+
36+
error: aborting due to previous error
37+
38+
For more information about this error, try `rustc --explain E0495`.

src/test/ui/nll/issue-50716.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | let _x = *s;
55
| ^^ lifetime mismatch
66
|
7-
= note: expected type `Sized`
8-
found type `Sized`
7+
= note: expected type `<<&'a T as A>::X as Sized>`
8+
found type `<<&'static T as A>::X as Sized>`
99
note: the lifetime `'a` as defined on the function body at 9:8...
1010
--> $DIR/issue-50716.rs:9:8
1111
|

src/test/ui/variance/variance-contravariant-self-trait-match.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | impls_get::<&'min G>();
55
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
66
|
7-
= note: expected type `Get`
8-
found type `Get`
7+
= note: expected type `<&'min G as Get>`
8+
found type `<&'max G as Get>`
99
note: the lifetime `'min` as defined on the function body at 10:21...
1010
--> $DIR/variance-contravariant-self-trait-match.rs:10:21
1111
|
@@ -23,8 +23,8 @@ error[E0308]: mismatched types
2323
LL | impls_get::<&'max G>();
2424
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
2525
|
26-
= note: expected type `Get`
27-
found type `Get`
26+
= note: expected type `<&'max G as Get>`
27+
found type `<&'min G as Get>`
2828
note: the lifetime `'min` as defined on the function body at 16:21...
2929
--> $DIR/variance-contravariant-self-trait-match.rs:16:21
3030
|

src/test/ui/variance/variance-covariant-self-trait-match.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | impls_get::<&'min G>();
55
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
66
|
7-
= note: expected type `Get`
8-
found type `Get`
7+
= note: expected type `<&'min G as Get>`
8+
found type `<&'max G as Get>`
99
note: the lifetime `'min` as defined on the function body at 10:21...
1010
--> $DIR/variance-covariant-self-trait-match.rs:10:21
1111
|
@@ -23,8 +23,8 @@ error[E0308]: mismatched types
2323
LL | impls_get::<&'max G>();
2424
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
2525
|
26-
= note: expected type `Get`
27-
found type `Get`
26+
= note: expected type `<&'max G as Get>`
27+
found type `<&'min G as Get>`
2828
note: the lifetime `'min` as defined on the function body at 17:21...
2929
--> $DIR/variance-covariant-self-trait-match.rs:17:21
3030
|

src/test/ui/variance/variance-invariant-self-trait-match.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0308]: mismatched types
44
LL | impls_get::<&'min G>();
55
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
66
|
7-
= note: expected type `Get`
8-
found type `Get`
7+
= note: expected type `<&'min G as Get>`
8+
found type `<&'max G as Get>`
99
note: the lifetime `'min` as defined on the function body at 7:21...
1010
--> $DIR/variance-invariant-self-trait-match.rs:7:21
1111
|
@@ -23,8 +23,8 @@ error[E0308]: mismatched types
2323
LL | impls_get::<&'max G>();
2424
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
2525
|
26-
= note: expected type `Get`
27-
found type `Get`
26+
= note: expected type `<&'max G as Get>`
27+
found type `<&'min G as Get>`
2828
note: the lifetime `'min` as defined on the function body at 13:21...
2929
--> $DIR/variance-invariant-self-trait-match.rs:13:21
3030
|

0 commit comments

Comments
 (0)