]> git.lizzy.rs Git - rust.git/blob - src/test/ui/drop/dynamic-drop.rs
slice_patterns: remove gates in tests
[rust.git] / src / test / ui / drop / dynamic-drop.rs
1 // run-pass
2 // ignore-wasm32-bare compiled with panic=abort by default
3
4 #![feature(generators, generator_trait, untagged_unions)]
5
6 #![allow(unused_assignments)]
7 #![allow(unused_variables)]
8
9 use std::cell::{Cell, RefCell};
10 use std::mem::ManuallyDrop;
11 use std::ops::Generator;
12 use std::panic;
13 use std::pin::Pin;
14 use std::usize;
15
16 struct InjectedFailure;
17
18 struct Allocator {
19     data: RefCell<Vec<bool>>,
20     failing_op: usize,
21     cur_ops: Cell<usize>,
22 }
23
24 impl panic::UnwindSafe for Allocator {}
25 impl panic::RefUnwindSafe for Allocator {}
26
27 impl Drop for Allocator {
28     fn drop(&mut self) {
29         let data = self.data.borrow();
30         if data.iter().any(|d| *d) {
31             panic!("missing free: {:?}", data);
32         }
33     }
34 }
35
36 impl Allocator {
37     fn new(failing_op: usize) -> Self {
38         Allocator {
39             failing_op: failing_op,
40             cur_ops: Cell::new(0),
41             data: RefCell::new(vec![])
42         }
43     }
44     fn alloc(&self) -> Ptr<'_> {
45         self.cur_ops.set(self.cur_ops.get() + 1);
46
47         if self.cur_ops.get() == self.failing_op {
48             panic!(InjectedFailure);
49         }
50
51         let mut data = self.data.borrow_mut();
52         let addr = data.len();
53         data.push(true);
54         Ptr(addr, self)
55     }
56     // FIXME(#47949) Any use of this indicates a bug in rustc: we should never
57     // be leaking values in the cases here.
58     //
59     // Creates a `Ptr<'_>` and checks that the allocated value is leaked if the
60     // `failing_op` is in the list of exception.
61     fn alloc_leaked(&self, exceptions: Vec<usize>) -> Ptr<'_> {
62         let ptr = self.alloc();
63
64         if exceptions.iter().any(|operation| *operation == self.failing_op) {
65             let mut data = self.data.borrow_mut();
66             data[ptr.0] = false;
67         }
68         ptr
69     }
70 }
71
72 struct Ptr<'a>(usize, &'a Allocator);
73 impl<'a> Drop for Ptr<'a> {
74     fn drop(&mut self) {
75         match self.1.data.borrow_mut()[self.0] {
76             false => {
77                 panic!("double free at index {:?}", self.0)
78             }
79             ref mut d => *d = false
80         }
81
82         self.1.cur_ops.set(self.1.cur_ops.get()+1);
83
84         if self.1.cur_ops.get() == self.1.failing_op {
85             panic!(InjectedFailure);
86         }
87     }
88 }
89
90 fn dynamic_init(a: &Allocator, c: bool) {
91     let _x;
92     if c {
93         _x = Some(a.alloc());
94     }
95 }
96
97 fn dynamic_drop(a: &Allocator, c: bool) {
98     let x = a.alloc();
99     if c {
100         Some(x)
101     } else {
102         None
103     };
104 }
105
106 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
107 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
108     for i in 0..2 {
109         let x;
110         let y;
111         if (c0 && i == 0) || (c1 && i == 1) {
112             x = (a.alloc(), a.alloc(), a.alloc());
113             y = TwoPtrs(a.alloc(), a.alloc());
114             if c {
115                 drop(x.1);
116                 drop(y.0);
117             }
118         }
119     }
120 }
121
122 fn field_assignment(a: &Allocator, c0: bool) {
123     let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
124
125     x.1 = a.alloc();
126     x.1 = a.alloc();
127
128     let f = (x.0).0;
129     if c0 {
130         (x.0).0 = f;
131     }
132 }
133
134 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
135     let mut _v = a.alloc();
136     let mut _w = a.alloc();
137     if c0 {
138         drop(_v);
139     }
140     _v = _w;
141     if c1 {
142         _w = a.alloc();
143     }
144 }
145
146 fn assignment1(a: &Allocator, c0: bool) {
147     let mut _v = a.alloc();
148     let mut _w = a.alloc();
149     if c0 {
150         drop(_v);
151     }
152     _v = _w;
153 }
154
155 union Boxy<T> {
156     a: ManuallyDrop<T>,
157     b: ManuallyDrop<T>,
158 }
159
160 fn union1(a: &Allocator) {
161     unsafe {
162         let mut u = Boxy { a: ManuallyDrop::new(a.alloc()) };
163         *u.b = a.alloc(); // drops first alloc
164         drop(ManuallyDrop::into_inner(u.a));
165     }
166 }
167
168 fn array_simple(a: &Allocator) {
169     let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
170 }
171
172 fn vec_simple(a: &Allocator) {
173     let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
174 }
175
176 fn generator(a: &Allocator, run_count: usize) {
177     assert!(run_count < 4);
178
179     let mut gen = || {
180         (a.alloc(),
181          yield a.alloc(),
182          a.alloc(),
183          yield a.alloc()
184          );
185     };
186     for _ in 0..run_count {
187         Pin::new(&mut gen).resume();
188     }
189 }
190
191 fn mixed_drop_and_nondrop(a: &Allocator) {
192     // check that destructor panics handle drop
193     // and non-drop blocks in the same scope correctly.
194     //
195     // Surprisingly enough, this used to not work.
196     let (x, y, z);
197     x = a.alloc();
198     y = 5;
199     z = a.alloc();
200 }
201
202 #[allow(unreachable_code)]
203 fn vec_unreachable(a: &Allocator) {
204     let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
205 }
206
207 fn slice_pattern_first(a: &Allocator) {
208     let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
209 }
210
211 fn slice_pattern_middle(a: &Allocator) {
212     let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
213 }
214
215 fn slice_pattern_two(a: &Allocator) {
216     let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
217 }
218
219 fn slice_pattern_last(a: &Allocator) {
220     let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
221 }
222
223 fn slice_pattern_one_of(a: &Allocator, i: usize) {
224     let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
225     let _x = match i {
226         0 => { let [a, ..] = array; a }
227         1 => { let [_, a, ..] = array; a }
228         2 => { let [_, _, a, _] = array; a }
229         3 => { let [_, _, _, a] = array; a }
230         _ => panic!("unmatched"),
231     };
232 }
233
234 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
235     let a = [a.alloc(), a.alloc(), a.alloc()];
236     if arg {
237         let[.., _x, _] = a;
238     } else {
239         let[_, _y @ ..] = a;
240     }
241 }
242
243 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
244     let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
245     if arg2 {
246         drop(a);
247         return;
248     }
249
250     if arg {
251         let[.., _x, _] = a;
252     } else {
253         let[_, _y @ ..] = a;
254     }
255 }
256
257 fn slice_pattern_reassign(a: &Allocator) {
258     let mut ar = [a.alloc(), a.alloc()];
259     let[_, _x] = ar;
260     ar = [a.alloc(), a.alloc()];
261     let[.., _y] = ar;
262 }
263
264 fn subslice_pattern_reassign(a: &Allocator) {
265     let mut ar = [a.alloc(), a.alloc(), a.alloc()];
266     let[_, _, _x] = ar;
267     ar = [a.alloc(), a.alloc(), a.alloc()];
268     let[_, _y @ ..] = ar;
269 }
270
271 fn index_field_mixed_ends(a: &Allocator) {
272     let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())];
273     let[(_x, _), ..] = ar;
274     let[(_, _y), _] = ar;
275     let[_, (_, _w)] = ar;
276     let[.., (_z, _)] = ar;
277 }
278
279 fn subslice_mixed_min_lengths(a: &Allocator, c: i32) {
280     let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())];
281     match c {
282         0 => { let[_x, ..] = ar; }
283         1 => { let[_x, _, ..] = ar; }
284         2 => { let[_x, _] = ar; }
285         3 => { let[(_x, _), _, ..] = ar; }
286         4 => { let[.., (_x, _)] = ar; }
287         5 => { let[.., (_x, _), _] = ar; }
288         6 => { let [_y @ ..] = ar; }
289         _ => { let [_y @ .., _] = ar; }
290     }
291 }
292
293 fn panic_after_return(a: &Allocator) -> Ptr<'_> {
294     // Panic in the drop of `p` or `q` can leak
295     let exceptions = vec![8, 9];
296     a.alloc();
297     let p = a.alloc();
298     {
299         a.alloc();
300         let p = a.alloc();
301         // FIXME (#47949) We leak values when we panic in a destructor after
302         // evaluating an expression with `rustc_mir::build::Builder::into`.
303         a.alloc_leaked(exceptions)
304     }
305 }
306
307 fn panic_after_return_expr(a: &Allocator) -> Ptr<'_> {
308     // Panic in the drop of `p` or `q` can leak
309     let exceptions = vec![8, 9];
310     a.alloc();
311     let p = a.alloc();
312     {
313         a.alloc();
314         let q = a.alloc();
315         // FIXME (#47949)
316         return a.alloc_leaked(exceptions);
317     }
318 }
319
320 fn panic_after_init(a: &Allocator) {
321     // Panic in the drop of `r` can leak
322     let exceptions = vec![8];
323     a.alloc();
324     let p = a.alloc();
325     let q = {
326         a.alloc();
327         let r = a.alloc();
328         // FIXME (#47949)
329         a.alloc_leaked(exceptions)
330     };
331 }
332
333 fn panic_after_init_temp(a: &Allocator) {
334     // Panic in the drop of `r` can leak
335     let exceptions = vec![8];
336     a.alloc();
337     let p = a.alloc();
338     {
339         a.alloc();
340         let r = a.alloc();
341         // FIXME (#47949)
342         a.alloc_leaked(exceptions)
343     };
344 }
345
346 fn panic_after_init_by_loop(a: &Allocator) {
347     // Panic in the drop of `r` can leak
348     let exceptions = vec![8];
349     a.alloc();
350     let p = a.alloc();
351     let q = loop {
352         a.alloc();
353         let r = a.alloc();
354         // FIXME (#47949)
355         break a.alloc_leaked(exceptions);
356     };
357 }
358
359 fn run_test<F>(mut f: F)
360     where F: FnMut(&Allocator)
361 {
362     let first_alloc = Allocator::new(usize::MAX);
363     f(&first_alloc);
364
365     for failing_op in 1..first_alloc.cur_ops.get()+1 {
366         let alloc = Allocator::new(failing_op);
367         let alloc = &alloc;
368         let f = panic::AssertUnwindSafe(&mut f);
369         let result = panic::catch_unwind(move || {
370             f.0(alloc);
371         });
372         match result {
373             Ok(..) => panic!("test executed {} ops but now {}",
374                              first_alloc.cur_ops.get(), alloc.cur_ops.get()),
375             Err(e) => {
376                 if e.downcast_ref::<InjectedFailure>().is_none() {
377                     panic::resume_unwind(e);
378                 }
379             }
380         }
381     }
382 }
383
384 fn run_test_nopanic<F>(mut f: F)
385     where F: FnMut(&Allocator)
386 {
387     let first_alloc = Allocator::new(usize::MAX);
388     f(&first_alloc);
389 }
390
391 fn main() {
392     run_test(|a| dynamic_init(a, false));
393     run_test(|a| dynamic_init(a, true));
394     run_test(|a| dynamic_drop(a, false));
395     run_test(|a| dynamic_drop(a, true));
396
397     run_test(|a| assignment2(a, false, false));
398     run_test(|a| assignment2(a, false, true));
399     run_test(|a| assignment2(a, true, false));
400     run_test(|a| assignment2(a, true, true));
401
402     run_test(|a| assignment1(a, false));
403     run_test(|a| assignment1(a, true));
404
405     run_test(|a| array_simple(a));
406     run_test(|a| vec_simple(a));
407     run_test(|a| vec_unreachable(a));
408
409     run_test(|a| struct_dynamic_drop(a, false, false, false));
410     run_test(|a| struct_dynamic_drop(a, false, false, true));
411     run_test(|a| struct_dynamic_drop(a, false, true, false));
412     run_test(|a| struct_dynamic_drop(a, false, true, true));
413     run_test(|a| struct_dynamic_drop(a, true, false, false));
414     run_test(|a| struct_dynamic_drop(a, true, false, true));
415     run_test(|a| struct_dynamic_drop(a, true, true, false));
416     run_test(|a| struct_dynamic_drop(a, true, true, true));
417
418     run_test(|a| field_assignment(a, false));
419     run_test(|a| field_assignment(a, true));
420
421     run_test(|a| generator(a, 0));
422     run_test(|a| generator(a, 1));
423     run_test(|a| generator(a, 2));
424     run_test(|a| generator(a, 3));
425
426     run_test(|a| mixed_drop_and_nondrop(a));
427
428     run_test(|a| slice_pattern_first(a));
429     run_test(|a| slice_pattern_middle(a));
430     run_test(|a| slice_pattern_two(a));
431     run_test(|a| slice_pattern_last(a));
432     run_test(|a| slice_pattern_one_of(a, 0));
433     run_test(|a| slice_pattern_one_of(a, 1));
434     run_test(|a| slice_pattern_one_of(a, 2));
435     run_test(|a| slice_pattern_one_of(a, 3));
436
437     run_test(|a| subslice_pattern_from_end(a, true));
438     run_test(|a| subslice_pattern_from_end(a, false));
439     run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
440     run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
441     run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
442     run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
443     run_test(|a| slice_pattern_reassign(a));
444     run_test(|a| subslice_pattern_reassign(a));
445
446     run_test(|a| index_field_mixed_ends(a));
447     run_test(|a| subslice_mixed_min_lengths(a, 0));
448     run_test(|a| subslice_mixed_min_lengths(a, 1));
449     run_test(|a| subslice_mixed_min_lengths(a, 2));
450     run_test(|a| subslice_mixed_min_lengths(a, 3));
451     run_test(|a| subslice_mixed_min_lengths(a, 4));
452     run_test(|a| subslice_mixed_min_lengths(a, 5));
453     run_test(|a| subslice_mixed_min_lengths(a, 6));
454     run_test(|a| subslice_mixed_min_lengths(a, 7));
455
456     run_test(|a| {
457         panic_after_return(a);
458     });
459     run_test(|a| {
460         panic_after_return_expr(a);
461     });
462     run_test(|a| panic_after_init(a));
463     run_test(|a| panic_after_init_temp(a));
464     run_test(|a| panic_after_init_by_loop(a));
465
466     run_test_nopanic(|a| union1(a));
467 }