-
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.
Rollup merge of #80614 - 1000teslas:issue-78938-fix, r=tmandry
Explain why borrows can't be held across yield point in async blocks For #78938.
- Loading branch information
Showing
4 changed files
with
89 additions
and
5 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
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
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,33 @@ | ||
// edition:2018 | ||
|
||
use std::{sync::Arc, future::Future, pin::Pin, task::{Context, Poll}}; | ||
|
||
async fn f() { | ||
let room_ref = Arc::new(Vec::new()); | ||
|
||
let gameloop_handle = spawn(async { //~ ERROR E0373 | ||
game_loop(Arc::clone(&room_ref)) | ||
}); | ||
gameloop_handle.await; | ||
} | ||
|
||
fn game_loop(v: Arc<Vec<usize>>) {} | ||
|
||
fn spawn<F>(future: F) -> JoinHandle | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
loop {} | ||
} | ||
|
||
struct JoinHandle; | ||
|
||
impl Future for JoinHandle { | ||
type Output = (); | ||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
loop {} | ||
} | ||
} | ||
|
||
fn main() {} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/async-await/issues/issue-78938-async-block.stderr
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,21 @@ | ||
error[E0373]: async block may outlive the current function, but it borrows `room_ref`, which is owned by the current function | ||
--> $DIR/issue-78938-async-block.rs:8:39 | ||
| | ||
LL | let gameloop_handle = spawn(async { | ||
| _______________________________________^ | ||
LL | | game_loop(Arc::clone(&room_ref)) | ||
| | -------- `room_ref` is borrowed here | ||
LL | | }); | ||
| |_____^ may outlive borrowed value `room_ref` | ||
| | ||
= note: async blocks are not executed immediately and must either take a reference or ownership of outside variables they use | ||
help: to force the async block to take ownership of `room_ref` (and any other referenced variables), use the `move` keyword | ||
| | ||
LL | let gameloop_handle = spawn(async move { | ||
LL | game_loop(Arc::clone(&room_ref)) | ||
LL | }); | ||
| | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0373`. |