rust-lang / rust-lang/rust

unclear diagnostic for when lifetimes are bound to static via error conversion

Open
#147,278 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-lifetimes D-newcomer-roadblock T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use thiserror::Error; // 2.0.16
use anyhow;

#[derive(Error, Copy, Clone, Debug)]
#[error("something happened in {ctx:?}")]
struct MyError<'a>{
    ctx: &'a str,
}

fn frob(src: &str) -> anyhow::Result<u8> {
    Ok(frob_inner(src)?)
}

fn frob_inner(src: &str) -> Result<u8, MyError<'_>> {
    // TODO
    Err(MyError{
        ctx: src,
    })
}
Current output
error[E0521]: borrowed data escapes outside of function
  --> src/lib.rs:11:8
   |
10 | fn frob(src: &str) -> anyhow::Result<u8> {
   |         ---  - let's call the lifetime of this reference `'1`
   |         |
   |         `src` is a reference that is only valid in the function body
11 |     Ok(frob_inner(src)?)
   |        ^^^^^^^^^^^^^^^
   |        |
   |        `src` 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
error[E0521]: borrowed data escapes outside of function
  --> src/lib.rs:11:8
   |
10 | fn frob(src: &str) -> anyhow::Result<u8> {
   |         ---  - let's call the lifetime of this reference `'1`
   |         |
   |         `src` is a reference that is only valid in the function body
11 |     Ok(frob_inner(src)?)
   |        ^^^^^^^^^^^^^^^
   |        |
   |        `src` escapes the function body here
   |        argument requires that `'1` must outlive `'static`
   = note: converting `MyError` to `anyhow::Result` requires that it is `'static`

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

No response

Other cases

Rust Version
rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: x86_64-unknown-linux-gnu
release: 1.90.0
LLVM version: 20.1.8
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

Start with the Rust reproduction in src/lib.rs and compile it with the reported rustc version to confirm the current diagnostic. Trace how the error conversion produces the 'static requirement, then update the diagnostic so it includes the note shown under Desired output. Re-run the reproduction to verify the wording.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.