-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Open
Labels
A-async-awaitArea: Async & AwaitArea: Async & AwaitA-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.Async-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.Category: This is a bug.P-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
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");
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-async-awaitArea: Async & AwaitArea: Async & AwaitA-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.Async-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.Category: This is a bug.P-mediumMedium priorityMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.