Skip to content

Commit

Permalink
Rollup merge of rust-lang#62608 - delan:async-unsafe-fn-tests, r=Centril
Browse files Browse the repository at this point in the history
`async unsafe fn` tests

- cc rust-lang#62121

r? @Centril
  • Loading branch information
Centril authored Jul 12, 2019
2 parents a7f1649 + 5f8d0a1 commit b1d6163
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/test/ui/async-await/async-await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/async-await/async-closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -77,5 +92,6 @@ fn main() {

test! {
async_closure,
async_closure_in_unsafe_block,
}
}
21 changes: 21 additions & 0 deletions src/test/ui/async-await/async-unsafe-fn-call-in-safe.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions src/test/ui/async-await/async-unsafe-fn-call-in-safe.stderr
Original file line number Diff line number Diff line change
@@ -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`.
33 changes: 31 additions & 2 deletions src/test/ui/async-await/await-macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -127,18 +133,34 @@ 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 {
fn foo() {}
}

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)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b1d6163

Please sign in to comment.