rust-lang / rust-lang/rust

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

Open
#121,670 1 comment 1 reaction 1 assignee View on GitHub

@dingxiangfei2009 is already working on this.

Since Feb 27, 2024.

A-associated-items A-trait-system C-bug S-blocked T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.