rust-lang / rust-lang/rust

"This generic parameter must be used with a generic lifetime parameter" on RPIT with precise capturing

Open
#135,152 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code:

struct Foo<'a> {
    children: Vec<&'a Foo<'a>>,
}

struct Bar;

impl<'a> Foo<'a> {
    fn failing<'p>(&'a self, param: &'p Bar) -> impl Iterator<Item = String> + use<'a, 'p> {
        self.children.iter().flat_map(|child| child.failing(param))
    }
}

I expected to see this happen:
the code compiles, since param gets used by all the children

Or, if the code actually violates ownership rules, get a clearer error message.

Instead, this happened:
compiler complains with "This generic parameter must be used with a generic lifetime parameter"

I've tried the following solutions:

  1. use an anonymous lifetime instead of 'p, like this:
fn failing(&'a self, param: &Bar) -> impl Iterator<Item = String> + use<'a, '_> { /* ... */ }
  1. make the two lifetimes the same:
fn failing(&'a self, param: &'a Bar) -> impl Iterator<Item = String> + use<'a> { /* ... */ }

And still got the same bizarre error.

Meta
rustc 1.82.0 (f6e511eec 2024-10-15) (built from a source tarball)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 18.1.8

also present on latest stable (1.83.0), beta (1.84.0-beta.6), and nightly (2025-01-05)

Backtrace

error[E0792]: expected generic lifetime parameter, found `'_`
   --> src/tree.rs:656:9
    |
655 |     fn failing<'p>(&'a self, param: &'p Bar) -> impl Iterator<Item = String> + use<'a, 'p> {
    |                                                                                    -- this generic parameter must be used with a generic lifetime parameter
656 |         self.children.iter().flat_map(|child| child.failing(param))
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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 reproducing the RPIT precise-capturing example and its E0792 diagnostic using the compiler versions listed in the issue; the reported location is src/tree.rs:656. Trace the compiler's handling of generic lifetime parameters in this case and add regression coverage. Done means the example either compiles as expected or produces a clear, correct ownership diagnostic.

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.