You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error E0301 "the parameter type T may not live long enough" could explain where the lifetime requirement comes from #33652
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.
error: the parameter type `T` may not live long enough [--explain E0310]
--> <anon>:5:55 |> 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:55 |> Box::new(T::default())
|>
Reacted by David Zmick, Eduardo Pinho, Gregory Petrosyan, Daniel Salvadori, Alessandro Re and ecryth
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())
| ^^^^^^^^^^^^^^^^^^^^^^
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> {
| +++++++++
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:
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 {
| +++++++++
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.
Activity
alexcrichton commentedon May 16, 2016
cc @jonathandturner
estebank commentedon Sep 21, 2019
Current output:
estebank commentedon Aug 3, 2023
Current output:
ecryth commentedon Aug 11, 2024
I found this one very confusing when I was first playing around with trait objects. I'd attempted something along the lines of:
with the resulting error:
which didn't help me understand at all why the bound was needed. Is it (1)
Box<T>requiringT: 'static, (2)Anynot being compatible with non-'statictypes, or (3) something inherent to trait objects themselves, requiring that the underlying type is'static?Of course the answer is (2), in that
Anyis only implemented for'statictypes, 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:Point at explicit `'static` obligations on a trait
Point at explicit `'static` obligations on a trait
Rollup merge of rust-lang#129070 - estebank:static-trait, r=davidtwco
Rollup merge of rust-lang#129070 - estebank:static-trait, r=davidtwco
Unrolled build for rust-lang#129070
estebank commentedon Nov 11, 2025
Current output after #148506: