Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

associated-types-invalid-trait-ref diagnostics can be misleading #106876

Open
matthiaskrgr opened this issue Jan 14, 2023 · 0 comments
Open

associated-types-invalid-trait-ref diagnostics can be misleading #106876

matthiaskrgr opened this issue Jan 14, 2023 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Jan 14, 2023

Code

code from ./ui/associated-types/associated-types-invalid-trait-ref-issue-18865.rs

trait Foo<T> {
    type Bar;
    fn get_bar(&self) -> Self::Bar;
}

fn f<T:Foo<isize>>(t: &T) {
    let u: <T as Foo<usize>>::Bar = t.get_bar();
    //~^ ERROR the trait bound `T: Foo<usize>` is not satisfied
}

fn main() { }

The solution to the error is to use Foo<isize> for u instead of usize but rustc totally misses that it looks like:

error[E0277]: the trait bound `T: Foo<usize>` is not satisfied
 --> src/lib.rs:7:12
  |
7 |     let u: <T as Foo<usize>>::Bar = t.get_bar();
  |            ^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo<usize>` is not implemented for `T`
  |
help: consider further restricting this bound
  |
6 | fn f<T:Foo<isize> + Foo<usize>>(t: &T) {
  |                   ++++++++++++

For more information about this error, try `rustc --explain E0277`

applied:

trait Foo<T> {
    type Bar;
    fn get_bar(&self) -> Self::Bar;
}

fn f<T:Foo<isize> + Foo<usize>>(t: &T) {
    let u: <T as Foo<usize>>::Bar = t.get_bar();
}

fn main() { }

=> new error:

error[E0284]: type annotations needed
 --> src/lib.rs:7:39
  |
7 |     let u: <T as Foo<usize>>::Bar = t.get_bar();
  |                                       ^^^^^^^
  |
  = note: cannot satisfy `<T as Foo<_>>::Bar == <T as Foo<usize>>::Bar`
help: try using a fully qualified path to specify the expected types
  |
7 |     let u: <T as Foo<usize>>::Bar = <T as Foo<T>>::get_bar(t);
  |                                     +++++++++++++++++++++++ ~

=> applied

trait Foo<T> {
    type Bar;
    fn get_bar(&self) -> Self::Bar;
}

fn f<T:Foo<isize> + Foo<usize>>(t: &T) {
    let u: <T as Foo<usize>>::Bar = <T as Foo<T>>::get_bar(t);
}

fn main() { }

=> new error (duplicate?)

error[E0277]: the trait bound `T: Foo<T>` is not satisfied
 --> src/lib.rs:7:60
  |
7 |     let u: <T as Foo<usize>>::Bar = <T as Foo<T>>::get_bar(t);
  |                                     ---------------------- ^ the trait `Foo<T>` is not implemented for `T`
  |                                     |
  |                                     required by a bound introduced by this call
  |
help: consider further restricting this bound
  |
6 | fn f<T:Foo<isize> + Foo<usize> + Foo<T>>(t: &T) {
  |                                ++++++++

error[E0277]: the trait bound `T: Foo<T>` is not satisfied
 --> src/lib.rs:7:37
  |
7 |     let u: <T as Foo<usize>>::Bar = <T as Foo<T>>::get_bar(t);
  |                                     ^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo<T>` is not implemented for `T`
  |
help: consider further restricting this bound
  |
6 | fn f<T:Foo<isize> + Foo<usize> + Foo<T>>(t: &T) {
  |                                ++++++++

For more information about this error, try `rustc --explain E0277`.

suggestion applied:

trait Foo<T> {
    type Bar;
    fn get_bar(&self) -> Self::Bar;
}

fn f<T:Foo<isize> + Foo<usize> + Foo<T>>(t: &T) {
    let u: <T as Foo<usize>>::Bar = <T as Foo<T>>::get_bar(t);
}

fn main() { }

final error WHICH ACTUALLY HINTS TO THE ACTUAL PROBLEM :D

error[E0308]: mismatched types
 --> src/lib.rs:7:37
  |
6 | fn f<T:Foo<isize> + Foo<usize> + Foo<T>>(t: &T) {
  |      - this type parameter
7 |     let u: <T as Foo<usize>>::Bar = <T as Foo<T>>::get_bar(t);
  |            ----------------------   ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found type parameter `T`
  |            |
  |            expected due to this
  |
  = note: expected associated type `<T as Foo<usize>>::Bar`
             found associated type `<T as Foo<T>>::Bar`
@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant