rust-lang / rust-lang/rust

Compiler doesn't explain that unfulfilled lifetime requirement comes from object lifetime default when it's behind an assoc ty in trait impl

Open
#147,668 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
use std::ops::Deref;

enum Either {
    Foo(Box<dyn Foo>),
    Bar(Box<dyn Bar>),
}

impl Either {
    fn as_foo(&self) -> &dyn Foo {
        // Compiles
        match self {
            Self::Foo(f) => f.as_ref(),
            Self::Bar(b) => b.as_foo(),
        }
    }
}

impl Deref for Either {
    type Target = dyn Foo;

    fn deref(&self) -> &Self::Target {
        // "lifetime may not live long enough, `&self` must outlive `'static`"
        match self {
            Self::Foo(f) => f.as_ref(),
            Self::Bar(b) => b.as_foo(),
        }
    }
}

trait Bar: Foo {
    fn as_foo(&self) -> &dyn Foo;
}

trait Foo {}

Playground.

Current output
error: lifetime may not live long enough
  --> src/lib.rs:23:9
   |
21 |       fn deref(&self) -> &Self::Target {
   |                - let's call the lifetime of this reference `'1`
22 |           // "lifetime may not live long enough, `&self` must outlive `'static`"
23 | /         match self {
24 | |             Self::Foo(f) => f.as_ref(),
25 | |             Self::Bar(b) => b.as_foo(),
26 | |         }
   | |_________^ returning this value requires that `'1` must outlive `'static`
Desired output
It's very unclear where the `'static` requirement is coming from in this case.
Rationale and extra context

No response

Other cases
Note that the same code works in an inherent impl method, which makes the error unintuitive.
Rust Version
rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: aarch64-apple-darwin
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

Reproduce the Rust snippet with rustc 1.90.0 and compare the inherent impl with the Deref impl. Trace the compiler's lifetime diagnostics for the associated Target type and object lifetime default; done means the diagnostic identifies why the 'static requirement arises and includes a focused regression test.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.