Skip to content

Error E0301 "the parameter type T may not live long enough" could explain where the lifetime requirement comes from #33652

Open
@bluss

Description

@bluss

Error E0301 "the parameter type T may not live long enough" could explain where the lifetime requirement comes from.

Here's some example code where the T: 'static requirement comes implicitly from a Box<Trait> trait object (so 'static is invisible).

playground

trait Funny { }

fn new_funny_handle<T: Funny + Default>() -> Box<Funny> {
    Box::new(T::default())
}
error: the parameter type `T` may not live long enough [--explain E0310]
 --> <anon>:5:5
5 |>     Box::new(T::default())
  |>     ^^^^^^^^^^^^^^^^^^^^^^
help: consider adding an explicit lifetime bound `T: 'static`...
note: ...so that the type `T` will meet its required lifetime bounds
 --> <anon>:5:5
5 |>     Box::new(T::default())
  |>   

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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