Skip to content

Trait bounds on associated types are not commuted to the rigid types when projection predicates are in environment #121670

Open
@dingxiangfei2009

Description

cc @compiler-errors
I tried this code:

trait Foo {
    type Bar: Qux;
}

trait Qux {
    fn hello();
}

fn doesnt_work<T: Foo<Bar = U>, U>() {
    T::Bar::hello(); // <~ `T::Bar` becomes `U`, for which we know nothing.
}

fn works<T: Foo>() {
    T::Bar::hello();
}

I expected to see this happen: this should pass type check.

Instead, this happened: since T::Bar is normalised to just U, but there is no facts about U except the projection predicate which is not used yet.

error[E0599]: no function or associated item named `hello` found for type parameter `U` in the current scope
  --> src/lib.rs:10:13
   |
9  | fn doesnt_work<U, T: Foo<Bar = U>>() {
   |                - function or associated item `hello` not found for this type parameter
10 |     T::Bar::hello();
   |             ^^^^^ function or associated item not found in `U`
   |
   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `hello`, perhaps you need to restrict type parameter `U` with it:
   |
9  | fn doesnt_work<U: Qux, T: Foo<Bar = U>>() {
   |                 +++++

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

Meta

It is known to be the case for a while including nightly and stable as of writing.

Backtrace

// Not an ICE

Activity

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

Metadata

Labels

A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.S-blockedStatus: Blocked on something else such as an RFC or other implementation work.T-typesRelevant to the types team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions