Skip to content

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

Closed

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())
  |>   

Activity

  1. alexcrichton commented on May 16, 2016

    @alexcrichton
    Member
  2. added
    A-diagnostics
    on May 18, 2016
  3. added
    C-enhancement
    on Jul 25, 2017
  4. estebank commented on Sep 21, 2019

    @estebank
    Contributor

    Current output:

    
    error[E0310]: the parameter type `T` may not live long enough
     --> src/lib.rs:5:5
      |
    4 | fn new_funny_handle<T: Funny + Default>() -> Box<Funny> {
      |                     -- help: consider adding an explicit lifetime bound `T: 'static`...
    5 |     Box::new(T::default())
      |     ^^^^^^^^^^^^^^^^^^^^^^
      |
    note: ...so that the type `T` will meet its required lifetime bounds
     --> src/lib.rs:5:5
      |
    5 |     Box::new(T::default())
      |     ^^^^^^^^^^^^^^^^^^^^^^
    
  5. added
    T-compiler
    on Jun 11, 2020
  6. estebank commented on Aug 3, 2023

    @estebank
    Contributor

    Current output:

    error[E0310]: the parameter type `T` may not live long enough
     --> src/lib.rs:4:5
      |
    4 |     Box::new(T::default())
      |     ^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
      |
    help: consider adding an explicit lifetime bound...
      |
    3 | fn new_funny_handle<T: Funny + Default + 'static>() -> Box<dyn Funny> {
      |                                        +++++++++
    
  7. added
    D-confusing
    D-papercut
    on Aug 3, 2023
  8. ecryth commented on Aug 11, 2024

    @ecryth

    I found this one very confusing when I was first playing around with trait objects. I'd attempted something along the lines of:

    struct Hello<'a> {
        value: Box<dyn Any + 'a>,
    }
    
    impl<'a> Hello<'a> {
        fn new<T: 'a>(value: T) -> Self {
            Self { value: Box::new(value) }
        }
    }

    with the resulting error:

    error[E0310]: the parameter type `T` may not live long enough
      --> src/lib.rs:88:27
       |
    88 |             Self { value: Box::new(value) }
       |                           ^^^^^^^^^^^^^^^
       |                           |
       |                           the parameter type `T` must be valid for the static lifetime...
       |                           ...so that the type `T` will meet its required lifetime bounds
       |
    help: consider adding an explicit lifetime bound
       |
    87 |         fn new<T: 'a + 'static>(value: T) -> Self {
       |                      +++++++++
    

    which didn't help me understand at all why the bound was needed. Is it (1) Box<T> requiring T: 'static, (2) Any not being compatible with non-'static types, or (3) something inherent to trait objects themselves, requiring that the underlying type is 'static?

    Of course the answer is (2), in that Any is only implemented for 'static types, but not being familiar with this kind of bound at the time I was sent on a wild goose chase trying to read up on trait objects that could have been avoided if the compiler had just pointed me at the following line:

    pub trait Any: 'static {
  9. added a commit that references this issue on Aug 21, 2024
  10. added a commit that references this issue on Aug 22, 2024
  11. added a commit that references this issue on Aug 22, 2024
  12. estebank commented on Nov 11, 2025

    @estebank
    Contributor

    Current output after #148506:

    error[E0310]: the parameter type `T` may not live long enough
     --> f19.rs:4:5
      |
    3 | fn new_funny_handle<T: Funny + Default>() -> Box<dyn Funny> {
      |                                                  --------- this `dyn Trait` has an implicit `'static` lifetime bound
    4 |     Box::new(T::default())
      |     ^^^^^^^^^^^^^^^^^^^^^^
      |     |
      |     the parameter type `T` must be valid for the static lifetime...
      |     ...so that the type `T` will meet its required lifetime bounds
      |
    help: consider adding an explicit lifetime bound
      |
    3 | fn new_funny_handle<T: Funny + Default + 'static>() -> Box<dyn Funny> {
      |                                        +++++++++
    
    error[E0478]: lifetime bound not satisfied
       --> f19.rs:2:12
        |
      2 |     value: Box<dyn std::any::Any + 'a>,
        |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
    note: lifetime parameter instantiated with the lifetime `'a` as defined here
       --> f19.rs:1:14
        |
      1 | struct Hello<'a> {
        |              ^^
    note: but lifetime parameter must outlive the static lifetime
       --> /home/gh-estebank/workspace/rust/library/core/src/any.rs:113:16
        |
    113 | pub trait Any: 'static {
        |                ^^^^^^^
    
    error[E0310]: the parameter type `T` may not live long enough
     --> f19.rs:7:23
      |
    7 |         Self { value: Box::new(value) }
      |                       ^^^^^^^^^^^^^^^
      |                       |
      |                       the parameter type `T` must be valid for the static lifetime...
      |                       ...so that the type `T` will meet its required lifetime bounds
      |
    help: consider adding an explicit lifetime bound
      |
    6 |     fn new<T: 'a + 'static>(value: T) -> Self {
      |                  +++++++++
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsC-enhancementD-confusingD-papercutT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions