Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bluss opened this issue May 15, 2016 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bluss
Copy link
Member

bluss commented May 15, 2016

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())
  |>   
@alexcrichton
Copy link
Member

cc @jonathandturner

@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label May 18, 2016
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@estebank
Copy link
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())
  |     ^^^^^^^^^^^^^^^^^^^^^^

@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

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> {
  |                                        +++++++++

@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Aug 3, 2023
@arvidfm
Copy link

arvidfm commented 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:

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 {

estebank added a commit to estebank/rust that referenced this issue Aug 14, 2024
Given `trait Any: 'static` and a `struct` with a `Box<dyn Any + 'a>` field, point at the `'static` bound in `Any` to explain why `'a: 'static`.

```
error[E0478]: lifetime bound not satisfied
   --> f202.rs:2:12
    |
2   |     value: Box<dyn std::any::Any + 'a>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
   --> f202.rs:1:14
    |
1   | struct Hello<'a> {
    |              ^^
note: but lifetime parameter must outlive the static lifetime
   --> /home/gh-estebank/rust/library/core/src/any.rs:113:16
    |
113 | pub trait Any: 'static {
    |                ^^^^^^^
```

Partially address rust-lang#33652.
estebank added a commit to estebank/rust that referenced this issue Aug 21, 2024
Given `trait Any: 'static` and a `struct` with a `Box<dyn Any + 'a>` field, point at the `'static` bound in `Any` to explain why `'a: 'static`.

```
error[E0478]: lifetime bound not satisfied
   --> f202.rs:2:12
    |
2   |     value: Box<dyn std::any::Any + 'a>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
   --> f202.rs:1:14
    |
1   | struct Hello<'a> {
    |              ^^
note: but lifetime parameter must outlive the static lifetime
   --> /home/gh-estebank/rust/library/core/src/any.rs:113:16
    |
113 | pub trait Any: 'static {
    |                ^^^^^^^
```

Partially address rust-lang#33652.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 21, 2024
Point at explicit `'static` obligations on a trait

Given `trait Any: 'static` and a `struct` with a `Box<dyn Any + 'a>` field, point at the `'static` bound in `Any` to explain why `'a: 'static`.

```
error[E0478]: lifetime bound not satisfied
   --> f202.rs:2:12
    |
2   |     value: Box<dyn std::any::Any + 'a>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
   --> f202.rs:1:14
    |
1   | struct Hello<'a> {
    |              ^^
note: but lifetime parameter must outlive the static lifetime
   --> /home/gh-estebank/rust/library/core/src/any.rs:113:16
    |
113 | pub trait Any: 'static {
    |                ^^^^^^^
```

Partially address rust-lang#33652.
jieyouxu added a commit to jieyouxu/rust that referenced this issue Aug 22, 2024
Point at explicit `'static` obligations on a trait

Given `trait Any: 'static` and a `struct` with a `Box<dyn Any + 'a>` field, point at the `'static` bound in `Any` to explain why `'a: 'static`.

```
error[E0478]: lifetime bound not satisfied
   --> f202.rs:2:12
    |
2   |     value: Box<dyn std::any::Any + 'a>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
   --> f202.rs:1:14
    |
1   | struct Hello<'a> {
    |              ^^
note: but lifetime parameter must outlive the static lifetime
   --> /home/gh-estebank/rust/library/core/src/any.rs:113:16
    |
113 | pub trait Any: 'static {
    |                ^^^^^^^
```

Partially address rust-lang#33652.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 22, 2024
Rollup merge of rust-lang#129070 - estebank:static-trait, r=davidtwco

Point at explicit `'static` obligations on a trait

Given `trait Any: 'static` and a `struct` with a `Box<dyn Any + 'a>` field, point at the `'static` bound in `Any` to explain why `'a: 'static`.

```
error[E0478]: lifetime bound not satisfied
   --> f202.rs:2:12
    |
2   |     value: Box<dyn std::any::Any + 'a>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
   --> f202.rs:1:14
    |
1   | struct Hello<'a> {
    |              ^^
note: but lifetime parameter must outlive the static lifetime
   --> /home/gh-estebank/rust/library/core/src/any.rs:113:16
    |
113 | pub trait Any: 'static {
    |                ^^^^^^^
```

Partially address rust-lang#33652.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants