Skip to content

"expected bound lifetime parameter, found concrete lifetime" when passing a 'static reference to a closure that returns a future #68521

Open
@shepmaster

Description

@shepmaster
use std::future::Future;

fn main() {
    let _ = wrapper(hello);
}

static GLOBAL: i32 = 42;

async fn wrapper<F, FutResp>(f: F)
where
    F: Fn(&i32) -> FutResp,
    FutResp: Future<Output = ()>,
{
    f(&GLOBAL).await
}

async fn hello(_: &i32) {
    println!("Hello");
}

(Playground)

Errors:

error[E0271]: type mismatch resolving `for<'r> <for<'_> fn(&i32) -> impl std::future::Future {hello} as std::ops::FnOnce<(&'r i32,)>>::Output == _`
  --> src/main.rs:4:13
   |
4  |     let _ = wrapper(hello);
   |             ^^^^^^^ expected bound lifetime parameter, found concrete lifetime
...
9  | async fn wrapper<F, FutResp>(f: F)
   |          -------
10 | where
11 |     F: Fn(&i32) -> FutResp,
   |                    ------- required by this bound in `wrapper`

I can make the code work by restricting the closure to only accept 'static references:

async fn wrapper<F, FutResp>(f: F)
where
    F: Fn(&'static i32) -> FutResp,
    //     ^^^^^^^
    FutResp: Future<Output = ()>,

I did try to add 'static bounds to the response future (and the closure, for good measure), but this did not help:

async fn wrapper<F, FutResp>(f: F)
where
    F: Fn(&i32) -> FutResp + 'static,
    FutResp: Future<Output = ()> + 'static,

The equivalent(?) synchronous code works as I'd expect:

fn main() {
    let _ = wrapper(hello);
}

static GLOBAL: i32 = 42;

fn wrapper<F>(f: F)
where
    F: Fn(&i32) -> (),
{
    f(&GLOBAL)
}

fn hello(_: &i32) {
    println!("Hello");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions