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

Confusing error message when trying to use a lifetime on an Any trait object #83325

Open
asquared31415 opened this issue Mar 20, 2021 · 1 comment · May be fixed by #121274
Open

Confusing error message when trying to use a lifetime on an Any trait object #83325

asquared31415 opened this issue Mar 20, 2021 · 1 comment · May be fixed by #121274
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@asquared31415
Copy link
Contributor

The following code playground link

use std::any::Any;

struct Something<'a> {
    broken: Box<dyn Any + 'a>
}

Outputs an error message related to lifetime issues that is not clear what the issue exactly is.

error[E0478]: lifetime bound not satisfied
 --> src/lib.rs:4:13
  |
4 |     broken: Box<dyn Any + 'a>
  |             ^^^^^^^^^^^^^^^^^
  |
note: lifetime parameter instantiated with the lifetime `'a` as defined on the struct at 3:18
 --> src/lib.rs:3:18
  |
3 | struct Something<'a> {
  |                  ^^
  = note: but lifetime parameter must outlive the static lifetime

This error is caused by trait Any: 'static, however this error message is not clear as to why the lifetime needs to outlive the static lifetime. Ideally the error message would say where the requirement comes from, like other lifetime-related errors.

cc @estebank who was looking for weird and unintuitive errors. This wasn't caused by a crate specifically, but it still isn't a good error.

@asquared31415 asquared31415 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 20, 2021
@Orrimp
Copy link

Orrimp commented Mar 26, 2023

I have the same problem, and it's really hard to solve it even with Google. ChatGPT suggest:

To resolve this error, you need to either increase the lifetime of the lifetime parameter or change the static reference to a reference with a shorter lifetime. Here are some strategies you can use:

Decrease the lifetime thanks to a reference.

fn my_function<'a>(x: &'a str) -> &'a str {
    &x[0..1]
}

Increase the lifetime:

fn my_function<'a>(x: &'static str, y: &'a str) -> &'a str {
    if x == "foo" {
        &y[0..1]
    } else {
        y
    }
}

I am still new to Rust but lifetime and Arc,Box.. Is hard to get.

estebank added a commit to estebank/rust that referenced this issue Feb 21, 2024
```
error[E0478]: lifetime bound not satisfied
   --> tests/ui/lifetimes/point-at-lifetime-obligation-from-trait-in-trait-object.rs:4:21
    |
4   |     broken: Box<dyn Any + 'a>
    |                     ^^^   ^^
    |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
   --> tests/ui/lifetimes/point-at-lifetime-obligation-from-trait-in-trait-object.rs:3:18
    |
3   | struct Something<'a> {
    |                  ^^
    = note: but lifetime parameter must outlive the static lifetime
note: unmet `'static` obligations introduced here
   --> rust/library/core/src/any.rs:115:16
    |
115 | pub trait Any: 'static {
    |                ^^^^^^^
```

Fix rust-lang#83325.
estebank added a commit to estebank/rust that referenced this issue Mar 1, 2024
```
error[E0478]: lifetime bound not satisfied
   --> tests/ui/lifetimes/point-at-lifetime-obligation-from-trait-in-trait-object.rs:4:21
    |
4   |     broken: Box<dyn Any + 'a>
    |                     ^^^   ^^
    |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
   --> tests/ui/lifetimes/point-at-lifetime-obligation-from-trait-in-trait-object.rs:3:18
    |
3   | struct Something<'a> {
    |                  ^^
    = note: but lifetime parameter must outlive the static lifetime
note: unmet `'static` obligations introduced here
   --> rust/library/core/src/any.rs:115:16
    |
115 | pub trait Any: 'static {
    |                ^^^^^^^
```

Fix rust-lang#83325.
estebank added a commit to estebank/rust that referenced this issue Mar 20, 2024
```
error[E0478]: lifetime bound not satisfied
   --> tests/ui/lifetimes/point-at-lifetime-obligation-from-trait-in-trait-object.rs:4:21
    |
4   |     broken: Box<dyn Any + 'a>
    |                     ^^^   ^^
    |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
   --> tests/ui/lifetimes/point-at-lifetime-obligation-from-trait-in-trait-object.rs:3:18
    |
3   | struct Something<'a> {
    |                  ^^
    = note: but lifetime parameter must outlive the static lifetime
note: unmet `'static` obligations introduced here
   --> rust/library/core/src/any.rs:115:16
    |
115 | pub trait Any: 'static {
    |                ^^^^^^^
```

Fix rust-lang#83325.
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants