rust-lang / rust-lang/rust

Incorrect diagnostic `error[E0309]: the parameter type `T` may not live long enough`, suggests duplicate bound

Open
#133,943 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-impl-trait A-lifetimes A-trait-system D-invalid-suggestion S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use std::marker::PhantomData;

trait Unit<'t, T> {}
trait Trait {
    fn frob<'a, T>() -> impl Unit<'a, T>;
}

struct Wrapper<T>(PhantomData<T>);
impl<'a, T> Unit<'a, T> for Wrapper<T> where T: 'a {}

struct Foo;
impl Trait for Foo {
    fn frob<'a, T: 'a>() -> impl Unit<'a, T> {
        Wrapper(PhantomData)
    }
}
Current output
error[E0309]: the parameter type `T` may not live long enough
  --> src/lib.rs:13:29
   |
13 |     fn frob<'a, T: 'a>() -> impl Unit<'a, T> {
   |             --              ^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
   |             |
   |             the parameter type `T` must be valid for the lifetime `'a` as defined here...
   |
note: ...that is required by this bound
  --> src/lib.rs:13:20
   |
13 |     fn frob<'a, T: 'a>() -> impl Unit<'a, T> {
   |                    ^^
help: consider adding an explicit lifetime bound
   |
13 |     fn frob<'a, T: 'a + 'a>() -> impl Unit<'a, T> {
   |                       ++++

For more information about this error, try `rustc --explain E0309`.
error: could not compile `testing` (lib) due to 1 previous error
Additional Context

The program compiles fine if frob is a free function, or if frob is not a trait implementation.

trait Unit<'t, T> {}
trait Trait {
    fn frob<'a, T>() -> impl Unit<'a, T>;
}

struct Wrapper<T>(PhantomData<T>);
impl<'a, T> Unit<'a, T> for Wrapper<T> where T: 'a {}

// Not a trait impl
struct Foo;
impl Foo {
    fn frob<'a, T: 'a>() -> impl Unit<'a, T> {
        Wrapper(PhantomData)
    }
}

// Regular function
fn frob<'a, T: 'a>() -> impl Unit<'a, T> {
    Wrapper(PhantomData)
}
Rust Version
rustc 1.85.0-nightly (acabb5248 2024-12-04)
binary: rustc
commit-hash: acabb5248231987ae1f0c215208d1005a5db402d
commit-date: 2024-12-04
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5

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

Run the reproduction in src/lib.rs with the reported rustc nightly, then compare the trait implementation with the free-function and inherent-method variants. Trace the compiler diagnostic path for the erroneous E0309; done means the trait implementation is accepted without suggesting the duplicate lifetime bound, with regression coverage for the case.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.