rust-lang / rust-lang/rust

Confusing E0308 (one type is more general) with GATs points to call site instead of the mismatched impl block

Open
#153,971 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When a type implements a trait with a Generic Associated Type (GAT) incorrectly (e.g., omitting a required reference), passing that type to a function with a Higher-Rank Trait Bound (for<'a>) generates a confusing E0308 error that does not mention the location of the actual mistake in the impl block.

Playground

Code
pub trait PipelineSink {
    type InputItem<'a>;
}

pub struct DynamicPipelineBuilder<OutputItem>(std::marker::PhantomData<OutputItem>);

impl<OutputItem> DynamicPipelineBuilder<OutputItem> {
    pub fn new() -> Self { Self(std::marker::PhantomData) }
    pub fn sink(self, _pipeline_sink: impl for<'a> PipelineSink<InputItem<'a> = &'a OutputItem>) {}
}

pub struct PrintingSink<O>(std::marker::PhantomData<O>);

impl<O> PrintingSink<O> {
    pub fn new() -> Self { Self(std::marker::PhantomData) }
}

impl<O: 'static> PipelineSink for PrintingSink<O> {
    // BUG: Missing reference. Should be `&'a O`
    // Error should mention this.
    type InputItem<'a> = O;
}

fn main() {
    let pipeline_builder = DynamicPipelineBuilder::<usize>::new();
    let sink = PrintingSink::new();
    
    // ERROR points here
    let _dyn_pipeline = pipeline_builder.sink(sink);
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/main.rs:43:25
   |
43 |     let _dyn_pipeline = pipeline_builder.sink(sink);
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected reference `&_`
              found reference `&'a _`
note: the lifetime requirement is introduced here
  --> src/main.rs:16:65
   |
16 |     pub fn sink(self, _pipeline_sink: impl for<'a> PipelineSink<InputItem<'a> = &'a OutputItem>) {}
   |                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
error[E0271]: type mismatch resolving `<PrintingSink<usize> as PipelineSink>::InputItem<'a> == &'a usize`
  --> src/main.rs:43:43
   |
43 |     let _dyn_pipeline = pipeline_builder.sink(sink);
   |                                          ^^^^ expected `&usize`, found `usize`
   |
note: the associated type is defined here
  --> src/main.rs:35:5
   |
35 |     type InputItem<'a> = O;
   |     ^^^^^^^^^^^^^^^^^^^^^^^ resolves to `usize`
   |
note: required by a bound in `DynamicPipelineBuilder::<OutputItem>::sink`
  --> src/main.rs:16:65
   |
16 |     pub fn sink(self, _pipeline_sink: impl for<'a> PipelineSink<InputItem<'a> = &'a OutputItem>) {}
   |                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound
   |
help: consider updating the associated type to match the expected reference type
   |
35 |     type InputItem<'a> = &'a O;
   |                          +++
Rationale and extra context

Suggesting adding a reference like that would be super cool if possible.
Don't know if the desired output is correct/achievable.
I just made something up that would have helped me.
At least mentioning type InputItem<'a> = O; in the error would already help a ton since that is where the issue arises.

Other cases

Rust Version
rustc 1.94.0 (4a4ef493e 2026-03-02)
binary: rustc
commit-hash: 4a4ef493e3a1488c6e321570238084b38948f6db
commit-date: 2026-03-02
host: x86_64-unknown-linux-gnu
release: 1.94.0
LLVM version: 21.1.8
Anything else?

No response

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

Reproduce the provided GAT and higher-ranked trait bound example in the linked Rust Playground and inspect how rustc constructs the E0308 diagnostic. Trace the diagnostic from the call site toward the mismatched associated type in the impl block. Done means the error identifies or points to type InputItem<'a> = O;, while retaining the relevant bound information.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.