Add size check inside of the async drop tests

This commit is contained in:
Daria Sukhonina 2024-05-24 17:20:49 +03:00
parent 5870f1ccbb
commit e0904cd6a9
2 changed files with 102 additions and 62 deletions

View File

@ -11,9 +11,21 @@ use core::mem::{self, ManuallyDrop};
use core::pin::{pin, Pin}; use core::pin::{pin, Pin};
use core::task::{Context, Poll, Waker}; use core::task::{Context, Poll, Waker};
async fn test_async_drop<T>(x: T) { async fn test_async_drop<T>(x: T, _size: usize) {
let mut x = mem::MaybeUninit::new(x); let mut x = mem::MaybeUninit::new(x);
let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) }); let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) });
// FIXME(zetanumbers): This check fully depends on the layout of
// the coroutine state, since async destructor combinators are just
// async functions.
#[cfg(target_pointer_width = "64")]
assert_eq!(
mem::size_of_val(&*dtor),
_size,
"sizes did not match for async destructor of type {}",
core::any::type_name::<T>(),
);
test_idempotency(dtor).await; test_idempotency(dtor).await;
} }
@ -34,49 +46,58 @@ fn main() {
let i = 13; let i = 13;
let fut = pin!(async { let fut = pin!(async {
test_async_drop(Int(0)).await; test_async_drop(Int(0), 16).await;
test_async_drop(AsyncInt(0)).await; test_async_drop(AsyncInt(0), 104).await;
test_async_drop([AsyncInt(1), AsyncInt(2)]).await; test_async_drop([AsyncInt(1), AsyncInt(2)], 152).await;
test_async_drop((AsyncInt(3), AsyncInt(4))).await; test_async_drop((AsyncInt(3), AsyncInt(4)), 488).await;
test_async_drop(5).await; test_async_drop(5, 0).await;
let j = 42; let j = 42;
test_async_drop(&i).await; test_async_drop(&i, 0).await;
test_async_drop(&j).await; test_async_drop(&j, 0).await;
test_async_drop(AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 }).await; test_async_drop(AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 }, 1688).await;
test_async_drop(ManuallyDrop::new(AsyncInt(9))).await; test_async_drop(ManuallyDrop::new(AsyncInt(9)), 0).await;
let foo = AsyncInt(10); let foo = AsyncInt(10);
test_async_drop(AsyncReference { foo: &foo }).await; test_async_drop(AsyncReference { foo: &foo }, 104).await;
let foo = AsyncInt(11); let foo = AsyncInt(11);
test_async_drop(|| { test_async_drop(
black_box(foo); || {
let foo = AsyncInt(10); black_box(foo);
foo let foo = AsyncInt(10);
}) foo
},
120,
)
.await; .await;
test_async_drop(AsyncEnum::A(AsyncInt(12))).await; test_async_drop(AsyncEnum::A(AsyncInt(12)), 792).await;
test_async_drop(AsyncEnum::B(SyncInt(13))).await; test_async_drop(AsyncEnum::B(SyncInt(13)), 792).await;
test_async_drop(SyncInt(14)).await; test_async_drop(SyncInt(14), 72).await;
test_async_drop(SyncThenAsync { i: 15, a: AsyncInt(16), b: SyncInt(17), c: AsyncInt(18) }) test_async_drop(
.await; SyncThenAsync { i: 15, a: AsyncInt(16), b: SyncInt(17), c: AsyncInt(18) },
3512,
)
.await;
let async_drop_fut = pin!(core::future::async_drop(AsyncInt(19))); let async_drop_fut = pin!(core::future::async_drop(AsyncInt(19)));
test_idempotency(async_drop_fut).await; test_idempotency(async_drop_fut).await;
let foo = AsyncInt(20); let foo = AsyncInt(20);
test_async_drop(async || { test_async_drop(
black_box(foo); async || {
let foo = AsyncInt(19); black_box(foo);
// Await point there, but this is async closure so it's fine let foo = AsyncInt(19);
black_box(core::future::ready(())).await; // Await point there, but this is async closure so it's fine
foo black_box(core::future::ready(())).await;
}) foo
},
120,
)
.await; .await;
test_async_drop(AsyncUnion { signed: 21 }).await; test_async_drop(AsyncUnion { signed: 21 }, 32).await;
}); });
let res = fut.poll(&mut cx); let res = fut.poll(&mut cx);
assert_eq!(res, Poll::Ready(())); assert_eq!(res, Poll::Ready(()));

