1 // aux-build:arc_wake.rs
5 #![allow(unused_variables)]
6 #![feature(async_await)]
8 // Test that the drop order for parameters in a fn and async fn matches up. Also test that
9 // parameters (used or unused) are not dropped until the async fn is cancelled.
10 // This file is mostly copy-pasted from drop-order-for-async-fn-parameters.rs
12 extern crate arc_wake;
14 use arc_wake::ArcWake;
15 use std::cell::RefCell;
16 use std::future::Future;
17 use std::marker::PhantomData;
21 use std::task::{Context, Poll};
25 impl ArcWake for EmptyWaker {
26 fn wake(self: Arc<Self>) {}
29 #[derive(Debug, Eq, PartialEq)]
35 type DropOrderListPtr = Rc<RefCell<Vec<DropOrder>>>;
37 struct D(&'static str, DropOrderListPtr);
41 self.1.borrow_mut().push(DropOrder::Val(self.0));
47 impl Future for NeverReady {
49 fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
54 /// Check that unused bindings are dropped after the function is polled.
55 async fn foo_async(x: D, _y: D) {
56 x.1.borrow_mut().push(DropOrder::Function);
60 fn foo_sync(x: D, _y: D) {
61 x.1.borrow_mut().push(DropOrder::Function);
64 /// Check that underscore patterns are dropped after the function is polled.
65 async fn bar_async(x: D, _: D) {
66 x.1.borrow_mut().push(DropOrder::Function);
70 fn bar_sync(x: D, _: D) {
71 x.1.borrow_mut().push(DropOrder::Function);
74 /// Check that underscore patterns within more complex patterns are dropped after the function
76 async fn baz_async((x, _): (D, D)) {
77 x.1.borrow_mut().push(DropOrder::Function);
81 fn baz_sync((x, _): (D, D)) {
82 x.1.borrow_mut().push(DropOrder::Function);
85 /// Check that underscore and unused bindings within and outwith more complex patterns are dropped
86 /// after the function is polled.
87 async fn foobar_async(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
88 x.1.borrow_mut().push(DropOrder::Function);
92 fn foobar_sync(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
93 x.1.borrow_mut().push(DropOrder::Function);
99 /// Check that unused bindings are dropped after the method is polled.
100 async fn foo_async(x: D, _y: D) {
101 x.1.borrow_mut().push(DropOrder::Function);
105 fn foo_sync(x: D, _y: D) {
106 x.1.borrow_mut().push(DropOrder::Function);
109 /// Check that underscore patterns are dropped after the method is polled.
110 async fn bar_async(x: D, _: D) {
111 x.1.borrow_mut().push(DropOrder::Function);
115 fn bar_sync(x: D, _: D) {
116 x.1.borrow_mut().push(DropOrder::Function);
119 /// Check that underscore patterns within more complex patterns are dropped after the method
121 async fn baz_async((x, _): (D, D)) {
122 x.1.borrow_mut().push(DropOrder::Function);
126 fn baz_sync((x, _): (D, D)) {
127 x.1.borrow_mut().push(DropOrder::Function);
130 /// Check that underscore and unused bindings within and outwith more complex patterns are
131 /// dropped after the method is polled.
132 async fn foobar_async(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
133 x.1.borrow_mut().push(DropOrder::Function);
137 fn foobar_sync(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
138 x.1.borrow_mut().push(DropOrder::Function);
142 struct Bar<'a>(PhantomData<&'a ()>);
145 /// Check that unused bindings are dropped after the method with self is polled.
146 async fn foo_async(&'a self, x: D, _y: D) {
147 x.1.borrow_mut().push(DropOrder::Function);
151 fn foo_sync(&'a self, x: D, _y: D) {
152 x.1.borrow_mut().push(DropOrder::Function);
155 /// Check that underscore patterns are dropped after the method with self is polled.
156 async fn bar_async(&'a self, x: D, _: D) {
157 x.1.borrow_mut().push(DropOrder::Function);
161 fn bar_sync(&'a self, x: D, _: D) {
162 x.1.borrow_mut().push(DropOrder::Function);
165 /// Check that underscore patterns within more complex patterns are dropped after the method
166 /// with self is polled.
167 async fn baz_async(&'a self, (x, _): (D, D)) {
168 x.1.borrow_mut().push(DropOrder::Function);
172 fn baz_sync(&'a self, (x, _): (D, D)) {
173 x.1.borrow_mut().push(DropOrder::Function);
176 /// Check that underscore and unused bindings within and outwith more complex patterns are
177 /// dropped after the method with self is polled.
178 async fn foobar_async(&'a self, x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
179 x.1.borrow_mut().push(DropOrder::Function);
183 fn foobar_sync(&'a self, x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
184 x.1.borrow_mut().push(DropOrder::Function);
188 fn assert_drop_order_after_cancel<Fut: Future<Output = ()>>(
189 f: impl FnOnce(DropOrderListPtr) -> Fut,
190 g: impl FnOnce(DropOrderListPtr),
192 let empty = Arc::new(EmptyWaker);
193 let waker = ArcWake::into_waker(empty);
194 let mut cx = Context::from_waker(&waker);
196 let actual_order = Rc::new(RefCell::new(Vec::new()));
197 let mut fut = Box::pin(f(actual_order.clone()));
198 let _ = fut.as_mut().poll(&mut cx);
200 // Parameters are never dropped until the future completes.
201 assert_eq!(*actual_order.borrow(), vec![DropOrder::Function]);
205 let expected_order = Rc::new(RefCell::new(Vec::new()));
206 g(expected_order.clone());
207 assert_eq!(*actual_order.borrow(), *expected_order.borrow());
211 // Free functions (see doc comment on function for what it tests).
212 assert_drop_order_after_cancel(
213 |l| foo_async(D("x", l.clone()), D("_y", l.clone())),
214 |l| foo_sync(D("x", l.clone()), D("_y", l.clone())),
216 assert_drop_order_after_cancel(
217 |l| bar_async(D("x", l.clone()), D("_", l.clone())),
218 |l| bar_sync(D("x", l.clone()), D("_", l.clone())),
220 assert_drop_order_after_cancel(
221 |l| baz_async((D("x", l.clone()), D("_", l.clone()))),
222 |l| baz_sync((D("x", l.clone()), D("_", l.clone()))),
224 assert_drop_order_after_cancel(
228 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
236 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
243 // Methods w/out self (see doc comment on function for what it tests).
244 assert_drop_order_after_cancel(
245 |l| Foo::foo_async(D("x", l.clone()), D("_y", l.clone())),
246 |l| Foo::foo_sync(D("x", l.clone()), D("_y", l.clone())),
248 assert_drop_order_after_cancel(
249 |l| Foo::bar_async(D("x", l.clone()), D("_", l.clone())),
250 |l| Foo::bar_sync(D("x", l.clone()), D("_", l.clone())),
252 assert_drop_order_after_cancel(
253 |l| Foo::baz_async((D("x", l.clone()), D("_", l.clone()))),
254 |l| Foo::baz_sync((D("x", l.clone()), D("_", l.clone()))),
256 assert_drop_order_after_cancel(
260 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
268 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
275 // Methods (see doc comment on function for what it tests).
276 let b = Bar(Default::default());
277 assert_drop_order_after_cancel(
278 |l| b.foo_async(D("x", l.clone()), D("_y", l.clone())),
279 |l| b.foo_sync(D("x", l.clone()), D("_y", l.clone())),
281 assert_drop_order_after_cancel(
282 |l| b.bar_async(D("x", l.clone()), D("_", l.clone())),
283 |l| b.bar_sync(D("x", l.clone()), D("_", l.clone())),
285 assert_drop_order_after_cancel(
286 |l| b.baz_async((D("x", l.clone()), D("_", l.clone()))),
287 |l| b.baz_sync((D("x", l.clone()), D("_", l.clone()))),
289 assert_drop_order_after_cancel(
293 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
301 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),