1 // aux-build:arc_wake.rs
5 // revisions: default nomiropt
6 //[nomiropt]compile-flags: -Z mir-opt-level=0
8 #![allow(unused_variables)]
10 // Test that the drop order for parameters in a fn and async fn matches up. Also test that
11 // parameters (used or unused) are not dropped until the async fn completes execution.
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;
22 use std::task::Context;
26 impl ArcWake for EmptyWaker {
27 fn wake(self: Arc<Self>) {}
30 #[derive(Debug, Eq, PartialEq)]
36 type DropOrderListPtr = Rc<RefCell<Vec<DropOrder>>>;
38 struct D(&'static str, DropOrderListPtr);
42 self.1.borrow_mut().push(DropOrder::Val(self.0));
46 /// Check that unused bindings are dropped after the function is polled.
47 async fn foo_async(x: D, _y: D) {
48 x.1.borrow_mut().push(DropOrder::Function);
51 fn foo_sync(x: D, _y: D) {
52 x.1.borrow_mut().push(DropOrder::Function);
55 /// Check that underscore patterns are dropped after the function is polled.
56 async fn bar_async(x: D, _: D) {
57 x.1.borrow_mut().push(DropOrder::Function);
60 fn bar_sync(x: D, _: D) {
61 x.1.borrow_mut().push(DropOrder::Function);
64 /// Check that underscore patterns within more complex patterns are dropped after the function
66 async fn baz_async((x, _): (D, D)) {
67 x.1.borrow_mut().push(DropOrder::Function);
70 fn baz_sync((x, _): (D, D)) {
71 x.1.borrow_mut().push(DropOrder::Function);
74 /// Check that underscore and unused bindings within and outwith more complex patterns are dropped
75 /// after the function is polled.
76 async fn foobar_async(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
77 x.1.borrow_mut().push(DropOrder::Function);
80 fn foobar_sync(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
81 x.1.borrow_mut().push(DropOrder::Function);
87 /// Check that unused bindings are dropped after the method is polled.
88 async fn foo_async(x: D, _y: D) {
89 x.1.borrow_mut().push(DropOrder::Function);
92 fn foo_sync(x: D, _y: D) {
93 x.1.borrow_mut().push(DropOrder::Function);
96 /// Check that underscore patterns are dropped after the method is polled.
97 async fn bar_async(x: D, _: D) {
98 x.1.borrow_mut().push(DropOrder::Function);
101 fn bar_sync(x: D, _: D) {
102 x.1.borrow_mut().push(DropOrder::Function);
105 /// Check that underscore patterns within more complex patterns are dropped after the method
107 async fn baz_async((x, _): (D, D)) {
108 x.1.borrow_mut().push(DropOrder::Function);
111 fn baz_sync((x, _): (D, D)) {
112 x.1.borrow_mut().push(DropOrder::Function);
115 /// Check that underscore and unused bindings within and outwith more complex patterns are
116 /// dropped after the method is polled.
117 async fn foobar_async(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
118 x.1.borrow_mut().push(DropOrder::Function);
121 fn foobar_sync(x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
122 x.1.borrow_mut().push(DropOrder::Function);
126 struct Bar<'a>(PhantomData<&'a ()>);
129 /// Check that unused bindings are dropped after the method with self is polled.
130 async fn foo_async(&'a self, x: D, _y: D) {
131 x.1.borrow_mut().push(DropOrder::Function);
134 fn foo_sync(&'a self, x: D, _y: D) {
135 x.1.borrow_mut().push(DropOrder::Function);
138 /// Check that underscore patterns are dropped after the method with self is polled.
139 async fn bar_async(&'a self, x: D, _: D) {
140 x.1.borrow_mut().push(DropOrder::Function);
143 fn bar_sync(&'a self, x: D, _: D) {
144 x.1.borrow_mut().push(DropOrder::Function);
147 /// Check that underscore patterns within more complex patterns are dropped after the method
148 /// with self is polled.
149 async fn baz_async(&'a self, (x, _): (D, D)) {
150 x.1.borrow_mut().push(DropOrder::Function);
153 fn baz_sync(&'a self, (x, _): (D, D)) {
154 x.1.borrow_mut().push(DropOrder::Function);
157 /// Check that underscore and unused bindings within and outwith more complex patterns are
158 /// dropped after the method with self is polled.
159 async fn foobar_async(&'a self, x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
160 x.1.borrow_mut().push(DropOrder::Function);
163 fn foobar_sync(&'a self, x: D, (a, _, _c): (D, D, D), _: D, _y: D) {
164 x.1.borrow_mut().push(DropOrder::Function);
168 fn assert_drop_order_after_poll<Fut: Future<Output = ()>>(
169 f: impl FnOnce(DropOrderListPtr) -> Fut,
170 g: impl FnOnce(DropOrderListPtr),
172 let empty = Arc::new(EmptyWaker);
173 let waker = ArcWake::into_waker(empty);
174 let mut cx = Context::from_waker(&waker);
176 let actual_order = Rc::new(RefCell::new(Vec::new()));
177 let mut fut = Box::pin(f(actual_order.clone()));
178 let _ = fut.as_mut().poll(&mut cx);
180 let expected_order = Rc::new(RefCell::new(Vec::new()));
181 g(expected_order.clone());
183 assert_eq!(*actual_order.borrow(), *expected_order.borrow());
187 // Free functions (see doc comment on function for what it tests).
188 assert_drop_order_after_poll(|l| foo_async(D("x", l.clone()), D("_y", l.clone())),
189 |l| foo_sync(D("x", l.clone()), D("_y", l.clone())));
190 assert_drop_order_after_poll(|l| bar_async(D("x", l.clone()), D("_", l.clone())),
191 |l| bar_sync(D("x", l.clone()), D("_", l.clone())));
192 assert_drop_order_after_poll(|l| baz_async((D("x", l.clone()), D("_", l.clone()))),
193 |l| baz_sync((D("x", l.clone()), D("_", l.clone()))));
194 assert_drop_order_after_poll(
198 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
206 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
213 // Methods w/out self (see doc comment on function for what it tests).
214 assert_drop_order_after_poll(|l| Foo::foo_async(D("x", l.clone()), D("_y", l.clone())),
215 |l| Foo::foo_sync(D("x", l.clone()), D("_y", l.clone())));
216 assert_drop_order_after_poll(|l| Foo::bar_async(D("x", l.clone()), D("_", l.clone())),
217 |l| Foo::bar_sync(D("x", l.clone()), D("_", l.clone())));
218 assert_drop_order_after_poll(|l| Foo::baz_async((D("x", l.clone()), D("_", l.clone()))),
219 |l| Foo::baz_sync((D("x", l.clone()), D("_", l.clone()))));
220 assert_drop_order_after_poll(
224 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
232 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
239 // Methods (see doc comment on function for what it tests).
240 let b = Bar(Default::default());
241 assert_drop_order_after_poll(|l| b.foo_async(D("x", l.clone()), D("_y", l.clone())),
242 |l| b.foo_sync(D("x", l.clone()), D("_y", l.clone())));
243 assert_drop_order_after_poll(|l| b.bar_async(D("x", l.clone()), D("_", l.clone())),
244 |l| b.bar_sync(D("x", l.clone()), D("_", l.clone())));
245 assert_drop_order_after_poll(|l| b.baz_async((D("x", l.clone()), D("_", l.clone()))),
246 |l| b.baz_sync((D("x", l.clone()), D("_", l.clone()))));
247 assert_drop_order_after_poll(
251 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),
259 (D("a", l.clone()), D("_", l.clone()), D("_c", l.clone())),