|
| 1 | +//@revisions: stack tree |
| 2 | +//@compile-flags: -Zmiri-strict-provenance |
| 3 | +//@[tree]compile-flags: -Zmiri-tree-borrows |
| 4 | + |
| 5 | +// WARNING: If you would ever want to modify this test, |
| 6 | +// please consider modifying rustc's async drop test at |
| 7 | +// `tests/ui/async-await/async-drop/async-drop-initial.rs`. |
| 8 | + |
| 9 | +#![feature(async_drop, impl_trait_in_assoc_type, noop_waker, async_closure)] |
| 10 | +#![allow(incomplete_features, dead_code)] |
| 11 | + |
| 12 | +// FIXME(zetanumbers): consider AsyncDestruct::async_drop cleanup tests |
| 13 | +use core::future::{async_drop_in_place, AsyncDrop, Future}; |
| 14 | +use core::hint::black_box; |
| 15 | +use core::mem::{self, ManuallyDrop}; |
| 16 | +use core::pin::{pin, Pin}; |
| 17 | +use core::task::{Context, Poll, Waker}; |
| 18 | + |
| 19 | +async fn test_async_drop<T>(x: T) { |
| 20 | + let mut x = mem::MaybeUninit::new(x); |
| 21 | + let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) }); |
| 22 | + test_idempotency(dtor).await; |
| 23 | +} |
| 24 | + |
| 25 | +fn test_idempotency<T>(mut x: Pin<&mut T>) -> impl Future<Output = ()> + '_ |
| 26 | +where |
| 27 | + T: Future<Output = ()>, |
| 28 | +{ |
| 29 | + core::future::poll_fn(move |cx| { |
| 30 | + assert_eq!(x.as_mut().poll(cx), Poll::Ready(())); |
| 31 | + assert_eq!(x.as_mut().poll(cx), Poll::Ready(())); |
| 32 | + Poll::Ready(()) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +fn main() { |
| 37 | + let waker = Waker::noop(); |
| 38 | + let mut cx = Context::from_waker(&waker); |
| 39 | + |
| 40 | + let i = 13; |
| 41 | + let fut = pin!(async { |
| 42 | + test_async_drop(Int(0)).await; |
| 43 | + test_async_drop(AsyncInt(0)).await; |
| 44 | + test_async_drop([AsyncInt(1), AsyncInt(2)]).await; |
| 45 | + test_async_drop((AsyncInt(3), AsyncInt(4))).await; |
| 46 | + test_async_drop(5).await; |
| 47 | + let j = 42; |
| 48 | + test_async_drop(&i).await; |
| 49 | + test_async_drop(&j).await; |
| 50 | + test_async_drop(AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 }).await; |
| 51 | + test_async_drop(ManuallyDrop::new(AsyncInt(9))).await; |
| 52 | + |
| 53 | + let foo = AsyncInt(10); |
| 54 | + test_async_drop(AsyncReference { foo: &foo }).await; |
| 55 | + |
| 56 | + let foo = AsyncInt(11); |
| 57 | + test_async_drop(|| { |
| 58 | + black_box(foo); |
| 59 | + let foo = AsyncInt(10); |
| 60 | + foo |
| 61 | + }) |
| 62 | + .await; |
| 63 | + |
| 64 | + test_async_drop(AsyncEnum::A(AsyncInt(12))).await; |
| 65 | + test_async_drop(AsyncEnum::B(SyncInt(13))).await; |
| 66 | + |
| 67 | + test_async_drop(SyncInt(14)).await; |
| 68 | + test_async_drop(SyncThenAsync { i: 15, a: AsyncInt(16), b: SyncInt(17), c: AsyncInt(18) }) |
| 69 | + .await; |
| 70 | + |
| 71 | + let mut ptr19 = mem::MaybeUninit::new(AsyncInt(19)); |
| 72 | + let async_drop_fut = pin!(unsafe { async_drop_in_place(ptr19.as_mut_ptr()) }); |
| 73 | + test_idempotency(async_drop_fut).await; |
| 74 | + |
| 75 | + let foo = AsyncInt(20); |
| 76 | + test_async_drop(async || { |
| 77 | + black_box(foo); |
| 78 | + let foo = AsyncInt(19); |
| 79 | + // Await point there, but this is async closure so it's fine |
| 80 | + black_box(core::future::ready(())).await; |
| 81 | + foo |
| 82 | + }) |
| 83 | + .await; |
| 84 | + |
| 85 | + test_async_drop(AsyncUnion { signed: 21 }).await; |
| 86 | + }); |
| 87 | + let res = fut.poll(&mut cx); |
| 88 | + assert_eq!(res, Poll::Ready(())); |
| 89 | +} |
| 90 | + |
| 91 | +struct AsyncInt(i32); |
| 92 | + |
| 93 | +impl Drop for AsyncInt { |
| 94 | + fn drop(&mut self) { |
| 95 | + println!("AsyncInt::drop: {}", self.0); |
| 96 | + } |
| 97 | +} |
| 98 | +impl AsyncDrop for AsyncInt { |
| 99 | + async fn drop(self: Pin<&mut Self>) { |
| 100 | + println!("AsyncInt::async_drop: {}", self.0); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +struct SyncInt(i32); |
| 105 | + |
| 106 | +impl Drop for SyncInt { |
| 107 | + fn drop(&mut self) { |
| 108 | + println!("SyncInt::drop: {}", self.0); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +struct SyncThenAsync { |
| 113 | + i: i32, |
| 114 | + a: AsyncInt, |
| 115 | + b: SyncInt, |
| 116 | + c: AsyncInt, |
| 117 | +} |
| 118 | + |
| 119 | +impl Drop for SyncThenAsync { |
| 120 | + fn drop(&mut self) { |
| 121 | + println!("SyncThenAsync::drop: {}", self.i); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +struct AsyncReference<'a> { |
| 126 | + foo: &'a AsyncInt, |
| 127 | +} |
| 128 | + |
| 129 | +impl Drop for AsyncReference<'_> { |
| 130 | + fn drop(&mut self) { |
| 131 | + println!("AsyncReference::drop: {}", self.foo.0); |
| 132 | + } |
| 133 | +} |
| 134 | +impl AsyncDrop for AsyncReference<'_> { |
| 135 | + async fn drop(self: Pin<&mut Self>) { |
| 136 | + println!("AsyncReference::async_drop: {}", self.foo.0); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +struct Int(i32); |
| 141 | + |
| 142 | +struct AsyncStruct { |
| 143 | + i: i32, |
| 144 | + a: AsyncInt, |
| 145 | + b: AsyncInt, |
| 146 | +} |
| 147 | + |
| 148 | +impl Drop for AsyncStruct { |
| 149 | + fn drop(&mut self) { |
| 150 | + println!("AsyncStruct::drop: {}", self.i); |
| 151 | + } |
| 152 | +} |
| 153 | +impl AsyncDrop for AsyncStruct { |
| 154 | + async fn drop(self: Pin<&mut Self>) { |
| 155 | + println!("AsyncStruct::async_drop: {}", self.i); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +enum AsyncEnum { |
| 160 | + A(AsyncInt), |
| 161 | + B(SyncInt), |
| 162 | +} |
| 163 | + |
| 164 | +impl Drop for AsyncEnum { |
| 165 | + fn drop(&mut self) { |
| 166 | + let new_self = match self { |
| 167 | + AsyncEnum::A(foo) => { |
| 168 | + println!("AsyncEnum(A)::drop: {}", foo.0); |
| 169 | + AsyncEnum::B(SyncInt(foo.0)) |
| 170 | + } |
| 171 | + AsyncEnum::B(foo) => { |
| 172 | + println!("AsyncEnum(B)::drop: {}", foo.0); |
| 173 | + AsyncEnum::A(AsyncInt(foo.0)) |
| 174 | + } |
| 175 | + }; |
| 176 | + mem::forget(mem::replace(&mut *self, new_self)); |
| 177 | + } |
| 178 | +} |
| 179 | +impl AsyncDrop for AsyncEnum { |
| 180 | + async fn drop(mut self: Pin<&mut Self>) { |
| 181 | + let new_self = match &*self { |
| 182 | + AsyncEnum::A(foo) => { |
| 183 | + println!("AsyncEnum(A)::async_drop: {}", foo.0); |
| 184 | + AsyncEnum::B(SyncInt(foo.0)) |
| 185 | + } |
| 186 | + AsyncEnum::B(foo) => { |
| 187 | + println!("AsyncEnum(B)::async_drop: {}", foo.0); |
| 188 | + AsyncEnum::A(AsyncInt(foo.0)) |
| 189 | + } |
| 190 | + }; |
| 191 | + mem::forget(mem::replace(&mut *self, new_self)); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +// FIXME(zetanumbers): Disallow types with `AsyncDrop` in unions |
| 196 | +union AsyncUnion { |
| 197 | + signed: i32, |
| 198 | + unsigned: u32, |
| 199 | +} |
| 200 | + |
| 201 | +impl Drop for AsyncUnion { |
| 202 | + fn drop(&mut self) { |
| 203 | + println!( |
| 204 | + "AsyncUnion::drop: {}, {}", |
| 205 | + unsafe { self.signed }, |
| 206 | + unsafe { self.unsigned }, |
| 207 | + ); |
| 208 | + } |
| 209 | +} |
| 210 | +impl AsyncDrop for AsyncUnion { |
| 211 | + async fn drop(self: Pin<&mut Self>) { |
| 212 | + println!("AsyncUnion::async_drop: {}, {}", unsafe { self.signed }, unsafe { |
| 213 | + self.unsigned |
| 214 | + }); |
| 215 | + } |
| 216 | +} |
0 commit comments