]> git.lizzy.rs Git - rust.git/blob - src/test/ui/drop/dynamic-drop.rs
Auto merge of #65140 - petrochenkov:disapp, r=nikomatsakis
[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::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 #[allow(unions_with_drop_fields)]
156 union Boxy<T> {
157     a: T,
158     b: T,
159 }
160
161 fn union1(a: &Allocator) {
162     unsafe {
163         let mut u = Boxy { a: a.alloc() };
164         u.b = a.alloc();
165         drop(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 panic_after_return(a: &Allocator) -> Ptr<'_> {
273     // Panic in the drop of `p` or `q` can leak
274     let exceptions = vec![8, 9];
275     a.alloc();
276     let p = a.alloc();
277     {
278         a.alloc();
279         let p = a.alloc();
280         // FIXME (#47949) We leak values when we panic in a destructor after
281         // evaluating an expression with `rustc_mir::build::Builder::into`.
282         a.alloc_leaked(exceptions)
283     }
284 }
285
286 fn panic_after_return_expr(a: &Allocator) -> Ptr<'_> {
287     // Panic in the drop of `p` or `q` can leak
288     let exceptions = vec![8, 9];
289     a.alloc();
290     let p = a.alloc();
291     {
292         a.alloc();
293         let q = a.alloc();
294         // FIXME (#47949)
295         return a.alloc_leaked(exceptions);
296     }
297 }
298
299 fn panic_after_init(a: &Allocator) {
300     // Panic in the drop of `r` can leak
301     let exceptions = vec![8];
302     a.alloc();
303     let p = a.alloc();
304     let q = {
305         a.alloc();
306         let r = a.alloc();
307         // FIXME (#47949)
308         a.alloc_leaked(exceptions)
309     };
310 }
311
312 fn panic_after_init_temp(a: &Allocator) {
313     // Panic in the drop of `r` can leak
314     let exceptions = vec![8];
315     a.alloc();
316     let p = a.alloc();
317     {
318         a.alloc();
319         let r = a.alloc();
320         // FIXME (#47949)
321         a.alloc_leaked(exceptions)
322     };
323 }
324
325 fn panic_after_init_by_loop(a: &Allocator) {
326     // Panic in the drop of `r` can leak
327     let exceptions = vec![8];
328     a.alloc();
329     let p = a.alloc();
330     let q = loop {
331         a.alloc();
332         let r = a.alloc();
333         // FIXME (#47949)
334         break a.alloc_leaked(exceptions);
335     };
336 }
337
338 fn run_test<F>(mut f: F)
339     where F: FnMut(&Allocator)
340 {
341     let first_alloc = Allocator::new(usize::MAX);
342     f(&first_alloc);
343
344     for failing_op in 1..first_alloc.cur_ops.get()+1 {
345         let alloc = Allocator::new(failing_op);
346         let alloc = &alloc;
347         let f = panic::AssertUnwindSafe(&mut f);
348         let result = panic::catch_unwind(move || {
349             f.0(alloc);
350         });
351         match result {
352             Ok(..) => panic!("test executed {} ops but now {}",
353                              first_alloc.cur_ops.get(), alloc.cur_ops.get()),
354             Err(e) => {
355                 if e.downcast_ref::<InjectedFailure>().is_none() {
356                     panic::resume_unwind(e);
357                 }
358             }
359         }
360     }
361 }
362
363 fn run_test_nopanic<F>(mut f: F)
364     where F: FnMut(&Allocator)
365 {
366     let first_alloc = Allocator::new(usize::MAX);
367     f(&first_alloc);
368 }
369
370 fn main() {
371     run_test(|a| dynamic_init(a, false));
372     run_test(|a| dynamic_init(a, true));
373     run_test(|a| dynamic_drop(a, false));
374     run_test(|a| dynamic_drop(a, true));
375
376     run_test(|a| assignment2(a, false, false));
377     run_test(|a| assignment2(a, false, true));
378     run_test(|a| assignment2(a, true, false));
379     run_test(|a| assignment2(a, true, true));
380
381     run_test(|a| assignment1(a, false));
382     run_test(|a| assignment1(a, true));
383
384     run_test(|a| array_simple(a));
385     run_test(|a| vec_simple(a));
386     run_test(|a| vec_unreachable(a));
387
388     run_test(|a| struct_dynamic_drop(a, false, false, false));
389     run_test(|a| struct_dynamic_drop(a, false, false, true));
390     run_test(|a| struct_dynamic_drop(a, false, true, false));
391     run_test(|a| struct_dynamic_drop(a, false, true, true));
392     run_test(|a| struct_dynamic_drop(a, true, false, false));
393     run_test(|a| struct_dynamic_drop(a, true, false, true));
394     run_test(|a| struct_dynamic_drop(a, true, true, false));
395     run_test(|a| struct_dynamic_drop(a, true, true, true));
396
397     run_test(|a| field_assignment(a, false));
398     run_test(|a| field_assignment(a, true));
399
400     run_test(|a| generator(a, 0));
401     run_test(|a| generator(a, 1));
402     run_test(|a| generator(a, 2));
403     run_test(|a| generator(a, 3));
404
405     run_test(|a| mixed_drop_and_nondrop(a));
406
407     run_test(|a| slice_pattern_first(a));
408     run_test(|a| slice_pattern_middle(a));
409     run_test(|a| slice_pattern_two(a));
410     run_test(|a| slice_pattern_last(a));
411     run_test(|a| slice_pattern_one_of(a, 0));
412     run_test(|a| slice_pattern_one_of(a, 1));
413     run_test(|a| slice_pattern_one_of(a, 2));
414     run_test(|a| slice_pattern_one_of(a, 3));
415
416     run_test(|a| subslice_pattern_from_end(a, true));
417     run_test(|a| subslice_pattern_from_end(a, false));
418     run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
419     run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
420     run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
421     run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
422     run_test(|a| slice_pattern_reassign(a));
423     run_test(|a| subslice_pattern_reassign(a));
424
425     run_test(|a| {
426         panic_after_return(a);
427     });
428     run_test(|a| {
429         panic_after_return_expr(a);
430     });
431     run_test(|a| panic_after_init(a));
432     run_test(|a| panic_after_init_temp(a));
433     run_test(|a| panic_after_init_by_loop(a));
434
435     run_test_nopanic(|a| union1(a));
436 }