rust-lang / rust-lang/rust

RPIT(IT)s "leak" internal implementation details of lifetime capturing via boundedness

Open
#140,517 14 comments 0 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #140523 by @compiler-errors — merged
A-impl-trait A-lifetimes C-bug S-has-mcve T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

trait A {
    type B<'a>;
}

struct Foo(u64);

impl A for Foo {
    type B<'a> = Bar<'a>;
}

struct Bar<'a>(&'a Foo);

trait X<Y: A> {
    fn method<'a, 'b>(&'a self, bar: Y::B<'b>) -> impl Iterator<Item=usize>;
}

struct Eric;

impl X<Foo> for Eric {
    fn method<'a, 'b>(&'a self, _bar: <Foo as A>::B<'b>) -> impl Iterator<Item=usize> {
        vec![1, 2].into_iter()
    }
}

struct Jack;

impl X<Foo> for Jack {
    fn method<'a, 'b>(&'a self, _bar: Bar<'b>) -> impl Iterator<Item=usize> {
        vec![1, 2].into_iter()
    }
}

fn main() {}

I expected to see this happen: implementation of X<Foo> on Jack compiles without error

  • As you can see, Bar<'a> and <Foo as A>::B<'a> are exactly the same type.
  • Therefore, implementation of X<Foo> on Jack and Eric should have identical behavior.
  • Implementation of X<Foo> on Eric compiles.

Instead, this happened: implementation of X<Foo> on Jack gives the following error

error[E0195]: lifetime parameters or bounds on method `method` do not match the trait declaration
  --> gat-borrow-checking-bug.rs:28:14
   |
14 |     fn method<'a, 'b>(&'a self, bar: Y::B<'b>) -> impl Iterator<Item=usize>;
   |              -------- lifetimes in impl do not match this method in trait
...
28 |     fn method<'a, 'b>(&'a self, _bar: Bar<'b>) -> impl Iterator<Item=usize> {
   |              ^^^^^^^^ lifetimes do not match method in trait

error: aborting due to 1 previous error
Meta

rustc --version --verbose:

rustc 1.85.1 (4eb161250 2025-03-15)

Using the nightly version, it still doesn't work

rust version 1.88.0-nightly (74509131e 2025-04-29)

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 inline Rust reproducer and run it using the reported stable and nightly rustc versions. Trace how the compiler compares the trait method's associated-type projection with the concrete Bar type, then verify that equivalent implementations are accepted or that the diagnostic is corrected.

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.