Skip to content

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

Open
@kosayoda

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions