]> git.lizzy.rs Git - rust.git/blob - src/test/ui/binding/fn-arg-incomplete-pattern-drop-order.rs
Do not complain about missing `fn main()` in some cases
[rust.git] / src / test / ui / binding / fn-arg-incomplete-pattern-drop-order.rs
1 // run-pass
2 // Check that partially moved from function parameters are dropped after the
3 // named bindings that move from them.
4
5 // ignore-emscripten compiled with panic=abort by default
6
7 use std::{panic, cell::RefCell};
8
9 struct LogDrop<'a>(i32, Context<'a>);
10
11 #[derive(Copy, Clone)]
12 struct Context<'a> {
13     panic_on: i32,
14     drops: &'a RefCell<Vec<i32>>,
15 }
16
17 impl<'a> Context<'a> {
18     fn record_drop(self, index: i32) {
19         self.drops.borrow_mut().push(index);
20         if index == self.panic_on {
21             panic!();
22         }
23     }
24 }
25
26 impl<'a> Drop for LogDrop<'a> {
27     fn drop(&mut self) {
28         self.1.record_drop(self.0);
29     }
30 }
31
32 fn bindings_in_params((_x, _): (LogDrop, LogDrop), (_, _y): (LogDrop, LogDrop)) {}
33 fn bindings_with_let(a: (LogDrop, LogDrop), b: (LogDrop, LogDrop)) {
34     // Drop order in foo is the same as the following bindings.
35     // _temp2 is declared after _x to avoid a difference between `_: T` and
36     // `x: T` in function parameters.
37     let _temp1 = a;
38     let (_x, _) = _temp1;
39
40     let _temp2 = b;
41     let (_, _y) = _temp2;
42 }
43
44 fn test_drop_order(panic_on: i32, fun: fn((LogDrop, LogDrop), (LogDrop, LogDrop))) {
45     let context = Context {
46         panic_on,
47         drops: &RefCell::new(Vec::new()),
48     };
49     let one = LogDrop(1, context);
50     let two = LogDrop(2, context);
51     let three = LogDrop(3, context);
52     let four = LogDrop(4, context);
53
54     let res = panic::catch_unwind(panic::AssertUnwindSafe(|| {
55         fun((three, four), (two, one));
56     }));
57     if panic_on == 0 {
58         assert!(res.is_ok(), "should not have panicked");
59     } else {
60         assert!(res.is_err(), "should have panicked");
61     }
62     assert_eq!(*context.drops.borrow(), [1, 2, 3, 4], "incorrect drop order");
63 }
64
65 fn main() {
66     (0..=4).for_each(|i| test_drop_order(i, bindings_in_params));
67     (0..=4).for_each(|i| test_drop_order(i, bindings_with_let));
68     (0..=4).for_each(|i| test_drop_order(i, |(_x, _), (_, _y)| {}));
69 }