rust-lang / rust-lang/rust

bad error message for missing static bounds on `&dyn Trait`

Open
#162,882 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-dyn-trait A-lifetimes D-incorrect D-lack-of-suggestion T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by compiling the provided reproducer with the referenced nightly rustc and trace the diagnostic and trait-resolution path for Error::is on dyn Error. Done means the diagnostic explains the missing 'static bound or identifies that is is implemented for dyn Error + 'static, with a regression test covering this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.