Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ada4a opened this issue Jan 6, 2025 · 3 comments
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@ada4a
Copy link
Contributor

ada4a commented Jan 6, 2025

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))
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@ada4a ada4a added the C-bug Category: This is a bug. label Jan 6, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 6, 2025
@fmease fmease added F-precise_capturing `#![feature(precise_capturing)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. and removed F-precise_capturing `#![feature(precise_capturing)]` labels Jan 6, 2025
@fmease
Copy link
Member

fmease commented Jan 6, 2025

Smaller:

//@ edition: 2021

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

impl Foo<'_> {
    fn failing(&self) -> impl Iterator<Item = String> + use<'_> {
        self.children.iter().flat_map(|child| child.failing())
    }
}
//@ edition: 2024

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

impl Foo<'_> {
    fn failing(&self) -> impl Iterator<Item = String> {
        self.children.iter().flat_map(|child| child.failing())
    }
}

@fmease fmease added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Jan 6, 2025
@ada4a
Copy link
Contributor Author

ada4a commented Jan 6, 2025

Oh okay, I though having at least one other parameter was necessary as well. If it's not, then struct Bar; isn't either

@jieyouxu jieyouxu added S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 14, 2025
Jakobeha added a commit to Jakobeha/type-sitter that referenced this issue Feb 1, 2025
The -Znext-solver issue is "expected generic lifetime parameter, found `'_`" with no source. It occurs as of Rust 1.86. It may be related to rust-lang/rust#135152.

The -Znext-solver fix also makes documentation more deterministic.

The 2024 warning is that now we need `unsafe` blocks in `unsafe extern "C"`. functions.
@TomFryersMidsummer
Copy link
Contributor

TomFryersMidsummer commented Mar 14, 2025

You can also get the error without a lifetime parameter on Foo.

struct Foo(Vec<Foo>);

impl Foo {
    fn failing(&self) -> impl Iterator<Item = ()> + use<'_> {
        self.0.iter().flat_map(|b| b.failing())
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants