rust-lang / rust-lang/rust

RPIT associated type lifetime bound is ignored

Open
#135,006 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-impl-trait A-lifetimes C-bug S-has-mcve T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

trait Foo<'x> {
    type Out;
    fn foo(self) -> Self::Out;
}

struct Bar;

impl<'x> Foo<'x> for Bar {
    type Out = ();
    fn foo(self) -> Self::Out {
        todo!()
    }
}

fn make_static_foo<'x>(_: &'x ()) -> impl Foo<'x, Out: 'static> {
    Bar
}

fn assert_static<T: 'static>(_: T) {}

fn test() {
    let local = ();
    assert_static(make_static_foo(&local).foo());
}

I expect it to compile, but instead it gives the following error:

error[E0597]: `local` does not live long enough
  --> src/lib.rs:23:35
   |
22 |     let local = ();
   |         ----- binding `local` declared here
23 |     assert_static(make_static_foo(&local).foo());
   |                   ----------------^^^^^^-
   |                   |               |
   |                   |               borrowed value does not live long enough
   |                   argument requires that `local` is borrowed for `'static`
24 | }
   | - `local` dropped here while still borrowed

It looks like the Out: 'static bound on make_static_foo is just ignored.

The issue sounds similar to #106684, but it is actually the opposite:
In this issue the bound is in the RPIT instead of in the trait definition.

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (7f75bfa1a 2024-12-30)
binary: rustc
commit-hash: 7f75bfa1ad4e9a9d33a179a90603001515e91991
commit-date: 2024-12-30
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Same happens on stable

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 provided minimal Rust reproducer with the reported stable or nightly rustc versions and confirm the lifetime error. Trace how the RPIT Out: 'static bound is handled, then verify that the reproducer compiles without requiring the local reference to be 'static.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.