Skip to content

Confusing error caused by double projection in trait supertraits #102185

Description

@WaffleLapkin

Given the following code: (play)

fn f<T>(_: impl A<X = T>) {}

trait A: C<Z = <<Self as A>::X as B>::Y> {
    type X: B;
}

trait B {
    type Y;
}

trait C {
    type Z;
}

The current output is:

error[E0277]: the trait bound `T: B` is not satisfied
 --> src/lib.rs:1:1
  |
1 | fn f<T>(_: impl A<X = T>) {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `B` is not implemented for `T`
  |
help: consider restricting type parameter `T`
  |
1 | fn f<T: B>(_: impl A<X = T>) {}
  |       +++

error[E0277]: the trait bound `T: B` is not satisfied
 --> src/lib.rs:1:17
  |
1 | fn f<T>(_: impl A<X = T>) {}
  |                 ^^^^^^^^ the trait `B` is not implemented for `T`
  |
help: consider restricting type parameter `T`
  |
1 | fn f<T: B>(_: impl A<X = T>) {}
  |       +++

This is confusing because it seems like this error is caused by type X: B in the trait, but actually it's caused by X as B in the supertraits, if your remove X as B the code compiles (as it should in any case, IMO!).

Ideally the code would compile or at least the output should look like:

error[E0277]: the trait bound `T: B` is not satisfied
 --> src/lib.rs:1:1
  |
1 | fn f<T>(_: impl A<X = T>) {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `B` is not implemented for `T`
  |
help: consider restricting type parameter `T`
  |
1 | fn f<T: B>(_: impl A<X = T>) {}
  |       +++
note: required because of this
  |
3 | trait A: C<Z = <<Self as A>::X as B>::Y> {
  |                  ^^^^^^^^^^^^^^^^^^

@rustbot label +D-confusing

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions