Show detailed expected/found types in error message when trait paths are the same

This commit is contained in:
rhysd 2021-10-08 01:00:15 +09:00
parent ca8078d7b2
commit 7b9ddbdcf2
9 changed files with 87 additions and 20 deletions

View File

@ -2060,14 +2060,24 @@ fn values_str(
expected: exp_found.expected.print_only_trait_path(), expected: exp_found.expected.print_only_trait_path(),
found: exp_found.found.print_only_trait_path(), found: exp_found.found.print_only_trait_path(),
}; };
self.expected_found_str(pretty_exp_found) match self.expected_found_str(pretty_exp_found) {
Some((expected, found)) if expected == found => {
self.expected_found_str(exp_found)
}
ret => ret,
}
} }
infer::PolyTraitRefs(exp_found) => { infer::PolyTraitRefs(exp_found) => {
let pretty_exp_found = ty::error::ExpectedFound { let pretty_exp_found = ty::error::ExpectedFound {
expected: exp_found.expected.print_only_trait_path(), expected: exp_found.expected.print_only_trait_path(),
found: exp_found.found.print_only_trait_path(), found: exp_found.found.print_only_trait_path(),
}; };
self.expected_found_str(pretty_exp_found) match self.expected_found_str(pretty_exp_found) {
Some((expected, found)) if expected == found => {
self.expected_found_str(exp_found)
}
ret => ret,
}
} }
} }
} }

View File

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

View File

@ -19,8 +19,8 @@ note: ...so that the types are compatible
| |
LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) { LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) {
| ^^^^^^^^^ | ^^^^^^^^^
= note: expected `Publisher<'_>` = note: expected `<MyStruct<'a> as Publisher<'_>>`
found `Publisher<'_>` found `<MyStruct<'_> as Publisher<'_>>`
error: aborting due to previous error error: aborting due to previous error

View File

@ -0,0 +1,19 @@
trait T {
type U;
fn f(&self) -> Self::U;
}
struct X<'a>(&'a mut i32);
impl<'a> T for X<'a> {
type U = &'a i32;
fn f(&self) -> Self::U {
self.0
}
//~^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'a`
//
// Return type of `f` has lifetime `'a` but it tries to return `self.0` which
// has lifetime `'_`.
}
fn main() {}

View File

@ -0,0 +1,38 @@
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> $DIR/issue-65230.rs:10:28
|
LL | fn f(&self) -> Self::U {
| ____________________________^
LL | | self.0
LL | | }
| |_____^
|
note: first, the lifetime cannot outlive the anonymous lifetime defined on the method body at 10:10...
--> $DIR/issue-65230.rs:10:10
|
LL | fn f(&self) -> Self::U {
| ^^^^^
note: ...so that reference does not outlive borrowed content
--> $DIR/issue-65230.rs:11:9
|
LL | self.0
| ^^^^^^
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 8:6...
--> $DIR/issue-65230.rs:8:6
|
LL | impl<'a> T for X<'a> {
| ^^
note: ...so that the types are compatible
--> $DIR/issue-65230.rs:10:28
|
LL | fn f(&self) -> Self::U {
| ____________________________^
LL | | self.0
LL | | }
| |_____^
= note: expected `<X<'a> as T>`
found `<X<'_> as T>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0495`.

View File

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

View File

@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'min G>(); LL | impls_get::<&'min G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
| |
= note: expected type `Get` = note: expected type `<&'min G as Get>`
found type `Get` found type `<&'max G as Get>`
note: the lifetime `'min` as defined on the function body at 10:21... note: the lifetime `'min` as defined on the function body at 10:21...
--> $DIR/variance-contravariant-self-trait-match.rs:10:21 --> $DIR/variance-contravariant-self-trait-match.rs:10:21
| |
@ -23,8 +23,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'max G>(); LL | impls_get::<&'max G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
| |
= note: expected type `Get` = note: expected type `<&'max G as Get>`
found type `Get` found type `<&'min G as Get>`
note: the lifetime `'min` as defined on the function body at 16:21... note: the lifetime `'min` as defined on the function body at 16:21...
--> $DIR/variance-contravariant-self-trait-match.rs:16:21 --> $DIR/variance-contravariant-self-trait-match.rs:16:21
| |

View File

@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'min G>(); LL | impls_get::<&'min G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
| |
= note: expected type `Get` = note: expected type `<&'min G as Get>`
found type `Get` found type `<&'max G as Get>`
note: the lifetime `'min` as defined on the function body at 10:21... note: the lifetime `'min` as defined on the function body at 10:21...
--> $DIR/variance-covariant-self-trait-match.rs:10:21 --> $DIR/variance-covariant-self-trait-match.rs:10:21
| |
@ -23,8 +23,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'max G>(); LL | impls_get::<&'max G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
| |
= note: expected type `Get` = note: expected type `<&'max G as Get>`
found type `Get` found type `<&'min G as Get>`
note: the lifetime `'min` as defined on the function body at 17:21... note: the lifetime `'min` as defined on the function body at 17:21...
--> $DIR/variance-covariant-self-trait-match.rs:17:21 --> $DIR/variance-covariant-self-trait-match.rs:17:21
| |

View File

@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'min G>(); LL | impls_get::<&'min G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
| |
= note: expected type `Get` = note: expected type `<&'min G as Get>`
found type `Get` found type `<&'max G as Get>`
note: the lifetime `'min` as defined on the function body at 7:21... note: the lifetime `'min` as defined on the function body at 7:21...
--> $DIR/variance-invariant-self-trait-match.rs:7:21 --> $DIR/variance-invariant-self-trait-match.rs:7:21
| |
@ -23,8 +23,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'max G>(); LL | impls_get::<&'max G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch | ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
| |
= note: expected type `Get` = note: expected type `<&'max G as Get>`
found type `Get` found type `<&'min G as Get>`
note: the lifetime `'min` as defined on the function body at 13:21... note: the lifetime `'min` as defined on the function body at 13:21...
--> $DIR/variance-invariant-self-trait-match.rs:13:21 --> $DIR/variance-invariant-self-trait-match.rs:13:21
| |