rust-lang / rust-lang/rust

Implementation of trait function returning GAT can't use same signature as trait definition

Open
#155,430 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-GATs A-higher-ranked A-impl-trait A-lifetimes A-trait-system C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Edit: I misinterpreted the error message, but there is still an inconsistency between trait definitions and implementations when GATs are involved; see this comment and this comment for examples and discussion.


Original title: GAT with where Self: 'a bound resolves to wrong type in trait function that converts between two associated types

I tried to compile this code:

trait Trait {
    type X;
    type Y<'a> where Self: 'a;
    fn x_to_y(x: &Self::X) -> Self::Y<'_>;
}

impl<'c> Trait for &'c u8 {
    type X = u8;
    type Y<'a> = &'a u8 where Self: 'a;
    fn x_to_y<'b>(x: &'b Self::X) -> Self::Y<'b> {
        x
    }
}

I expected the code to compile, but instead, rustc generates the following error:

error[E0477]: the type `&'c u8` does not fulfill the required lifetime
  --> lib.rs:10:38
   |
10 |     fn x_to_y<'b>(x: &'b Self::X) -> Self::Y<'b> {
   |                                      ^^^^^^^^^^^
   |
note: type must outlive the lifetime `'b` as defined here as required by this binding
  --> lib.rs:10:15
   |
10 |     fn x_to_y<'b>(x: &'b Self::X) -> Self::Y<'b> {
   |               ^^

The error message suggests that Self::Y<'b> resolved to &'c u8. But this is incorrect: it should resolve to &'b u8. In fact, if I replace Self::Y<'b> with &'b u8, it compiles:

trait Trait {
    type X;
    type Y<'a> where Self: 'a;
    fn x_to_y(x: &Self::X) -> Self::Y<'_>;
}

impl<'c> Trait for &'c u8 {
    type X = u8;
    type Y<'a> = &'a u8 where Self: 'a;
    //fn x_to_y<'b>(x: &'b Self::X) -> Self::Y<'b> {
    fn x_to_y<'b>(x: &'b Self::X) -> &'b u8 { // now it compiles
        x
    }
}
Meta

rustc --version --verbose:

rustc 1.97.0-nightly (7af3402cd 2026-04-16)
binary: rustc
commit-hash: 7af3402cda75aaead39f72516fd6cbb2f3ee0dbd
commit-date: 2026-04-16
host: powerpc64le-unknown-linux-gnu
release: 1.97.0-nightly
LLVM version: 22.1.2

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 reproducer in lib.rs and run it using the reported nightly rustc version. Compare the trait definition and implementation signatures involving the GAT and lifetimes, then review the linked comments for the current inconsistency. Done means the implementation can use the corresponding signature consistently with the trait definition.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.