Open
Description
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");
}
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
Labels
Area: Async & AwaitArea: Closures (`|…| { … }`)Area: Messages for errors, warnings, and lintsArea: Lifetimes / regionsAsync-await issues that have been triaged during a working group meeting.Category: This is a bug.Medium priorityRelevant to the compiler team, which will review and decide on the PR/issue.