Skip to content

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

Open
@asquared31415

Description

@asquared31415

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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