Lifetime Issue with `impl Trait` in Associated Type - `'_` vs Annotated `'a`

⚓ Rust    📅 2025-07-21    👤 surdeus    👁️ 1      

surdeus

I annotated the lifetime 'a everywhere, but the compiler still complains about an unexpected '_. Is there any way to resolve this in current Rust (using nightly)?

Code with Unstable Feature:

#![feature(impl_trait_in_assoc_type)]

pub trait Curry<'a, P1, P2, R> {
    type OutputL: FnOnce(P2) -> R + 'a;
    fn curryl(self) -> impl FnOnce(P1) -> Self::OutputL + 'a;
}

impl<'a, P1: 'a, P2: 'a, R, F> Curry<'a, P1, P2, R> for F 
where F: FnOnce(P1, P2) -> R + 'a 
{
    type OutputL = impl FnOnce(P2) -> R + 'a;

    fn curryl(self) -> impl FnOnce(P1) -> Self::OutputL + 'a {
        |p1| |p2| self(p1, p2) // Error occurs here
    }
}

Compiler Error:

error[E0792]: expected generic lifetime parameter, found `'_`
  --> src/lib.rs:14:14
   |
8  | impl<'a, P1: 'a, P2: 'a, R, F> Curry<'a, P1, P2, R> for F 
   |      -- this generic parameter must be used with a generic lifetime parameter
...
14 |         |p1| |p2| self(p1, p2)
   |              ^^^^^^^^^^^^^^^^^

Context:

  • Boxing closures works, but I want stack-allocated closures via impl Trait.
  • Using associated types with impl Trait (unstable) seems necessary for this pattern.
  • The error persists despite explicit 'a annotations on all relevant types.

Question:
Does anyone have insight into why the compiler introduces '_ here instead of using 'a, and how to properly express these nested closure lifetimes with associated impl Trait? Are there workarounds without boxing?

2 posts - 2 participants

Read full topic

🏷️ rust_feed