]> git.lizzy.rs Git - rust.git/blob - src/test/ui/drop/dynamic-drop.rs
Update const_forget.rs
[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 #![feature(move_ref_pattern)]
6
7 #![allow(unused_assignments)]
8 #![allow(unused_variables)]
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 move_ref_pattern(a: &Allocator) {
295     let mut tup = (a.alloc(), a.alloc(), a.alloc(), a.alloc());
296     let (ref _a, ref mut _b, _c, mut _d) = tup;
297 }
298
299 fn panic_after_return(a: &Allocator) -> Ptr<'_> {
300     // Panic in the drop of `p` or `q` can leak
301     let exceptions = vec![8, 9];
302     a.alloc();
303     let p = a.alloc();
304     {
305         a.alloc();
306         let p = a.alloc();
307         // FIXME (#47949) We leak values when we panic in a destructor after
308         // evaluating an expression with `rustc_mir::build::Builder::into`.
309         a.alloc_leaked(exceptions)
310     }
311 }
312
313 fn panic_after_return_expr(a: &Allocator) -> Ptr<'_> {
314     // Panic in the drop of `p` or `q` can leak
315     let exceptions = vec![8, 9];
316     a.alloc();
317     let p = a.alloc();
318     {
319         a.alloc();
320         let q = a.alloc();
321         // FIXME (#47949)
322         return a.alloc_leaked(exceptions);
323     }
324 }
325
326 fn panic_after_init(a: &Allocator) {
327     // Panic in the drop of `r` can leak
328     let exceptions = vec![8];
329     a.alloc();
330     let p = a.alloc();
331     let q = {
332         a.alloc();
333         let r = a.alloc();
334         // FIXME (#47949)
335         a.alloc_leaked(exceptions)
336     };
337 }
338
339 fn panic_after_init_temp(a: &Allocator) {
340     // Panic in the drop of `r` can leak
341     let exceptions = vec![8];
342     a.alloc();
343     let p = a.alloc();
344     {
345         a.alloc();
346         let r = a.alloc();
347         // FIXME (#47949)
348         a.alloc_leaked(exceptions)
349     };
350 }
351
352 fn panic_after_init_by_loop(a: &Allocator) {
353     // Panic in the drop of `r` can leak
354     let exceptions = vec![8];
355     a.alloc();
356     let p = a.alloc();
357     let q = loop {
358         a.alloc();
359         let r = a.alloc();
360         // FIXME (#47949)
361         break a.alloc_leaked(exceptions);
362     };
363 }
364
365 fn run_test<F>(mut f: F)
366     where F: FnMut(&Allocator)
367 {
368     let first_alloc = Allocator::new(usize::MAX);
369     f(&first_alloc);
370
371     for failing_op in 1..first_alloc.cur_ops.get()+1 {
372         let alloc = Allocator::new(failing_op);
373         let alloc = &alloc;
374         let f = panic::AssertUnwindSafe(&mut f);
375         let result = panic::catch_unwind(move || {
376             f.0(alloc);
377         });
378         match result {
379             Ok(..) => panic!("test executed {} ops but now {}",
380                              first_alloc.cur_ops.get(), alloc.cur_ops.get()),
381             Err(e) => {
382                 if e.downcast_ref::<InjectedFailure>().is_none() {
383                     panic::resume_unwind(e);
384                 }
385             }
386         }
387     }
388 }
389
390 fn run_test_nopanic<F>(mut f: F)
391     where F: FnMut(&Allocator)
392 {
393     let first_alloc = Allocator::new(usize::MAX);
394     f(&first_alloc);
395 }
396
397 fn main() {
398     run_test(|a| dynamic_init(a, false));
399     run_test(|a| dynamic_init(a, true));
400     run_test(|a| dynamic_drop(a, false));
401     run_test(|a| dynamic_drop(a, true));
402
403     run_test(|a| assignment2(a, false, false));
404     run_test(|a| assignment2(a, false, true));
405     run_test(|a| assignment2(a, true, false));
406     run_test(|a| assignment2(a, true, true));
407
408     run_test(|a| assignment1(a, false));
409     run_test(|a| assignment1(a, true));
410
411     run_test(|a| array_simple(a));
412     run_test(|a| vec_simple(a));
413     run_test(|a| vec_unreachable(a));
414
415     run_test(|a| struct_dynamic_drop(a, false, false, false));
416     run_test(|a| struct_dynamic_drop(a, false, false, true));
417     run_test(|a| struct_dynamic_drop(a, false, true, false));
418     run_test(|a| struct_dynamic_drop(a, false, true, true));
419     run_test(|a| struct_dynamic_drop(a, true, false, false));
420     run_test(|a| struct_dynamic_drop(a, true, false, true));
421     run_test(|a| struct_dynamic_drop(a, true, true, false));
422     run_test(|a| struct_dynamic_drop(a, true, true, true));
423
424     run_test(|a| field_assignment(a, false));
425     run_test(|a| field_assignment(a, true));
426
427     run_test(|a| generator(a, 0));
428     run_test(|a| generator(a, 1));
429     run_test(|a| generator(a, 2));
430     run_test(|a| generator(a, 3));
431
432     run_test(|a| mixed_drop_and_nondrop(a));
433
434     run_test(|a| slice_pattern_first(a));
435     run_test(|a| slice_pattern_middle(a));
436     run_test(|a| slice_pattern_two(a));
437     run_test(|a| slice_pattern_last(a));
438     run_test(|a| slice_pattern_one_of(a, 0));
439     run_test(|a| slice_pattern_one_of(a, 1));
440     run_test(|a| slice_pattern_one_of(a, 2));
441     run_test(|a| slice_pattern_one_of(a, 3));
442
443     run_test(|a| subslice_pattern_from_end(a, true));
444     run_test(|a| subslice_pattern_from_end(a, false));
445     run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
446     run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
447     run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
448     run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
449     run_test(|a| slice_pattern_reassign(a));
450     run_test(|a| subslice_pattern_reassign(a));
451
452     run_test(|a| index_field_mixed_ends(a));
453     run_test(|a| subslice_mixed_min_lengths(a, 0));
454     run_test(|a| subslice_mixed_min_lengths(a, 1));
455     run_test(|a| subslice_mixed_min_lengths(a, 2));
456     run_test(|a| subslice_mixed_min_lengths(a, 3));
457     run_test(|a| subslice_mixed_min_lengths(a, 4));
458     run_test(|a| subslice_mixed_min_lengths(a, 5));
459     run_test(|a| subslice_mixed_min_lengths(a, 6));
460     run_test(|a| subslice_mixed_min_lengths(a, 7));
461
462     run_test(|a| move_ref_pattern(a));
463
464     run_test(|a| {
465         panic_after_return(a);
466     });
467     run_test(|a| {
468         panic_after_return_expr(a);
469     });
470     run_test(|a| panic_after_init(a));
471     run_test(|a| panic_after_init_temp(a));
472     run_test(|a| panic_after_init_by_loop(a));
473
474     run_test_nopanic(|a| union1(a));
475 }