View File

@ -13,9 +13,21 @@ use core::mem::{self, ManuallyDrop};
use core::pin::{pin, Pin}; use core::pin::{pin, Pin};
use core::task::{Context, Poll, Waker}; use core::task::{Context, Poll, Waker};
async fn test_async_drop<T>(x: T) { async fn test_async_drop<T>(x: T, _size: usize) {
let mut x = mem::MaybeUninit::new(x); let mut x = mem::MaybeUninit::new(x);
let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) }); let dtor = pin!(unsafe { async_drop_in_place(x.as_mut_ptr()) });
// FIXME(zetanumbers): This check fully depends on the layout of
// the coroutine state, since async destructor combinators are just
// async functions.
#[cfg(target_pointer_width = "64")]
assert_eq!(
mem::size_of_val(&*dtor),
_size,
"sizes did not match for async destructor of type {}",
core::any::type_name::<T>(),
);
test_idempotency(dtor).await; test_idempotency(dtor).await;
} }
@ -36,51 +48,58 @@ fn main() {
let i = 13; let i = 13;
let fut = pin!(async { let fut = pin!(async {
test_async_drop(Int(0)).await; test_async_drop(Int(0), 16).await;
test_async_drop(AsyncInt(0)).await; test_async_drop(AsyncInt(0), 104).await;
test_async_drop([AsyncInt(1), AsyncInt(2)]).await; test_async_drop([AsyncInt(1), AsyncInt(2)], 152).await;
test_async_drop((AsyncInt(3), AsyncInt(4))).await; test_async_drop((AsyncInt(3), AsyncInt(4)), 488).await;
test_async_drop(5).await; test_async_drop(5, 0).await;
let j = 42; let j = 42;
test_async_drop(&i).await; test_async_drop(&i, 0).await;
test_async_drop(&j).await; test_async_drop(&j, 0).await;
test_async_drop(AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 }).await; test_async_drop(AsyncStruct { b: AsyncInt(8), a: AsyncInt(7), i: 6 }, 1688).await;
test_async_drop(ManuallyDrop::new(AsyncInt(9))).await; test_async_drop(ManuallyDrop::new(AsyncInt(9)), 0).await;
let foo = AsyncInt(10); let foo = AsyncInt(10);
test_async_drop(AsyncReference { foo: &foo }).await; test_async_drop(AsyncReference { foo: &foo }, 104).await;
let foo = AsyncInt(11); let foo = AsyncInt(11);
test_async_drop(|| { test_async_drop(
black_box(foo); || {
let foo = AsyncInt(10); black_box(foo);
foo let foo = AsyncInt(10);
}).await; foo
},
120,
)
.await;
test_async_drop(AsyncEnum::A(AsyncInt(12))).await; test_async_drop(AsyncEnum::A(AsyncInt(12)), 792).await;
test_async_drop(AsyncEnum::B(SyncInt(13))).await; test_async_drop(AsyncEnum::B(SyncInt(13)), 792).await;
test_async_drop(SyncInt(14)).await; test_async_drop(SyncInt(14), 72).await;
test_async_drop(SyncThenAsync { test_async_drop(
i: 15, SyncThenAsync { i: 15, a: AsyncInt(16), b: SyncInt(17), c: AsyncInt(18) },
a: AsyncInt(16), 3512,
b: SyncInt(17), )
c: AsyncInt(18), .await;
}).await;
let async_drop_fut = pin!(core::future::async_drop(AsyncInt(19))); let async_drop_fut = pin!(core::future::async_drop(AsyncInt(19)));
test_idempotency(async_drop_fut).await; test_idempotency(async_drop_fut).await;
let foo = AsyncInt(20); let foo = AsyncInt(20);
test_async_drop(async || { test_async_drop(
black_box(foo); async || {
let foo = AsyncInt(19); black_box(foo);
// Await point there, but this is async closure so it's fine let foo = AsyncInt(19);
black_box(core::future::ready(())).await; // Await point there, but this is async closure so it's fine
foo black_box(core::future::ready(())).await;
}).await; foo
},
120,
)
.await;
test_async_drop(AsyncUnion { signed: 21 }).await; test_async_drop(AsyncUnion { signed: 21 }, 32).await;
}); });
let res = fut.poll(&mut cx); let res = fut.poll(&mut cx);
assert_eq!(res, Poll::Ready(())); assert_eq!(res, Poll::Ready(()));