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