]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/drop/dynamic-drop.rs
Rollup merge of #57950 - QuietMisdreavus:lifetime-err-desc, r=estebank
[rust.git] / src / test / run-pass / drop / dynamic-drop.rs
1 // run-pass
2 #![allow(unused_assignments)]
3 #![allow(unused_variables)]
4 // revisions:lexical nll
5 #![cfg_attr(nll, feature(nll))]
6
7 // ignore-wasm32-bare compiled with panic=abort by default
8
9 #![feature(generators, generator_trait, untagged_unions)]
10 #![feature(slice_patterns)]
11
12 use std::cell::{Cell, RefCell};
13 use std::ops::Generator;
14 use std::panic;
15 use std::pin::Pin;
16 use std::usize;
17
18 struct InjectedFailure;
19
20 struct Allocator {
21     data: RefCell<Vec<bool>>,
22     failing_op: usize,
23     cur_ops: Cell<usize>,
24 }
25
26 impl panic::UnwindSafe for Allocator {}
27 impl panic::RefUnwindSafe for Allocator {}
28
29 impl Drop for Allocator {
30     fn drop(&mut self) {
31         let data = self.data.borrow();
32         if data.iter().any(|d| *d) {
33             panic!("missing free: {:?}", data);
34         }
35     }
36 }
37
38 impl Allocator {
39     fn new(failing_op: usize) -> Self {
40         Allocator {
41             failing_op: failing_op,
42             cur_ops: Cell::new(0),
43             data: RefCell::new(vec![])
44         }
45     }
46     fn alloc(&self) -> Ptr {
47         self.cur_ops.set(self.cur_ops.get() + 1);
48
49         if self.cur_ops.get() == self.failing_op {
50             panic!(InjectedFailure);
51         }
52
53         let mut data = self.data.borrow_mut();
54         let addr = data.len();
55         data.push(true);
56         Ptr(addr, self)
57     }
58 }
59
60 struct Ptr<'a>(usize, &'a Allocator);
61 impl<'a> Drop for Ptr<'a> {
62     fn drop(&mut self) {
63         match self.1.data.borrow_mut()[self.0] {
64             false => {
65                 panic!("double free at index {:?}", self.0)
66             }
67             ref mut d => *d = false
68         }
69
70         self.1.cur_ops.set(self.1.cur_ops.get()+1);
71
72         if self.1.cur_ops.get() == self.1.failing_op {
73             panic!(InjectedFailure);
74         }
75     }
76 }
77
78 fn dynamic_init(a: &Allocator, c: bool) {
79     let _x;
80     if c {
81         _x = Some(a.alloc());
82     }
83 }
84
85 fn dynamic_drop(a: &Allocator, c: bool) {
86     let x = a.alloc();
87     if c {
88         Some(x)
89     } else {
90         None
91     };
92 }
93
94 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
95 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
96     for i in 0..2 {
97         let x;
98         let y;
99         if (c0 && i == 0) || (c1 && i == 1) {
100             x = (a.alloc(), a.alloc(), a.alloc());
101             y = TwoPtrs(a.alloc(), a.alloc());
102             if c {
103                 drop(x.1);
104                 drop(y.0);
105             }
106         }
107     }
108 }
109
110 fn field_assignment(a: &Allocator, c0: bool) {
111     let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
112
113     x.1 = a.alloc();
114     x.1 = a.alloc();
115
116     let f = (x.0).0;
117     if c0 {
118         (x.0).0 = f;
119     }
120 }
121
122 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
123     let mut _v = a.alloc();
124     let mut _w = a.alloc();
125     if c0 {
126         drop(_v);
127     }
128     _v = _w;
129     if c1 {
130         _w = a.alloc();
131     }
132 }
133
134 fn assignment1(a: &Allocator, c0: bool) {
135     let mut _v = a.alloc();
136     let mut _w = a.alloc();
137     if c0 {
138         drop(_v);
139     }
140     _v = _w;
141 }
142
143 #[allow(unions_with_drop_fields)]
144 union Boxy<T> {
145     a: T,
146     b: T,
147 }
148
149 fn union1(a: &Allocator) {
150     unsafe {
151         let mut u = Boxy { a: a.alloc() };
152         u.b = a.alloc();
153         drop(u.a);
154     }
155 }
156
157 fn array_simple(a: &Allocator) {
158     let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
159 }
160
161 fn vec_simple(a: &Allocator) {
162     let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
163 }
164
165 fn generator(a: &Allocator, run_count: usize) {
166     assert!(run_count < 4);
167
168     let mut gen = || {
169         (a.alloc(),
170          yield a.alloc(),
171          a.alloc(),
172          yield a.alloc()
173          );
174     };
175     for _ in 0..run_count {
176         Pin::new(&mut gen).resume();
177     }
178 }
179
180 fn mixed_drop_and_nondrop(a: &Allocator) {
181     // check that destructor panics handle drop
182     // and non-drop blocks in the same scope correctly.
183     //
184     // Surprisingly enough, this used to not work.
185     let (x, y, z);
186     x = a.alloc();
187     y = 5;
188     z = a.alloc();
189 }
190
191 #[allow(unreachable_code)]
192 fn vec_unreachable(a: &Allocator) {
193     let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
194 }
195
196 fn slice_pattern_first(a: &Allocator) {
197     let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
198 }
199
200 fn slice_pattern_middle(a: &Allocator) {
201     let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
202 }
203
204 fn slice_pattern_two(a: &Allocator) {
205     let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
206 }
207
208 fn slice_pattern_last(a: &Allocator) {
209     let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
210 }
211
212 fn slice_pattern_one_of(a: &Allocator, i: usize) {
213     let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
214     let _x = match i {
215         0 => { let [a, ..] = array; a }
216         1 => { let [_, a, ..] = array; a }
217         2 => { let [_, _, a, _] = array; a }
218         3 => { let [_, _, _, a] = array; a }
219         _ => panic!("unmatched"),
220     };
221 }
222
223 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
224     let a = [a.alloc(), a.alloc(), a.alloc()];
225     if arg {
226         let[.., _x, _] = a;
227     } else {
228         let[_, _y..] = a;
229     }
230 }
231
232 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
233     let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
234     if arg2 {
235         drop(a);
236         return;
237     }
238
239     if arg {
240         let[.., _x, _] = a;
241     } else {
242         let[_, _y..] = a;
243     }
244 }
245
246 fn slice_pattern_reassign(a: &Allocator) {
247     let mut ar = [a.alloc(), a.alloc()];
248     let[_, _x] = ar;
249     ar = [a.alloc(), a.alloc()];
250     let[.., _y] = ar;
251 }
252
253 fn subslice_pattern_reassign(a: &Allocator) {
254     let mut ar = [a.alloc(), a.alloc(), a.alloc()];
255     let[_, _, _x] = ar;
256     ar = [a.alloc(), a.alloc(), a.alloc()];
257     let[_, _y..] = ar;
258 }
259
260 fn run_test<F>(mut f: F)
261     where F: FnMut(&Allocator)
262 {
263     let first_alloc = Allocator::new(usize::MAX);
264     f(&first_alloc);
265
266     for failing_op in 1..first_alloc.cur_ops.get()+1 {
267         let alloc = Allocator::new(failing_op);
268         let alloc = &alloc;
269         let f = panic::AssertUnwindSafe(&mut f);
270         let result = panic::catch_unwind(move || {
271             f.0(alloc);
272         });
273         match result {
274             Ok(..) => panic!("test executed {} ops but now {}",
275                              first_alloc.cur_ops.get(), alloc.cur_ops.get()),
276             Err(e) => {
277                 if e.downcast_ref::<InjectedFailure>().is_none() {
278                     panic::resume_unwind(e);
279                 }
280             }
281         }
282     }
283 }
284
285 fn run_test_nopanic<F>(mut f: F)
286     where F: FnMut(&Allocator)
287 {
288     let first_alloc = Allocator::new(usize::MAX);
289     f(&first_alloc);
290 }
291
292 fn main() {
293     run_test(|a| dynamic_init(a, false));
294     run_test(|a| dynamic_init(a, true));
295     run_test(|a| dynamic_drop(a, false));
296     run_test(|a| dynamic_drop(a, true));
297
298     run_test(|a| assignment2(a, false, false));
299     run_test(|a| assignment2(a, false, true));
300     run_test(|a| assignment2(a, true, false));
301     run_test(|a| assignment2(a, true, true));
302
303     run_test(|a| assignment1(a, false));
304     run_test(|a| assignment1(a, true));
305
306     run_test(|a| array_simple(a));
307     run_test(|a| vec_simple(a));
308     run_test(|a| vec_unreachable(a));
309
310     run_test(|a| struct_dynamic_drop(a, false, false, false));
311     run_test(|a| struct_dynamic_drop(a, false, false, true));
312     run_test(|a| struct_dynamic_drop(a, false, true, false));
313     run_test(|a| struct_dynamic_drop(a, false, true, true));
314     run_test(|a| struct_dynamic_drop(a, true, false, false));
315     run_test(|a| struct_dynamic_drop(a, true, false, true));
316     run_test(|a| struct_dynamic_drop(a, true, true, false));
317     run_test(|a| struct_dynamic_drop(a, true, true, true));
318
319     run_test(|a| field_assignment(a, false));
320     run_test(|a| field_assignment(a, true));
321
322     run_test(|a| generator(a, 0));
323     run_test(|a| generator(a, 1));
324     run_test(|a| generator(a, 2));
325     run_test(|a| generator(a, 3));
326
327     run_test(|a| mixed_drop_and_nondrop(a));
328
329     run_test(|a| slice_pattern_first(a));
330     run_test(|a| slice_pattern_middle(a));
331     run_test(|a| slice_pattern_two(a));
332     run_test(|a| slice_pattern_last(a));
333     run_test(|a| slice_pattern_one_of(a, 0));
334     run_test(|a| slice_pattern_one_of(a, 1));
335     run_test(|a| slice_pattern_one_of(a, 2));
336     run_test(|a| slice_pattern_one_of(a, 3));
337
338     run_test(|a| subslice_pattern_from_end(a, true));
339     run_test(|a| subslice_pattern_from_end(a, false));
340     run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
341     run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
342     run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
343     run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
344     run_test(|a| slice_pattern_reassign(a));
345     run_test(|a| subslice_pattern_reassign(a));
346
347     run_test_nopanic(|a| union1(a));
348 }