Skip to content

Commit b60281f

Browse files
committed
Test that async blocks are UnwindSafe
This was a regression from the reverted #105250 which is now covered by a test.
1 parent 1d12c3c commit b60281f

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// edition:2018
2+
3+
fn is_unwindsafe(_: impl std::panic::UnwindSafe) {}
4+
5+
fn main() {
6+
// A normal future created by an async block takes a `&mut Context<'_>` argument.
7+
// That should not leak through to the whole async block.
8+
is_unwindsafe(async {
9+
async {}.await; // this needs an inner await point
10+
});
11+
12+
is_unwindsafe(async {
13+
//~^ ERROR the type `&mut Context<'_>` may not be safely transferred across an unwind boundary
14+
use std::ptr::null;
15+
use std::task::{Context, RawWaker, RawWakerVTable, Waker};
16+
let waker = unsafe {
17+
Waker::from_raw(RawWaker::new(
18+
null(),
19+
&RawWakerVTable::new(|_| todo!(), |_| todo!(), |_| todo!(), |_| todo!()),
20+
))
21+
};
22+
let mut cx = Context::from_waker(&waker);
23+
let cx_ref = &mut cx;
24+
25+
async {}.await; // this needs an inner await point
26+
27+
// in this case, `&mut Context<'_>` is *truly* alive across an await point
28+
drop(cx_ref);
29+
});
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0277]: the type `&mut Context<'_>` may not be safely transferred across an unwind boundary
2+
--> $DIR/async-is-unwindsafe.rs:12:19
3+
|
4+
LL | is_unwindsafe(async {
5+
| ___________________^
6+
LL | |
7+
LL | | use std::ptr::null;
8+
LL | | use std::task::{Context, RawWaker, RawWakerVTable, Waker};
9+
... |
10+
LL | | drop(cx_ref);
11+
LL | | });
12+
| | ^
13+
| | |
14+
| |_____`&mut Context<'_>` may not be safely transferred across an unwind boundary
15+
| within this `[async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6]`
16+
|
17+
= help: within `[async block@$DIR/async-is-unwindsafe.rs:12:19: 29:6]`, the trait `UnwindSafe` is not implemented for `&mut Context<'_>`
18+
= note: `UnwindSafe` is implemented for `&std::task::Context<'_>`, but not for `&mut std::task::Context<'_>`
19+
note: future does not implement `UnwindSafe` as this value is used across an await
20+
--> $DIR/async-is-unwindsafe.rs:25:17
21+
|
22+
LL | let cx_ref = &mut cx;
23+
| ------ has type `&mut Context<'_>` which does not implement `UnwindSafe`
24+
LL |
25+
LL | async {}.await; // this needs an inner await point
26+
| ^^^^^^ await occurs here, with `cx_ref` maybe used later
27+
...
28+
LL | });
29+
| - `cx_ref` is later dropped here
30+
note: required by a bound in `is_unwindsafe`
31+
--> $DIR/async-is-unwindsafe.rs:3:26
32+
|
33+
LL | fn is_unwindsafe(_: impl std::panic::UnwindSafe) {}
34+
| ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_unwindsafe`
35+
36+
error: aborting due to previous error
37+
38+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)