rust-lang / rust-lang/rust

E0195 when method lifetime used on a GAT inside an APIT in trait is inlined and unused

Open
#147,273 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-GATs C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

trait Query<'a, 's> {}

impl<'a, 's> Query<'a, 's> for &'a i32 {}

trait Foo {
    type Query<'a, 's>: Query<'a, 's>;
    fn read<'a, 's>(arg: impl Iterator<Item = Self::Query<'a, 's>>);
}

struct Bar;

impl Foo for Bar {
    type Query<'a, 's> = &'a i32;
    fn read<'a, 's>(_arg: impl Iterator<Item = &'a i32>) {}
}

I expected to see this happen:
This should compile since the Foo for Bar impl just resolved Self::Query<'a, 's> into its real type (&'a i32).

Instead, this happened:

error[E0195]: lifetime parameters or bounds on associated function `read` do not match the trait declaration
  --> src/lib.rs:14:12
   |
 7 |     fn read<'a, 's>(arg: impl Iterator<Item = Self::Query<'a, 's>>);
   |            -------- lifetimes in impl do not match this associated function in trait
...
14 |     fn read<'a, 's>(_arg: impl Iterator<Item = &'a i32>) {}
   |            ^^^^^^^^ lifetimes do not match associated function in trait

However, if we change impl Iterator<Item = Xxx> to Xxx in both the trait and the impl, it compiles:

trait Query<'a, 's> {}

impl<'a, 's> Query<'a, 's> for &'a i32 {}

trait Foo {
    type Query<'a, 's>: Query<'a, 's>;
    fn read<'a, 's>(arg:  Self::Query<'a, 's>);
}

struct Bar;

impl Foo for Bar {
    type Query<'a, 's> = &'a i32;
    fn read<'a, 's>(_arg:  &'a i32) {}
}

Also works when the APIT is changed to a &dyn Iterator<Item = Xxx>:

trait Query<'a, 's> {}

impl<'a, 's> Query<'a, 's> for &'a i32 {}

trait Foo {
    type Query<'a, 's>: Query<'a, 's>;
    fn read<'a, 's>(arg: &dyn Iterator<Item = Self::Query<'a, 's>>);
}

struct Bar;

impl Foo for Bar {
    type Query<'a, 's> = &'a i32;
    fn read<'a, 's>(_arg: &dyn Iterator<Item = &'a i32>) {}
}
Meta

rustc --version --verbose:

1.92.0-nightly (2025-10-01 4da69dfff1929cc79872)

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 by compiling the reproducer shown for src/lib.rs with the reported nightly rustc version and compare it with the working non-APIT variants. Trace handling of E0195 for a GAT used through an associated type in an argument-position impl Trait. Done means the APIT example compiles without the lifetime mismatch while the relevant regression behavior remains covered.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.