|
| 1 | +// aux-build:arc_wake.rs |
| 2 | +// edition:2018 |
| 3 | +// run-pass |
| 4 | + |
| 5 | +#![allow(unused_variables)] |
| 6 | + |
| 7 | +// Test that the drop order for parameters in a fn and async fn matches up. Also test that |
| 8 | +// parameters (used or unused) are not dropped until the async fn completes execution. |
| 9 | +// See also #54716. |
| 10 | + |
| 11 | +extern crate arc_wake; |
| 12 | + |
| 13 | +use arc_wake::ArcWake; |
| 14 | +use std::cell::RefCell; |
| 15 | +use std::future::Future; |
| 16 | +use std::sync::Arc; |
| 17 | +use std::rc::Rc; |
| 18 | +use std::task::Context; |
| 19 | + |
| 20 | +struct EmptyWaker; |
| 21 | + |
| 22 | +impl ArcWake for EmptyWaker { |
| 23 | + fn wake(self: Arc<Self>) {} |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Debug, Eq, PartialEq)] |
| 27 | +enum DropOrder { |
| 28 | + Function, |
| 29 | + Val(&'static str), |
| 30 | +} |
| 31 | + |
| 32 | +type DropOrderListPtr = Rc<RefCell<Vec<DropOrder>>>; |
| 33 | + |
| 34 | +struct D(&'static str, DropOrderListPtr); |
| 35 | + |
| 36 | +impl Drop for D { |
| 37 | + fn drop(&mut self) { |
| 38 | + self.1.borrow_mut().push(DropOrder::Val(self.0)); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Check drop order of temporary "temp" as compared to x, y, and z. |
| 43 | +/// |
| 44 | +/// Expected order: |
| 45 | +/// - z |
| 46 | +/// - temp |
| 47 | +/// - y |
| 48 | +/// - x |
| 49 | +async fn foo_async(x: D, _y: D) { |
| 50 | + let l = x.1.clone(); |
| 51 | + let z = D("z", l.clone()); |
| 52 | + l.borrow_mut().push(DropOrder::Function); |
| 53 | + helper_async(&D("temp", l)).await |
| 54 | +} |
| 55 | + |
| 56 | +async fn helper_async(v: &D) { } |
| 57 | + |
| 58 | +fn foo_sync(x: D, _y: D) { |
| 59 | + let l = x.1.clone(); |
| 60 | + let z = D("z", l.clone()); |
| 61 | + l.borrow_mut().push(DropOrder::Function); |
| 62 | + helper_sync(&D("temp", l)) |
| 63 | +} |
| 64 | + |
| 65 | +fn helper_sync(v: &D) { } |
| 66 | + |
| 67 | +fn assert_drop_order_after_poll<Fut: Future<Output = ()>>( |
| 68 | + f: impl FnOnce(DropOrderListPtr) -> Fut, |
| 69 | + g: impl FnOnce(DropOrderListPtr), |
| 70 | +) { |
| 71 | + let empty = Arc::new(EmptyWaker); |
| 72 | + let waker = ArcWake::into_waker(empty); |
| 73 | + let mut cx = Context::from_waker(&waker); |
| 74 | + |
| 75 | + let actual_order = Rc::new(RefCell::new(Vec::new())); |
| 76 | + let mut fut = Box::pin(f(actual_order.clone())); |
| 77 | + let r = fut.as_mut().poll(&mut cx); |
| 78 | + |
| 79 | + assert!(match r { |
| 80 | + std::task::Poll::Ready(()) => true, |
| 81 | + _ => false, |
| 82 | + }); |
| 83 | + |
| 84 | + let expected_order = Rc::new(RefCell::new(Vec::new())); |
| 85 | + g(expected_order.clone()); |
| 86 | + |
| 87 | + assert_eq!(*actual_order.borrow(), *expected_order.borrow()); |
| 88 | +} |
| 89 | + |
| 90 | +fn main() { |
| 91 | + // Free functions (see doc comment on function for what it tests). |
| 92 | + assert_drop_order_after_poll(|l| foo_async(D("x", l.clone()), D("_y", l.clone())), |
| 93 | + |l| foo_sync(D("x", l.clone()), D("_y", l.clone()))); |
| 94 | +} |
0 commit comments