]> git.lizzy.rs Git - rust.git/blob - src/test/ui/pattern/move-ref-patterns/move-ref-patterns-dynamic-semantics.rs
Rollup merge of #105555 - krasimirgg:llvm-int-opt-2, r=cuviper
[rust.git] / src / test / ui / pattern / move-ref-patterns / move-ref-patterns-dynamic-semantics.rs
1 // run-pass
2
3 // This test checks the dynamic semantics and drop order of pattern matching
4 // where a product pattern has both a by-move and by-ref binding.
5
6 use std::cell::RefCell;
7 use std::rc::Rc;
8
9 struct X {
10     x: Box<usize>,
11     d: DropOrderListPtr,
12 }
13
14 type DropOrderListPtr = Rc<RefCell<Vec<usize>>>;
15
16 impl Drop for X {
17     fn drop(&mut self) {
18         self.d.borrow_mut().push(*self.x);
19     }
20 }
21
22 enum DoubleOption<T, U> {
23     Some2(T, U),
24     _None2,
25 }
26
27 fn main() {
28     let d: DropOrderListPtr = <_>::default();
29     {
30         let mk = |v| X { x: Box::new(v), d: d.clone() };
31         let check = |a1: &X, a2, b1: &X, b2| {
32             assert_eq!(*a1.x, a2);
33             assert_eq!(*b1.x, b2);
34         };
35
36         let x = DoubleOption::Some2(mk(1), mk(2));
37         match x {
38             DoubleOption::Some2(ref a, b) => check(a, 1, &b, 2),
39             DoubleOption::_None2 => panic!(),
40         }
41         let x = DoubleOption::Some2(mk(3), mk(4));
42         match x {
43             DoubleOption::Some2(a, ref b) => check(&a, 3, b, 4),
44             DoubleOption::_None2 => panic!(),
45         }
46         match DoubleOption::Some2(mk(5), mk(6)) {
47             DoubleOption::Some2(ref a, b) => check(a, 5, &b, 6),
48             DoubleOption::_None2 => panic!(),
49         }
50         match DoubleOption::Some2(mk(7), mk(8)) {
51             DoubleOption::Some2(a, ref b) => check(&a, 7, b, 8),
52             DoubleOption::_None2 => panic!(),
53         }
54         {
55             let (a, ref b) = (mk(9), mk(10));
56             let (ref c, d) = (mk(11), mk(12));
57             check(&a, 9, b, 10);
58             check(c, 11, &d, 12);
59         }
60         fn fun([a, ref mut b, ref xs @ .., ref c, d]: [X; 6]) {
61             assert_eq!(*a.x, 13);
62             assert_eq!(*b.x, 14);
63             assert_eq!(&[*xs[0].x, *xs[1].x], &[15, 16]);
64             assert_eq!(*c.x, 17);
65             assert_eq!(*d.x, 18);
66         }
67         fun([mk(13), mk(14), mk(15), mk(16), mk(17), mk(18)]);
68
69         let lam = |(a, ref b, c, ref mut d): (X, X, X, X)| {
70             assert_eq!(*a.x, 19);
71             assert_eq!(*b.x, 20);
72             assert_eq!(*c.x, 21);
73             assert_eq!(*d.x, 22);
74         };
75         lam((mk(19), mk(20), mk(21), mk(22)));
76     }
77     let expected = [2, 3, 6, 5, 7, 8, 12, 11, 9, 10, 18, 13, 14, 15, 16, 17, 21, 19, 20, 22, 4, 1];
78     assert_eq!(&*d.borrow(), &expected);
79 }