Skip to content

bad error message for missing static bounds on &dyn Trait #162882

Description

@sadan4

Code

use std::error::Error;
fn is_caused_by<T: Error + 'static>(err: &dyn Error) -> bool {
    err.is::<T>()
        || err
            .source()
            .is_some_and(|cause| is_caused_by::<T>(cause))
}

Current output

error[E0521]: borrowed data escapes outside of function
 --> src/lib.rs:3:5
  |
2 | fn is_caused_by<T: StdError + 'static>(err: &dyn StdError) -> bool {
  |                                        ---  - let's call the lifetime of this reference `'1`
  |                                        |
  |                                        `err` is a reference that is only valid in the function body
3 |     err.is::<T>()
  |     ^^^^^^^^^^^^^
  |     |
  |     `err` escapes the function body here
  |     argument requires that `'1` must outlive `'static`

For more information about this error, try `rustc --explain E0521`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

the error message should mention the missing 'static bound on dyn Error or mention that the is method is implemented for dyn Error + 'static

Rationale and extra context

No response

Other cases

Rust Version

rustc 1.100.0-nightly (923c95cdf 2026-09-16)
binary: rustc
commit-hash: 923c95cdf5ba65cea505aa2ea829f578e1506ed8
commit-date: 2026-09-16
host: x86_64-unknown-linux-gnu
release: 1.100.0-nightly
LLVM version: 23.1.1

Anything else?

No response

Activity

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-diagnosticsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutA-lifetimesArea: Lifetimes / regionsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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