rust-lang / rust-lang/rust

"borrowed data escapes" diagnostic claims that anything with escaping trait object is a reference

Open
#162,890 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Found while playing with #162882

Code
use std::error::Error;

struct Generic<'a>(&'a ());
impl<'a> Generic<'a> {
    fn get(&self) -> &(dyn Error + 'a) {
        unimplemented!()
    }
}

// Or use deref with `Box<dyn Error + '_>`, `Rc<dyn Error + '_>`, etc.
fn err_is<T: Error + 'static>(err: Generic<'_>) {
    err.get().is::<T>(); // anything that takes `&(dyn Trait + 'static)
}
Current output
error[E0521]: borrowed data escapes outside of function
  --> src/main.rs:11:5
   |
10 | fn err_is<T: Error + 'static>(err: Generic<'_>) {
   |                               ---
   |                               |
   |                               `err` is a reference that is only valid in the function body
   |                               has type `Generic<'1>`
11 |     err.get().is::<T>();
   |     ^^^^^^^^^^^^^^^^^^^
   |     |
   |     `err` escapes the function body here
   |     argument requires that `'1` must outlive `'static`
Desired output

Explain where the lifetime is coming from and that it is too short.
References are completely irrelevant here (see comment for an example that involves no references, even in the call site).

Rust Version
cargo 1.100.0-nightly (7941be6fb 2026-09-11)
release: 1.100.0-nightly
commit-hash: 7941be6fb416b4cd9666aef7b858dfea25587a8c
commit-date: 2026-09-11
host: x86_64-unknown-linux-gnu
libgit2: 1.9.6 (sys:0.21.0 vendored)
libcurl: 8.21.0-DEV (sys:0.4.90+curl-8.21.0 vendored ssl:OpenSSL/3.6.3)
ssl: OpenSSL 3.6.3 9 Jun 2026
os: NixOS 26.5.0 (yarara) [64-bit]

@rustbot label D-incorrect A-dyn-trait A-lifetimes T-types

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

Start by compiling the Rust snippet in the issue and compare the current diagnostic with the desired explanation. Trace the diagnostic behavior around the err.get().is::<T>() call and the lifetime requirement, then update coverage so the output explains the lifetime source and why it is too short; use the linked comment's no-reference example as an additional check.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.