-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,487 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
//@revisions: stack tree | ||
//@compile-flags: -Zmiri-strict-provenance | ||
//@[tree]compile-flags: -Zmiri-tree-borrows | ||
|
||
// WARNING: If you would ever want to modify this test, | ||
// please consider modifying rustc's async drop test at | ||
// `tests/ui/async-await/async-drop/async-drop-initial.rs`. | ||
|
||
#![feature(async_drop, impl_trait_in_assoc_type, noop_waker, async_closure)] | ||
#![allow(incomplete_features, dead_code)] | ||
|
||
// FIXME(zetanumbers): consider AsyncDestruct::async_drop cleanup tests | ||
use core::future::{async_drop_in_place, AsyncDrop, Future}; | ||
use core::hint::black_box; | ||
use core::mem::{self, ManuallyDrop}; | ||
use core::pin::{pin, Pin}; | ||
use core::task::{Context, Poll, Waker}; | ||
|
||
async fn test_async_drop<T>(x: T) { | ||
let mut x = mem::MaybeUninit::new(x); | ||
let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) }); | ||
test_idempotency(dtor).await; | ||
} | ||
|
||
fn test_idempotency<T>(mut x: Pin<&mut T>) -> impl Future<Output = ()> + '_ | ||
where | ||
T: Future<Output = ()>, | ||
{ | ||
core::future::poll_fn(move |cx| { | ||
assert_eq!(x.as_mut().poll(cx), Poll::Ready(())); | ||
assert_eq!(x.as_mut().poll(cx), Poll::Ready(())); | ||
Poll::Ready(()) | ||
}) | ||
} | ||
|
||
fn main() { | ||
let waker = Waker::noop(); | ||
let mut cx = Context::from_waker(&waker); | ||
|
||
let i = 13; | ||
let fut = pin!(async { | ||
test_async_drop(Int(0)).await; | ||
test_async_drop(AsyncInt(0)).await; | ||
test_async_drop([AsyncInt(1), AsyncInt(2)]).await; | ||
test_async_drop((AsyncInt(3), AsyncInt(4))).await; | ||
test_async_drop(5).await; | ||
let j = 42; | ||
test_async_drop(&i).await; | ||
test_async_drop(&j).await; | ||
test_async_drop(AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 }).await; | ||
test_async_drop(ManuallyDrop::new(AsyncInt(9))).await; | ||
|
||
let foo = AsyncInt(10); | ||
test_async_drop(AsyncReference { foo: &foo }).await; | ||
|
||
let foo = AsyncInt(11); | ||
test_async_drop(|| { | ||
black_box(foo); | ||
let foo = AsyncInt(10); | ||
foo | ||
}) | ||
.await; | ||
|
||
test_async_drop(AsyncEnum::A(AsyncInt(12))).await; | ||
test_async_drop(AsyncEnum::B(SyncInt(13))).await; | ||
|
||
test_async_drop(SyncInt(14)).await; | ||
test_async_drop(SyncThenAsync { i: 15, a: AsyncInt(16), b: SyncInt(17), c: AsyncInt(18) }) | ||
.await; | ||
|
||
let mut ptr19 = mem::MaybeUninit::new(AsyncInt(19)); | ||
let async_drop_fut = pin!(unsafe { async_drop_in_place(ptr19.as_mut_ptr()) }); | ||
test_idempotency(async_drop_fut).await; | ||
|
||
let foo = AsyncInt(20); | ||
test_async_drop(async || { | ||
black_box(foo); | ||
let foo = AsyncInt(19); | ||
// Await point there, but this is async closure so it's fine | ||
black_box(core::future::ready(())).await; | ||
foo | ||
}) | ||
.await; | ||
|
||
test_async_drop(AsyncUnion { signed: 21 }).await; | ||
}); | ||
let res = fut.poll(&mut cx); | ||
assert_eq!(res, Poll::Ready(())); | ||
} | ||
|
||
struct AsyncInt(i32); | ||
|
||
impl Drop for AsyncInt { | ||
fn drop(&mut self) { | ||
println!("AsyncInt::drop: {}", self.0); | ||
} | ||
} | ||
impl AsyncDrop for AsyncInt { | ||
async fn drop(self: Pin<&mut Self>) { | ||
println!("AsyncInt::async_drop: {}", self.0); | ||
} | ||
} | ||
|
||
struct SyncInt(i32); | ||
|
||
impl Drop for SyncInt { | ||
fn drop(&mut self) { | ||
println!("SyncInt::drop: {}", self.0); | ||
} | ||
} | ||
|
||
struct SyncThenAsync { | ||
i: i32, | ||
a: AsyncInt, | ||
b: SyncInt, | ||
c: AsyncInt, | ||
} | ||
|
||
impl Drop for SyncThenAsync { | ||
fn drop(&mut self) { | ||
println!("SyncThenAsync::drop: {}", self.i); | ||
} | ||
} | ||
|
||
struct AsyncReference<'a> { | ||
foo: &'a AsyncInt, | ||
} | ||
|
||
impl Drop for AsyncReference<'_> { | ||
fn drop(&mut self) { | ||
println!("AsyncReference::drop: {}", self.foo.0); | ||
} | ||
} | ||
impl AsyncDrop for AsyncReference<'_> { | ||
async fn drop(self: Pin<&mut Self>) { | ||
println!("AsyncReference::async_drop: {}", self.foo.0); | ||
} | ||
} | ||
|
||
struct Int(i32); | ||
|
||
struct AsyncStruct { | ||
i: i32, | ||
a: AsyncInt, | ||
b: AsyncInt, | ||
} | ||
|
||
impl Drop for AsyncStruct { | ||
fn drop(&mut self) { | ||
println!("AsyncStruct::drop: {}", self.i); | ||
} | ||
} | ||
impl AsyncDrop for AsyncStruct { | ||
async fn drop(self: Pin<&mut Self>) { | ||
println!("AsyncStruct::async_drop: {}", self.i); | ||
} | ||
} | ||
|
||
enum AsyncEnum { | ||
A(AsyncInt), | ||
B(SyncInt), | ||
} | ||
|
||
impl Drop for AsyncEnum { | ||
fn drop(&mut self) { | ||
let new_self = match self { | ||
AsyncEnum::A(foo) => { | ||
println!("AsyncEnum(A)::drop: {}", foo.0); | ||
AsyncEnum::B(SyncInt(foo.0)) | ||
} | ||
AsyncEnum::B(foo) => { | ||
println!("AsyncEnum(B)::drop: {}", foo.0); | ||
AsyncEnum::A(AsyncInt(foo.0)) | ||
} | ||
}; | ||
mem::forget(mem::replace(&mut *self, new_self)); | ||
} | ||
} | ||
impl AsyncDrop for AsyncEnum { | ||
async fn drop(mut self: Pin<&mut Self>) { | ||
let new_self = match &*self { | ||
AsyncEnum::A(foo) => { | ||
println!("AsyncEnum(A)::async_drop: {}", foo.0); | ||
AsyncEnum::B(SyncInt(foo.0)) | ||
} | ||
AsyncEnum::B(foo) => { | ||
println!("AsyncEnum(B)::async_drop: {}", foo.0); | ||
AsyncEnum::A(AsyncInt(foo.0)) | ||
} | ||
}; | ||
mem::forget(mem::replace(&mut *self, new_self)); | ||
} | ||
} | ||
|
||
// FIXME(zetanumbers): Disallow types with `AsyncDrop` in unions | ||
union AsyncUnion { | ||
signed: i32, | ||
unsigned: u32, | ||
} | ||
|
||
impl Drop for AsyncUnion { | ||
fn drop(&mut self) { | ||
println!( | ||
"AsyncUnion::drop: {}, {}", | ||
unsafe { self.signed }, | ||
unsafe { self.unsigned }, | ||
); | ||
} | ||
} | ||
impl AsyncDrop for AsyncUnion { | ||
async fn drop(self: Pin<&mut Self>) { | ||
println!("AsyncUnion::async_drop: {}, {}", unsafe { self.signed }, unsafe { | ||
self.unsigned | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
AsyncInt::Dropper::poll: 0 | ||
AsyncInt::Dropper::poll: 1 | ||
AsyncInt::Dropper::poll: 2 | ||
AsyncInt::Dropper::poll: 3 | ||
AsyncInt::Dropper::poll: 4 | ||
AsyncStruct::Dropper::poll: 6 | ||
AsyncInt::Dropper::poll: 7 | ||
AsyncInt::Dropper::poll: 8 | ||
AsyncReference::Dropper::poll: 10 | ||
AsyncInt::Dropper::poll: 11 | ||
AsyncEnum(A)::Dropper::poll: 12 | ||
AsyncInt::async_drop: 0 | ||
AsyncInt::async_drop: 1 | ||
AsyncInt::async_drop: 2 | ||
AsyncInt::async_drop: 3 | ||
AsyncInt::async_drop: 4 | ||
AsyncStruct::async_drop: 6 | ||
AsyncInt::async_drop: 7 | ||
AsyncInt::async_drop: 8 | ||
AsyncReference::async_drop: 10 | ||
AsyncInt::async_drop: 11 | ||
AsyncEnum(A)::async_drop: 12 | ||
SyncInt::drop: 12 | ||
AsyncEnum(B)::Dropper::poll: 13 | ||
AsyncInt::Dropper::poll: 13 | ||
AsyncEnum(B)::async_drop: 13 | ||
AsyncInt::async_drop: 13 | ||
SyncInt::drop: 14 | ||
SyncThenAsync::drop: 15 | ||
AsyncInt::Dropper::poll: 16 | ||
AsyncInt::async_drop: 16 | ||
SyncInt::drop: 17 | ||
AsyncInt::Dropper::poll: 18 | ||
AsyncInt::Dropper::poll: 19 | ||
AsyncInt::Dropper::poll: 20 | ||
AsyncUnion::Dropper::poll: 21, 21 | ||
AsyncInt::async_drop: 18 | ||
AsyncInt::async_drop: 19 | ||
AsyncInt::async_drop: 20 | ||
AsyncUnion::async_drop: 21, 21 | ||
AsyncInt::async_drop: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
AsyncInt::Dropper::poll: 0 | ||
AsyncInt::Dropper::poll: 1 | ||
AsyncInt::Dropper::poll: 2 | ||
AsyncInt::Dropper::poll: 3 | ||
AsyncInt::Dropper::poll: 4 | ||
AsyncStruct::Dropper::poll: 6 | ||
AsyncInt::Dropper::poll: 7 | ||
AsyncInt::Dropper::poll: 8 | ||
AsyncReference::Dropper::poll: 10 | ||
AsyncInt::Dropper::poll: 11 | ||
AsyncEnum(A)::Dropper::poll: 12 | ||
AsyncInt::async_drop: 0 | ||
AsyncInt::async_drop: 1 | ||
AsyncInt::async_drop: 2 | ||
AsyncInt::async_drop: 3 | ||
AsyncInt::async_drop: 4 | ||
AsyncStruct::async_drop: 6 | ||
AsyncInt::async_drop: 7 | ||
AsyncInt::async_drop: 8 | ||
AsyncReference::async_drop: 10 | ||
AsyncInt::async_drop: 11 | ||
AsyncEnum(A)::async_drop: 12 | ||
SyncInt::drop: 12 | ||
AsyncEnum(B)::Dropper::poll: 13 | ||
AsyncInt::Dropper::poll: 13 | ||
AsyncEnum(B)::async_drop: 13 | ||
AsyncInt::async_drop: 13 | ||
SyncInt::drop: 14 | ||
SyncThenAsync::drop: 15 | ||
AsyncInt::Dropper::poll: 16 | ||
AsyncInt::async_drop: 16 | ||
SyncInt::drop: 17 | ||
AsyncInt::Dropper::poll: 18 | ||
AsyncInt::Dropper::poll: 19 | ||
AsyncInt::Dropper::poll: 20 | ||
AsyncUnion::Dropper::poll: 21, 21 | ||
AsyncInt::async_drop: 18 | ||
AsyncInt::async_drop: 19 | ||
AsyncInt::async_drop: 20 | ||
AsyncUnion::async_drop: 21, 21 | ||
AsyncInt::async_drop: 10 |
Oops, something went wrong.