rust-lang / rust-lang/rust

Failing `'static` requirement in `Deref` should point to dereference, not construction

Open
#160,657 1 comment 1 reaction 1 assignee View on GitHub

@ravlynd is already working on this.

Since Sep 4, 2026.

A-borrow-checker A-diagnostics D-imprecise-spans T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct Foo<'a>(&'a u32);

impl std::ops::Deref for Foo<'static> {
    type Target = u32;
    
    fn deref(&self) -> &u32 {
        self.0
    }
}

fn main() {
    let x = 42;
    let foo = Foo(&x);

    // Deref
    let _: &u32 = &*foo;
}
Current output
error[E0597]: `x` does not live long enough
  --> src/main.rs:13:19
   |
12 |     let x = 42;
   |         - binding `x` declared here
13 |     let foo = Foo(&x);
   |               ----^^-
   |               |   |
   |               |   borrowed value does not live long enough
   |               assignment requires that `x` is borrowed for `'static`
...
17 | }
   | - `x` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
Desired output
error[E0597]: `x` does not live long enough
  --> src/main.rs:13:19
   |
12 |     let x = 42;
   |         - binding `x` declared here
13 |     let foo = Foo(&x);
   |                   ^^ borrowed value does not live long enough
...
16 |     let _: &u32 = &*foo;
   |                    ^^^^ requires that `x` is borrowed for `'static`
17 | }
   | - `x` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
Rationale and extra context

I noticed that having a Deref impl that requires 'static makes diagnostics worse when trying to dereference a non-'static variable.

Compare this with AsRef:

struct Foo<'a>(&'a u32);

impl AsRef<u32> for Foo<'static> {
    fn as_ref(&self) -> &u32 {
        self.0
    }
}

fn main() {
    let x = 42;
    let foo = Foo(&x);

    let _: &u32 = foo.as_ref();
}
error[E0597]: `x` does not live long enough
  --> src/main.rs:11:19
   |
10 |     let x = 42;
   |         - binding `x` declared here
11 |     let foo = Foo(&x);
   |                   ^^ borrowed value does not live long enough
12 |
13 |     let _: &u32 = foo.as_ref();
   |                   ------------ argument requires that `x` is borrowed for `'static`
14 | }
   | - `x` dropped here while still borrowed
Rust Version
$ rustc 1.99.0-nightly (8ab9fdff5 2026-07-30)
binary: rustc
commit-hash: 8ab9fdff5a91b9f2b5ed57fb0275452d9a0d0280
commit-date: 2026-07-30
host: aarch64-apple-darwin
release: 1.99.0-nightly
LLVM version: 22.1.8

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.