rust-lang / rust-lang/rust

GAT: Fails to compile with additional bounds

Open
#139,207 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lifetimes A-trait-system A-type-system C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

trait Gat {
    type Assoc<'a>;
}

struct Foo<S>(S);

impl<S> Foo<S> {
    fn bar<'a, C, T>(&self)
    where
        C: Gat<Assoc<'a> = T>,
        C: Gat<Assoc<'static> = S>, // By commenting out this bound code will compile
    {
        self.foo::<C, T>(); // or comment out this line, it will compile too
    }

    fn foo<'a, C, T>(&self)
    where
        C: Gat<Assoc<'a> = T>,
        // To be closer to original code `C: Gat<Assoc<'static> = S>` may be included, but error is the same anyway
    {
    }
}

I expected to see this happen: Code should compile

Instead, this happened: Code did not compile. Probably because 'a is different in both cases, I but cannot specify lifetime arguments explicitly if late bound lifetime parameters are present #42868 .

 1  error[E0284]: type annotations needed: cannot satisfy `<C as Gat>::Assoc<'_> == T`
   --> src/main.rs:13:14
    |
 13 |         self.foo::<C, T>();
    |              ^^^ cannot satisfy `<C as Gat>::Assoc<'_> == T`
    |
 note: required by a bound in `Foo::<S>::foo`
   --> src/main.rs:18:16
    |
 16 |     fn foo<'a, C, T>(&self)
    |        --- required by a bound in this associated function
 17 |     where
 18 |         C: Gat<Assoc<'a> = T>,
    |                ^^^^^^^^^^^^^ required by this bound in `Foo::<S>::foo`
Meta

rustc --version --verbose:

rustc 1.87.0-nightly (b48576b4d 2025-03-22)
binary: rustc
commit-hash: b48576b4db5a595f453891f0b7243ef75d8c0afa
commit-date: 2025-03-22
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.1

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.

Research direction

Start with the minimal reproducer in src/main.rs and run it using the reported rustc 1.87.0-nightly version. Investigate how the compiler handles the two GAT associated-type bounds and the call to Foo::foo when the late-bound lifetimes differ. Done means the example compiles as expected without the E0284 error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.