]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-23338-ensure-param-drop-order.rs
Rollup merge of #92942 - Xaeroxe:raw_arg, r=dtolnay
[rust.git] / src / test / ui / issues / issue-23338-ensure-param-drop-order.rs
1 // run-pass
2 #![allow(non_upper_case_globals)]
3
4 // This test is ensuring that parameters are indeed dropped after
5 // temporaries in a fn body.
6
7 use std::cell::RefCell;
8
9 use self::d::D;
10
11 pub fn main() {
12     let log = RefCell::new(vec![]);
13     d::println("created empty log");
14     test(&log);
15
16     assert_eq!(&log.borrow()[..],
17                [
18                    //                                    created empty log
19                    //    +-- Make D(da_0, 0)
20                    //    | +-- Make D(de_1, 1)
21                    //    | |                             calling foo
22                    //    | |                             entered foo
23                    //    | | +-- Make D(de_2, 2)
24                    //    | | | +-- Make D(da_1, 3)
25                    //    | | | | +-- Make D(de_3, 4)
26                    //    | | | | | +-- Make D(de_4, 5)
27                    3, // | | | +-- Drop D(da_1, 3)
28                    //    | | |   | |
29                    4, // | | |   +-- Drop D(de_3, 4)
30                    //    | | |     |
31                    //    | | |     |                     eval tail of foo
32                    //    | | | +-- Make D(de_5, 6)
33                    //    | | | | +-- Make D(de_6, 7)
34                    5, // | | | | | +-- Drop D(de_4, 5)
35                    //    | | | | |
36                    2, // | | +-- Drop D(de_2, 2)
37                    //    | |   | |
38                    6, // | |   +-- Drop D(de_5, 6)
39                    //    | |     |
40                    1, // | +-- Drop D(de_1, 1)
41                    //    |       |
42                    0, // +-- Drop D(da_0, 0)
43                    //            |
44                    //            |                       result D(de_6, 7)
45                    7 //          +-- Drop D(de_6, 7)
46
47                        ]);
48 }
49
50 fn test<'a>(log: d::Log<'a>) {
51     let da = D::new("da", 0, log);
52     let de = D::new("de", 1, log);
53     d::println("calling foo");
54     let result = foo(da, de);
55     d::println(&format!("result {}", result));
56 }
57
58 fn foo<'a>(da0: D<'a>, de1: D<'a>) -> D<'a> {
59     d::println("entered foo");
60     let de2 = de1.incr();      // creates D(de_2, 2)
61     let de4 = {
62         let _da1 = da0.incr(); // creates D(da_1, 3)
63         de2.incr().incr()      // creates D(de_3, 4) and D(de_4, 5)
64     };
65     d::println("eval tail of foo");
66     de4.incr().incr()          // creates D(de_5, 6) and D(de_6, 7)
67 }
68
69 // This module provides simultaneous printouts of the dynamic extents
70 // of all of the D values, in addition to logging the order that each
71 // is dropped.
72
73 const PREF_INDENT: u32 = 16;
74
75 pub mod d {
76     #![allow(unused_parens)]
77     use std::fmt;
78     use std::mem;
79     use std::cell::RefCell;
80
81     static mut counter: u32 = 0;
82     static mut trails: u64 = 0;
83
84     pub type Log<'a> = &'a RefCell<Vec<u32>>;
85
86     pub fn current_width() -> u32 {
87         unsafe { max_width() - trails.leading_zeros() }
88     }
89
90     pub fn max_width() -> u32 {
91         unsafe {
92             (mem::size_of_val(&trails)*8) as u32
93         }
94     }
95
96     pub fn indent_println(my_trails: u32, s: &str) {
97         let mut indent: String = String::new();
98         for i in 0..my_trails {
99             unsafe {
100                 if trails & (1 << i) != 0 {
101                     indent = indent + "| ";
102                 } else {
103                     indent = indent + "  ";
104                 }
105             }
106         }
107         println!("{}{}", indent, s);
108     }
109
110     pub fn println(s: &str) {
111         indent_println(super::PREF_INDENT, s);
112     }
113
114     fn first_avail() -> u32 {
115         unsafe {
116             for i in 0..64 {
117                 if trails & (1 << i) == 0 {
118                     return i;
119                 }
120             }
121         }
122         panic!("exhausted trails");
123     }
124
125     pub struct D<'a> {
126         name: &'static str, i: u32, uid: u32, trail: u32, log: Log<'a>
127     }
128
129     impl<'a> fmt::Display for D<'a> {
130         fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
131             write!(w, "D({}_{}, {})", self.name, self.i, self.uid)
132         }
133     }
134
135     impl<'a> D<'a> {
136         pub fn new(name: &'static str, i: u32, log: Log<'a>) -> D<'a> {
137             unsafe {
138                 let trail = first_avail();
139                 let ctr = counter;
140                 counter += 1;
141                 trails |= (1 << trail);
142                 let ret = D {
143                     name: name, i: i, log: log, uid: ctr, trail: trail
144                 };
145                 indent_println(trail, &format!("+-- Make {}", ret));
146                 ret
147             }
148         }
149         pub fn incr(&self) -> D<'a> {
150             D::new(self.name, self.i + 1, self.log)
151         }
152     }
153
154     impl<'a> Drop for D<'a> {
155         fn drop(&mut self) {
156             unsafe { trails &= !(1 << self.trail); };
157             self.log.borrow_mut().push(self.uid);
158             indent_println(self.trail, &format!("+-- Drop {}", self));
159             indent_println(::PREF_INDENT, "");
160         }
161     }
162 }