Rollup merge of #62608 - delan:async-unsafe-fn-tests, r=Centril
`async unsafe fn` tests - cc #62121 r? @Centril
This commit is contained in:
commit
b1d6163622
5 changed files with 118 additions and 2 deletions
|
|
@ -70,6 +70,8 @@ fn async_nonmove_block(x: u8) -> impl Future<Output = u8> {
|
|||
}
|
||||
}
|
||||
|
||||
// see async-closure.rs for async_closure + async_closure_in_unsafe_block
|
||||
|
||||
async fn async_fn(x: u8) -> u8 {
|
||||
wake_and_yield_once().await;
|
||||
x
|
||||
|
|
@ -120,6 +122,18 @@ async unsafe fn unsafe_async_fn(x: u8) -> u8 {
|
|||
x
|
||||
}
|
||||
|
||||
unsafe fn unsafe_fn(x: u8) -> u8 {
|
||||
x
|
||||
}
|
||||
|
||||
fn async_block_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
|
||||
unsafe {
|
||||
async move {
|
||||
unsafe_fn(unsafe_async_fn(x).await)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Foo;
|
||||
|
||||
trait Bar {
|
||||
|
|
@ -176,6 +190,7 @@ fn main() {
|
|||
async_fn,
|
||||
generic_async_fn,
|
||||
async_fn_with_internal_borrow,
|
||||
async_block_in_unsafe_block,
|
||||
Foo::async_assoc_item,
|
||||
|x| {
|
||||
async move {
|
||||
|
|
|
|||
|
|
@ -53,6 +53,21 @@ fn async_closure(x: u8) -> impl Future<Output = u8> {
|
|||
})(x)
|
||||
}
|
||||
|
||||
fn async_closure_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
|
||||
(unsafe {
|
||||
async move |x: u8| unsafe_fn(unsafe_async_fn(x).await)
|
||||
})(x)
|
||||
}
|
||||
|
||||
async unsafe fn unsafe_async_fn(x: u8) -> u8 {
|
||||
wake_and_yield_once().await;
|
||||
x
|
||||
}
|
||||
|
||||
unsafe fn unsafe_fn(x: u8) -> u8 {
|
||||
x
|
||||
}
|
||||
|
||||
fn test_future_yields_once_then_returns<F, Fut>(f: F)
|
||||
where
|
||||
F: FnOnce(u8) -> Fut,
|
||||
|
|
@ -77,5 +92,6 @@ fn main() {
|
|||
|
||||
test! {
|
||||
async_closure,
|
||||
async_closure_in_unsafe_block,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
21
src/test/ui/async-await/async-unsafe-fn-call-in-safe.rs
Normal file
21
src/test/ui/async-await/async-unsafe-fn-call-in-safe.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// edition:2018
|
||||
|
||||
#![feature(async_await)]
|
||||
|
||||
struct S;
|
||||
|
||||
impl S {
|
||||
async unsafe fn f() {}
|
||||
}
|
||||
|
||||
async unsafe fn f() {}
|
||||
|
||||
async fn g() {
|
||||
S::f(); //~ ERROR call to unsafe function is unsafe
|
||||
f(); //~ ERROR call to unsafe function is unsafe
|
||||
}
|
||||
|
||||
fn main() {
|
||||
S::f(); //~ ERROR call to unsafe function is unsafe
|
||||
f(); //~ ERROR call to unsafe function is unsafe
|
||||
}
|
||||
35
src/test/ui/async-await/async-unsafe-fn-call-in-safe.stderr
Normal file
35
src/test/ui/async-await/async-unsafe-fn-call-in-safe.stderr
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
|
||||
--> $DIR/async-unsafe-fn-call-in-safe.rs:14:5
|
||||
|
|
||||
LL | S::f();
|
||||
| ^^^^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
|
||||
--> $DIR/async-unsafe-fn-call-in-safe.rs:15:5
|
||||
|
|
||||
LL | f();
|
||||
| ^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
|
||||
--> $DIR/async-unsafe-fn-call-in-safe.rs:19:5
|
||||
|
|
||||
LL | S::f();
|
||||
| ^^^^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
|
||||
--> $DIR/async-unsafe-fn-call-in-safe.rs:20:5
|
||||
|
|
||||
LL | f();
|
||||
| ^^^ call to unsafe function
|
||||
|
|
||||
= note: consult the function's documentation for information on how to avoid undefined behavior
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0133`.
|
||||
|
|
@ -77,6 +77,12 @@ fn async_closure(x: u8) -> impl Future<Output = u8> {
|
|||
})(x)
|
||||
}
|
||||
|
||||
fn async_closure_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
|
||||
(unsafe {
|
||||
async move |x: u8| unsafe_fn(await!(unsafe_async_fn(x)))
|
||||
})(x)
|
||||
}
|
||||
|
||||
async fn async_fn(x: u8) -> u8 {
|
||||
await!(wake_and_yield_once());
|
||||
x
|
||||
|
|
@ -127,6 +133,18 @@ async unsafe fn unsafe_async_fn(x: u8) -> u8 {
|
|||
x
|
||||
}
|
||||
|
||||
unsafe fn unsafe_fn(x: u8) -> u8 {
|
||||
x
|
||||
}
|
||||
|
||||
fn async_block_in_unsafe_block(x: u8) -> impl Future<Output = u8> {
|
||||
unsafe {
|
||||
async move {
|
||||
unsafe_fn(await!(unsafe_async_fn(x)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Foo;
|
||||
|
||||
trait Bar {
|
||||
|
|
@ -134,11 +152,15 @@ trait Bar {
|
|||
}
|
||||
|
||||
impl Foo {
|
||||
async fn async_method(x: u8) -> u8 {
|
||||
async fn async_assoc_item(x: u8) -> u8 {
|
||||
unsafe {
|
||||
await!(unsafe_async_fn(x))
|
||||
}
|
||||
}
|
||||
|
||||
async unsafe fn async_unsafe_assoc_item(x: u8) -> u8 {
|
||||
await!(unsafe_async_fn(x))
|
||||
}
|
||||
}
|
||||
|
||||
fn test_future_yields_once_then_returns<F, Fut>(f: F)
|
||||
|
|
@ -177,15 +199,22 @@ fn main() {
|
|||
async_block,
|
||||
async_nonmove_block,
|
||||
async_closure,
|
||||
async_closure_in_unsafe_block,
|
||||
async_fn,
|
||||
generic_async_fn,
|
||||
async_fn_with_internal_borrow,
|
||||
Foo::async_method,
|
||||
async_block_in_unsafe_block,
|
||||
Foo::async_assoc_item,
|
||||
|x| {
|
||||
async move {
|
||||
unsafe { await!(unsafe_async_fn(x)) }
|
||||
}
|
||||
},
|
||||
|x| {
|
||||
async move {
|
||||
unsafe { await!(Foo::async_unsafe_assoc_item(x)) }
|
||||
}
|
||||
},
|
||||
}
|
||||
test_with_borrow! {
|
||||
async_block_with_borrow_named_lifetime,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue