Skip to content

Commit af5b0d9

Browse files
authored
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.
2 parents d2b63d4 + 3e9c95b commit af5b0d9

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

compiler/rustc_error_codes/src/error_codes/E0373.md

+21
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,24 @@ fn foo() -> Box<Fn(u32) -> u32> {
5050

5151
Now that the closure has its own copy of the data, there's no need to worry
5252
about safety.
53+
54+
This error may also be encountered while using `async` blocks:
55+
56+
```compile_fail,E0373,edition2018
57+
use std::future::Future;
58+
59+
async fn f() {
60+
let v = vec![1, 2, 3i32];
61+
spawn(async { //~ ERROR E0373
62+
println!("{:?}", v)
63+
});
64+
}
65+
66+
fn spawn<F: Future + Send + 'static>(future: F) {
67+
unimplemented!()
68+
}
69+
```
70+
71+
Similarly to closures, `async` blocks are not executed immediately and may
72+
capture closed-over data by reference. For more information, see
73+
https://rust-lang.github.io/async-book/03_async_await/01_chapter.html.

compiler/rustc_mir/src/borrow_check/diagnostics/conflict_errors.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1318,21 +1318,30 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13181318
Applicability::MachineApplicable,
13191319
);
13201320

1321-
let msg = match category {
1321+
match category {
13221322
ConstraintCategory::Return(_) | ConstraintCategory::OpaqueType => {
1323-
format!("{} is returned here", kind)
1323+
let msg = format!("{} is returned here", kind);
1324+
err.span_note(constraint_span, &msg);
13241325
}
13251326
ConstraintCategory::CallArgument => {
13261327
fr_name.highlight_region_name(&mut err);
1327-
format!("function requires argument type to outlive `{}`", fr_name)
1328+
if matches!(use_span.generator_kind(), Some(GeneratorKind::Async(_))) {
1329+
err.note(
1330+
"async blocks are not executed immediately and must either take a \
1331+
reference or ownership of outside variables they use",
1332+
);
1333+
} else {
1334+
let msg = format!("function requires argument type to outlive `{}`", fr_name);
1335+
err.span_note(constraint_span, &msg);
1336+
}
13281337
}
13291338
_ => bug!(
13301339
"report_escaping_closure_capture called with unexpected constraint \
13311340
category: `{:?}`",
13321341
category
13331342
),
1334-
};
1335-
err.span_note(constraint_span, &msg);
1343+
}
1344+
13361345
err
13371346
}
13381347

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// edition:2018
2+
3+
use std::{sync::Arc, future::Future, pin::Pin, task::{Context, Poll}};
4+
5+
async fn f() {
6+
let room_ref = Arc::new(Vec::new());
7+
8+
let gameloop_handle = spawn(async { //~ ERROR E0373
9+
game_loop(Arc::clone(&room_ref))
10+
});
11+
gameloop_handle.await;
12+
}
13+
14+
fn game_loop(v: Arc<Vec<usize>>) {}
15+
16+
fn spawn<F>(future: F) -> JoinHandle
17+
where
18+
F: Future + Send + 'static,
19+
F::Output: Send + 'static,
20+
{
21+
loop {}
22+
}
23+
24+
struct JoinHandle;
25+
26+
impl Future for JoinHandle {
27+
type Output = ();
28+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
29+
loop {}
30+
}
31+
}
32+
33+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0373]: async block may outlive the current function, but it borrows `room_ref`, which is owned by the current function
2+
--> $DIR/issue-78938-async-block.rs:8:39
3+
|
4+
LL | let gameloop_handle = spawn(async {
5+
| _______________________________________^
6+
LL | | game_loop(Arc::clone(&room_ref))
7+
| | -------- `room_ref` is borrowed here
8+
LL | | });
9+
| |_____^ may outlive borrowed value `room_ref`
10+
|
11+
= note: async blocks are not executed immediately and must either take a reference or ownership of outside variables they use
12+
help: to force the async block to take ownership of `room_ref` (and any other referenced variables), use the `move` keyword
13+
|
14+
LL | let gameloop_handle = spawn(async move {
15+
LL | game_loop(Arc::clone(&room_ref))
16+
LL | });
17+
|
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0373`.

0 commit comments

Comments
 (0)