rust-lang / rust-lang/rust

Confusing diagnostics with nested precise capturing RPIT

Open
#133,716 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-confusing F-impl_trait_in_fn_trait_return requires-nightly T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#![feature(impl_trait_in_fn_trait_return)]

trait Foo {}
impl Foo for &() {}

fn foo<'a>(u: &'a ()) -> impl FnOnce() -> (impl Foo + use<'a>) + use<'a> {
    move || u
}
Current output
error[E0700]: hidden type for `impl Foo` captures lifetime that does not appear in bounds
 --> src/main.rs:7:13
  |
6 | fn foo<'a>(u: &'a ()) -> impl FnOnce() -> (impl Foo + use<'a>) + use<'a> {
  |                                            ------------------ opaque type defined here
7 |     move || u
  |             ^
  |
  = note: hidden type `&()` captures lifetime `'_`

error[E0720]: cannot resolve opaque type
 --> src/main.rs:6:44
  |
6 | fn foo<'a>(u: &'a ()) -> impl FnOnce() -> (impl Foo + use<'a>) + use<'a> {
  |                                            ^^^^^^^^^^^^^^^^^^ recursive opaque type
7 |     move || u
  |     --------- returning here with type `{closure@src/main.rs:7:5: 7:12}`
Desired output

Rationale and extra context

I assume using precise capturing in this position isn't meant to be supported.

Both of these diagnostics seem inaccurate.

The first is talking about some elided lifetime, when the only relevant lifetime is named.

The second is claiming that the return type is recursive when it obviously isn't. This disappears when getting rid of the inner use<'a> so is somehow triggered by using that in this location.

Equivalent-ish code avoiding the closure traits compiles successfully on stable:

trait Foo {}
trait Bar { type Foo: Foo; }
impl Foo for &() {}
impl Bar for &() { type Foo = Self; }

fn foo<'a>(u: &'a ()) -> impl Bar<Foo = impl Foo + use<'a>> + use<'a> {
    u
}
Other cases

Rust Version
1.85.0-nightly

(2024-11-30 7442931d49b199ad0a1c)
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 issue using the Rust code in the report on the specified nightly version, starting with the E0700 and E0720 diagnostic paths reported for src/main.rs. Compare the diagnostics with the named lifetime and nested precise-capturing RPIT structure. Done means the diagnostics accurately describe the failure, or the issue is resolved according to the supported behavior for this syntax.

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.