1 // aux-build:arc_wake.rs
5 // revisions: default nomiropt
6 //[nomiropt]compile-flags: -Z mir-opt-level=0
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 #![allow(unused_variables)]
14 extern crate arc_wake;
16 use arc_wake::ArcWake;
17 use std::cell::RefCell;
18 use std::future::Future;
19 use std::marker::PhantomData;
23 use std::task::{Context, Poll};
27 impl ArcWake for EmptyWaker {
28 fn wake(self: Arc<Self>) {}
31 #[derive(Debug, Eq, PartialEq)]
37 type DropOrderListPtr = Rc<RefCell<Vec<DropOrder>>>;
39 struct D(&'static str, DropOrderListPtr);
43 self.1.borrow_mut().push(DropOrder::Val(self.0));
49 impl Future for NeverReady {
51 fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
56 /// Check that unused bindings are dropped after the function is polled.
57 async fn foo_async(x: D, _y: D) {
58 x.1.borrow_mut().push(DropOrder::Function);
62 fn foo_sync(x: D, _y: D) {
63 x.1.borrow_mut().push(DropOrder::Function);
66 /// Check that underscore patterns are dropped after the function is polled.
67 async fn bar_async(x: D, _: D) {
68 x.1.borrow_mut().push(DropOrder::Function);
72 fn bar_sync(x: D, _: D) {
73 x.1.borrow_mut().push(DropOrder::Function);
76 /// Check that underscore patterns within more complex patterns are dropped after the function
78 async fn baz_async((x, _): (D, D)) {
79 x.1.borrow_mut().push(DropOrder::Function);
83 fn baz_sync((x, _): (D, D)) {
84 x.1.borrow_mut().push(DropOrder::Function);
87 /// Check that underscore and unused bindings within and outwith more complex patterns are dropped
88 /// after the function is polled.
89 async fn foobar_async(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
90 x.1.borrow_mut().push(DropOrder::Function);
94 fn foobar_sync(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
95 x.1.borrow_mut().push(DropOrder::Function);
101 /// Check that unused bindings are dropped after the method is polled.
102 async fn foo_async(x: D, _y: D) {
103 x.1.borrow_mut().push(DropOrder::Function);
107 fn foo_sync(x: D, _y: D) {
108 x.1.borrow_mut().push(DropOrder::Function);
111 /// Check that underscore patterns are dropped after the method is polled.
112 async fn bar_async(x: D, _: D) {
113 x.1.borrow_mut().push(DropOrder::Function);
117 fn bar_sync(x: D, _: D) {
118 x.1.borrow_mut().push(DropOrder::Function);
121 /// Check that underscore patterns within more complex patterns are dropped after the method
123 async fn baz_async((x, _): (D, D)) {
124 x.1.borrow_mut().push(DropOrder::Function);
128 fn baz_sync((x, _): (D, D)) {
129 x.1.borrow_mut().push(DropOrder::Function);
132 /// Check that underscore and unused bindings within and outwith more complex patterns are
133 /// dropped after the method is polled.
134 async fn foobar_async(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
135 x.1.borrow_mut().push(DropOrder::Function);
139 fn foobar_sync(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
140 x.1.borrow_mut().push(DropOrder::Function);
144 struct Bar<'a>(PhantomData<&'a ()>);
147 /// Check that unused bindings are dropped after the method with self is polled.
148 async fn foo_async(&'a self, x: D, _y: D) {
149 x.1.borrow_mut().push(DropOrder::Function);
153 fn foo_sync(&'a self, x: D, _y: D) {
154 x.1.borrow_mut().push(DropOrder::Function);
157 /// Check that underscore patterns are dropped after the method with self is polled.
158 async fn bar_async(&'a self, x: D, _: D) {
159 x.1.borrow_mut().push(DropOrder::Function);
163 fn bar_sync(&'a self, x: D, _: D) {
164 x.1.borrow_mut().push(DropOrder::Function);
167 /// Check that underscore patterns within more complex patterns are dropped after the method
168 /// with self is polled.
169 async fn baz_async(&'a self, (x, _): (D, D)) {
170 x.1.borrow_mut().push(DropOrder::Function);
174 fn baz_sync(&'a self, (x, _): (D, D)) {
175 x.1.borrow_mut().push(DropOrder::Function);
178 /// Check that underscore and unused bindings within and outwith more complex patterns are
179 /// dropped after the method with self is polled.
180 async fn foobar_async(&'a self, x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
181 x.1.borrow_mut().push(DropOrder::Function);
185 fn foobar_sync(&'a self, x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
186 x.1.borrow_mut().push(DropOrder::Function);
190 fn assert_drop_order_after_cancel<Fut: Future<Output = ()>>(
191 f: impl FnOnce(DropOrderListPtr) -> Fut,
192 g: impl FnOnce(DropOrderListPtr),
194 let empty = Arc::new(EmptyWaker);
195 let waker = ArcWake::into_waker(empty);
196 let mut cx = Context::from_waker(&waker);
198 let actual_order = Rc::new(RefCell::new(Vec::new()));
199 let mut fut = Box::pin(f(actual_order.clone()));
200 let _ = fut.as_mut().poll(&mut cx);
202 // Parameters are never dropped until the future completes.
203 assert_eq!(*actual_order.borrow(), vec![DropOrder::Function]);
207 let expected_order = Rc::new(RefCell::new(Vec::new()));
208 g(expected_order.clone());
209 assert_eq!(*actual_order.borrow(), *expected_order.borrow());
213 // Free functions (see doc comment on function for what it tests).
214 assert_drop_order_after_cancel(
215 |l| foo_async(D("x", l.clone()), D("_y", l.clone())),
216 |l| foo_sync(D("x", l.clone()), D("_y", l.clone())),
218 assert_drop_order_after_cancel(
219 |l| bar_async(D("x", l.clone()), D("_", l.clone())),
220 |l| bar_sync(D("x", l.clone()), D("_", l.clone())),
222 assert_drop_order_after_cancel(
223 |l| baz_async((D("x", l.clone()), D("_", l.clone()))),
224 |l| baz_sync((D("x", l.clone()), D("_", l.clone()))),
226 assert_drop_order_after_cancel(
230 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
238 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
245 // Methods w/out self (see doc comment on function for what it tests).
246 assert_drop_order_after_cancel(
247 |l| Foo::foo_async(D("x", l.clone()), D("_y", l.clone())),
248 |l| Foo::foo_sync(D("x", l.clone()), D("_y", l.clone())),
250 assert_drop_order_after_cancel(
251 |l| Foo::bar_async(D("x", l.clone()), D("_", l.clone())),
252 |l| Foo::bar_sync(D("x", l.clone()), D("_", l.clone())),
254 assert_drop_order_after_cancel(
255 |l| Foo::baz_async((D("x", l.clone()), D("_", l.clone()))),
256 |l| Foo::baz_sync((D("x", l.clone()), D("_", l.clone()))),
258 assert_drop_order_after_cancel(
262 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
270 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
277 // Methods (see doc comment on function for what it tests).
278 let b = Bar(Default::default());
279 assert_drop_order_after_cancel(
280 |l| b.foo_async(D("x", l.clone()), D("_y", l.clone())),
281 |l| b.foo_sync(D("x", l.clone()), D("_y", l.clone())),
283 assert_drop_order_after_cancel(
284 |l| b.bar_async(D("x", l.clone()), D("_", l.clone())),
285 |l| b.bar_sync(D("x", l.clone()), D("_", l.clone())),
287 assert_drop_order_after_cancel(
288 |l| b.baz_async((D("x", l.clone()), D("_", l.clone()))),
289 |l| b.baz_sync((D("x", l.clone()), D("_", l.clone()))),
291 assert_drop_order_after_cancel(
295 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
303 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),