-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Force the inner coroutine of an async closure to
move
if the outer …
…closure is `move` and `FnOnce`
- Loading branch information
1 parent
12075f0
commit 6b30582
Showing
3 changed files
with
109 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tests/ui/async-await/async-closures/force-move-due-to-actually-fnonce.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//@ aux-build:block-on.rs | ||
//@ edition:2021 | ||
//@ check-pass | ||
|
||
#![feature(async_closure)] | ||
|
||
extern crate block_on; | ||
|
||
fn consume(_: String) {} | ||
|
||
fn main() { | ||
block_on::block_on(async { | ||
let x = 1i32; | ||
let s = String::new(); | ||
// `consume(s)` pulls the closure's kind down to `FnOnce`, | ||
// which means that we don't treat the borrow of `x` as a | ||
// self-borrow (with `'env` lifetime). This leads to a lifetime | ||
// error which is solved by forcing the inner coroutine to | ||
// be `move` as well, so that it moves `x`. | ||
let c = async move || { | ||
println!("{x}"); | ||
// This makes the closure FnOnce... | ||
consume(s); | ||
}; | ||
c().await; | ||
}); | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/ui/async-await/async-closures/force-move-due-to-inferred-kind.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//@ aux-build:block-on.rs | ||
//@ edition:2021 | ||
//@ check-pass | ||
|
||
#![feature(async_closure)] | ||
|
||
extern crate block_on; | ||
|
||
fn force_fnonce<T: async FnOnce()>(t: T) -> T { t } | ||
|
||
fn main() { | ||
block_on::block_on(async { | ||
let x = 1i32; | ||
// `force_fnonce` pulls the closure's kind down to `FnOnce`, | ||
// which means that we don't treat the borrow of `x` as a | ||
// self-borrow (with `'env` lifetime). This leads to a lifetime | ||
// error which is solved by forcing the inner coroutine to | ||
// be `move` as well, so that it moves `x`. | ||
let c = force_fnonce(async move || { | ||
println!("{x}"); | ||
}); | ||
c().await; | ||
}); | ||
} |