Skip to content

Commit

Permalink
Auto merge of #89633 - rhysd:issue-65230, r=petrochenkov
Browse files Browse the repository at this point in the history
Show detailed expected/found types in error message when trait paths are the same

Fixes #65230.

### Issue solved by this PR

```rust
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
    }
}

fn main() {}
```

Compiler generates the following note:

```
note: ...so that the types are compatible
  --> test.rs:10:28
   |
10 |       fn f(&self) -> Self::U {
   |  ____________________________^
11 | |         self.0
12 | |     }
   | |_____^
   = note: expected `T`
              found `T`
```

This note is not useful since the expected type and the found type are the same.

### How this PR solve the issue

When the expected type and the found type are exactly the same in string representation, the note falls back to the detailed string representation of trait ref:

```
note: ...so that the types are compatible
  --> test.rs:10:28
   |
10 |       fn f(&self) -> Self::U {
   |  ____________________________^
11 | |         self.0
12 | |     }
   | |_____^
   = note: expected `<X<'a> as T>`
              found `<X<'_> as T>`
```

So that a user can notice what was different between the expected one and the found one.
  • Loading branch information
bors committed Oct 10, 2021
2 parents 0c87288 + 9211bee commit 68dfa07
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 20 deletions.
14 changes: 12 additions & 2 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,14 +2060,24 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
expected: exp_found.expected.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) => {
let pretty_exp_found = ty::error::ExpectedFound {
expected: exp_found.expected.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,
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/error-codes/E0308-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | impl Eq for &dyn DynEq {}
| ^^ lifetime mismatch
|
= note: expected trait `PartialEq`
found trait `PartialEq`
= note: expected trait `<&dyn DynEq as PartialEq>`
found trait `<&(dyn DynEq + 'static) as PartialEq>`
note: the lifetime `'_` as defined on the impl at 9:13...
--> $DIR/E0308-2.rs:9:13
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-20831-debruijn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
| ^^^^^^^^^
= note: expected `Publisher<'_>`
found `Publisher<'_>`
= note: expected `<MyStruct<'a> as Publisher<'_>>`
found `<MyStruct<'_> as Publisher<'_>>`

error: aborting due to previous error

Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-65230.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trait T0 {}
trait T1: T0 {}

trait T2 {}

impl<'a> T0 for &'a (dyn T2 + 'static) {}

impl T1 for &dyn T2 {}
//~^ ERROR mismatched types

fn main() {}
18 changes: 18 additions & 0 deletions src/test/ui/issues/issue-65230.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0308]: mismatched types
--> $DIR/issue-65230.rs:8:6
|
LL | impl T1 for &dyn T2 {}
| ^^ lifetime mismatch
|
= note: expected trait `<&dyn T2 as T0>`
found trait `<&(dyn T2 + 'static) as T0>`
note: the lifetime `'_` as defined on the impl at 8:13...
--> $DIR/issue-65230.rs:8:13
|
LL | impl T1 for &dyn T2 {}
| ^
= note: ...does not necessarily outlive the static lifetime

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
4 changes: 2 additions & 2 deletions src/test/ui/nll/issue-50716.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | let _x = *s;
| ^^ lifetime mismatch
|
= note: expected type `Sized`
found type `Sized`
= note: expected type `<<&'a T as A>::X as Sized>`
found type `<<&'static T as A>::X as Sized>`
note: the lifetime `'a` as defined on the function body at 9:8...
--> $DIR/issue-50716.rs:9:8
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'min G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `Get`
found type `Get`
= note: expected type `<&'min G as Get>`
found type `<&'max G as Get>`
note: the lifetime `'min` as defined on the function body at 10:21...
--> $DIR/variance-contravariant-self-trait-match.rs:10:21
|
Expand All @@ -23,8 +23,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'max G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `Get`
found type `Get`
= note: expected type `<&'max G as Get>`
found type `<&'min G as Get>`
note: the lifetime `'min` as defined on the function body at 16:21...
--> $DIR/variance-contravariant-self-trait-match.rs:16:21
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'min G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `Get`
found type `Get`
= note: expected type `<&'min G as Get>`
found type `<&'max G as Get>`
note: the lifetime `'min` as defined on the function body at 10:21...
--> $DIR/variance-covariant-self-trait-match.rs:10:21
|
Expand All @@ -23,8 +23,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'max G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `Get`
found type `Get`
= note: expected type `<&'max G as Get>`
found type `<&'min G as Get>`
note: the lifetime `'min` as defined on the function body at 17:21...
--> $DIR/variance-covariant-self-trait-match.rs:17:21
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'min G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `Get`
found type `Get`
= note: expected type `<&'min G as Get>`
found type `<&'max G as Get>`
note: the lifetime `'min` as defined on the function body at 7:21...
--> $DIR/variance-invariant-self-trait-match.rs:7:21
|
Expand All @@ -23,8 +23,8 @@ error[E0308]: mismatched types
LL | impls_get::<&'max G>();
| ^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected type `Get`
found type `Get`
= note: expected type `<&'max G as Get>`
found type `<&'min G as Get>`
note: the lifetime `'min` as defined on the function body at 13:21...
--> $DIR/variance-invariant-self-trait-match.rs:13:21
|
Expand Down

0 comments on commit 68dfa07

Please sign in to comment.