rust-lang / rust-lang/rust

Lifetime annotation not respected in recursive function with RPIT

Open
#153,597 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-impl-trait A-variance C-bug fixed-by-next-solver I-unsound P-high regression-from-stable-to-stable T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Edit: See this comment for an unsoundness (in the old solver).

I tried this code:

fn conjure<T>() -> T { panic!() }

#[expect(unconditional_recursion)]
pub fn foo<'a: 'a>() -> impl Sized + use<> {
    let _: Contra<'a> = foo::<'a>();
    conjure()
}

struct Contra<'a>(fn(&'a ()));

I expect a compile error. Due to the use<>, the return type has to be Contra<'static>, which shouldn't be coercible to Contra<'a>. However, the above code compiles fine.

Changing let _ to let _x gives me the following error I expect:

error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
 --> src/lib.rs:5:26
  |
4 | pub fn foo<'a: 'a>() -> impl Sized + use<> {
  |            --           ------------------ opaque type defined here
  |            |
  |            hidden type `Contra<'a>` captures the lifetime `'a` as defined here
5 |     let _x: Contra<'a> = foo::<'a>();
  |                          ^^^^^^^^^^^
  |
help: add `'a` to the `use<...>` bound to explicitly capture it
  |
4 | pub fn foo<'a: 'a>() -> impl Sized + use<'a> {
  |                                          ++

For more information about this error, try `rustc --explain E0700`.
Meta

Reproducible on the playground with version 1.96.0-nightly (2026-03-08 b41f22de2a13a0babd28)

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 running the src/lib.rs reproducer from the issue on the stated nightly version. Trace RPIT lifetime capture for the recursive foo::<’a>() call and compare it with the let _x case that reports E0700. Done means the original let _ case is rejected appropriately, with regression coverage added for the reproducer.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.