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