Skip to content

Commit b1d6163

Browse files
authored
Rollup merge of #62608 - delan:async-unsafe-fn-tests, r=Centril
`async unsafe fn` tests - cc #62121 r? @Centril
2 parents a7f1649 + 5f8d0a1 commit b1d6163

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

src/test/ui/async-await/async-await.rs

+15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
7070
}
7171
}
7272

73+
// see async-closure.rs for async_closure + async_closure_in_unsafe_block
74+
7375
async fn async_fn(x: u8) -> u8 {
7476
wake_and_yield_once().await;
7577
x
@@ -120,6 +122,18 @@ async unsafe fn unsafe_async_fn(x: u8) -> u8 {
120122
x
121123
}
122124

125+
unsafe fn unsafe_fn(x: u8) -> u8 {
126+
x
127+
}
128+
129+
fn async_block_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
130+
unsafe {
131+
async move {
132+
unsafe_fn(unsafe_async_fn(x).await)
133+
}
134+
}
135+
}
136+
123137
struct Foo;
124138

125139
trait Bar {
@@ -176,6 +190,7 @@ fn main() {
176190
async_fn,
177191
generic_async_fn,
178192
async_fn_with_internal_borrow,
193+
async_block_in_unsafe_block,
179194
Foo::async_assoc_item,
180195
|x| {
181196
async move {

src/test/ui/async-await/async-closure.rs

+16
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ fn async_closure(x: u8) -> impl Future<Output = u8> {
5353
})(x)
5454
}
5555

56+
fn async_closure_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
57+
(unsafe {
58+
async move |x: u8| unsafe_fn(unsafe_async_fn(x).await)
59+
})(x)
60+
}
61+
62+
async unsafe fn unsafe_async_fn(x: u8) -> u8 {
63+
wake_and_yield_once().await;
64+
x
65+
}
66+
67+
unsafe fn unsafe_fn(x: u8) -> u8 {
68+
x
69+
}
70+
5671
fn test_future_yields_once_then_returns<F, Fut>(f: F)
5772
where
5873
F: FnOnce(u8) -> Fut,
@@ -77,5 +92,6 @@ fn main() {
7792

7893
test! {
7994
async_closure,
95+
async_closure_in_unsafe_block,
8096
}
8197
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// edition:2018
2+
3+
#![feature(async_await)]
4+
5+
struct S;
6+
7+
impl S {
8+
async unsafe fn f() {}
9+
}
10+
11+
async unsafe fn f() {}
12+
13+
async fn g() {
14+
S::f(); //~ ERROR call to unsafe function is unsafe
15+
f(); //~ ERROR call to unsafe function is unsafe
16+
}
17+
18+
fn main() {
19+
S::f(); //~ ERROR call to unsafe function is unsafe
20+
f(); //~ ERROR call to unsafe function is unsafe
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
2+
--> $DIR/async-unsafe-fn-call-in-safe.rs:14:5
3+
|
4+
LL | S::f();
5+
| ^^^^^^ call to unsafe function
6+
|
7+
= note: consult the function's documentation for information on how to avoid undefined behavior
8+
9+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
10+
--> $DIR/async-unsafe-fn-call-in-safe.rs:15:5
11+
|
12+
LL | f();
13+
| ^^^ call to unsafe function
14+
|
15+
= note: consult the function's documentation for information on how to avoid undefined behavior
16+
17+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
18+
--> $DIR/async-unsafe-fn-call-in-safe.rs:19:5
19+
|
20+
LL | S::f();
21+
| ^^^^^^ call to unsafe function
22+
|
23+
= note: consult the function's documentation for information on how to avoid undefined behavior
24+
25+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
26+
--> $DIR/async-unsafe-fn-call-in-safe.rs:20:5
27+
|
28+
LL | f();
29+
| ^^^ call to unsafe function
30+
|
31+
= note: consult the function's documentation for information on how to avoid undefined behavior
32+
33+
error: aborting due to 4 previous errors
34+
35+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/async-await/await-macro.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ fn async_closure(x: u8) -> impl Future<Output = u8> {
7777
})(x)
7878
}
7979

80+
fn async_closure_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
81+
(unsafe {
82+
async move |x: u8| unsafe_fn(await!(unsafe_async_fn(x)))
83+
})(x)
84+
}
85+
8086
async fn async_fn(x: u8) -> u8 {
8187
await!(wake_and_yield_once());
8288
x
@@ -127,18 +133,34 @@ async unsafe fn unsafe_async_fn(x: u8) -> u8 {
127133
x
128134
}
129135

136+
unsafe fn unsafe_fn(x: u8) -> u8 {
137+
x
138+
}
139+
140+
fn async_block_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
141+
unsafe {
142+
async move {
143+
unsafe_fn(await!(unsafe_async_fn(x)))
144+
}
145+
}
146+
}
147+
130148
struct Foo;
131149

132150
trait Bar {
133151
fn foo() {}
134152
}
135153

136154
impl Foo {
137-
async fn async_method(x: u8) -> u8 {
155+
async fn async_assoc_item(x: u8) -> u8 {
138156
unsafe {
139157
await!(unsafe_async_fn(x))
140158
}
141159
}
160+
161+
async unsafe fn async_unsafe_assoc_item(x: u8) -> u8 {
162+
await!(unsafe_async_fn(x))
163+
}
142164
}
143165

144166
fn test_future_yields_once_then_returns<F, Fut>(f: F)
@@ -177,15 +199,22 @@ fn main() {
177199
async_block,
178200
async_nonmove_block,
179201
async_closure,
202+
async_closure_in_unsafe_block,
180203
async_fn,
181204
generic_async_fn,
182205
async_fn_with_internal_borrow,
183-
Foo::async_method,
206+
async_block_in_unsafe_block,
207+
Foo::async_assoc_item,
184208
|x| {
185209
async move {
186210
unsafe { await!(unsafe_async_fn(x)) }
187211
}
188212
},
213+
|x| {
214+
async move {
215+
unsafe { await!(Foo::async_unsafe_assoc_item(x)) }
216+
}
217+
},
189218
}
190219
test_with_borrow! {
191220
async_block_with_borrow_named_lifetime,

0 commit comments

Comments
 (0)