2 // ignore-wasm32-bare compiled with panic=abort by default
4 #![feature(generators, generator_trait, untagged_unions)]
5 #![feature(bindings_after_at)]
7 #![allow(unused_assignments)]
8 #![allow(unused_variables)]
10 use std::cell::{Cell, RefCell};
11 use std::mem::ManuallyDrop;
12 use std::ops::Generator;
17 struct InjectedFailure;
20 data: RefCell<Vec<bool>>,
25 impl panic::UnwindSafe for Allocator {}
26 impl panic::RefUnwindSafe for Allocator {}
28 impl Drop for Allocator {
30 let data = self.data.borrow();
31 if data.iter().any(|d| *d) {
32 panic!("missing free: {:?}", data);
38 fn new(failing_op: usize) -> Self {
40 failing_op: failing_op,
41 cur_ops: Cell::new(0),
42 data: RefCell::new(vec![])
45 fn alloc(&self) -> Ptr<'_> {
46 self.cur_ops.set(self.cur_ops.get() + 1);
48 if self.cur_ops.get() == self.failing_op {
49 panic!(InjectedFailure);
52 let mut data = self.data.borrow_mut();
53 let addr = data.len();
57 // FIXME(#47949) Any use of this indicates a bug in rustc: we should never
58 // be leaking values in the cases here.
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();
65 if exceptions.iter().any(|operation| *operation == self.failing_op) {
66 let mut data = self.data.borrow_mut();
73 struct Ptr<'a>(usize, &'a Allocator);
74 impl<'a> Drop for Ptr<'a> {
76 match self.1.data.borrow_mut()[self.0] {
78 panic!("double free at index {:?}", self.0)
80 ref mut d => *d = false
83 self.1.cur_ops.set(self.1.cur_ops.get()+1);
85 if self.1.cur_ops.get() == self.1.failing_op {
86 panic!(InjectedFailure);
91 fn dynamic_init(a: &Allocator, c: bool) {
98 fn dynamic_drop(a: &Allocator, c: bool) {
107 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
108 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
112 if (c0 && i == 0) || (c1 && i == 1) {
113 x = (a.alloc(), a.alloc(), a.alloc());
114 y = TwoPtrs(a.alloc(), a.alloc());
123 fn field_assignment(a: &Allocator, c0: bool) {
124 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
135 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
136 let mut _v = a.alloc();
137 let mut _w = a.alloc();
147 fn assignment1(a: &Allocator, c0: bool) {
148 let mut _v = a.alloc();
149 let mut _w = a.alloc();
161 fn union1(a: &Allocator) {
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));
169 fn array_simple(a: &Allocator) {
170 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
173 fn vec_simple(a: &Allocator) {
174 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
177 fn generator(a: &Allocator, run_count: usize) {
178 assert!(run_count < 4);
187 for _ in 0..run_count {
188 Pin::new(&mut gen).resume(());
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.
196 // Surprisingly enough, this used to not work.
203 #[allow(unreachable_code)]
204 fn vec_unreachable(a: &Allocator) {
205 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
208 fn slice_pattern_first(a: &Allocator) {
209 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
212 fn slice_pattern_middle(a: &Allocator) {
213 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
216 fn slice_pattern_two(a: &Allocator) {
217 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
220 fn slice_pattern_last(a: &Allocator) {
221 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
224 fn slice_pattern_one_of(a: &Allocator, i: usize) {
225 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
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"),
235 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
236 let a = [a.alloc(), a.alloc(), a.alloc()];
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()];
258 fn slice_pattern_reassign(a: &Allocator) {
259 let mut ar = [a.alloc(), a.alloc()];
261 ar = [a.alloc(), a.alloc()];
265 fn subslice_pattern_reassign(a: &Allocator) {
266 let mut ar = [a.alloc(), a.alloc(), a.alloc()];
268 ar = [a.alloc(), a.alloc(), a.alloc()];
269 let[_, _y @ ..] = ar;
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;
280 fn subslice_mixed_min_lengths(a: &Allocator, c: i32) {
281 let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())];
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; }
294 fn bindings_after_at_dynamic_init_move(a: &Allocator, c: bool) {
295 let foo = if c { Some(a.alloc()) } else { None };
298 if let bar @ Some(_) = foo {
303 fn bindings_after_at_dynamic_init_ref(a: &Allocator, c: bool) {
304 let foo = if c { Some(a.alloc()) } else { None };
307 if let bar @ Some(_baz) = &foo {
312 fn bindings_after_at_dynamic_drop_move(a: &Allocator, c: bool) {
313 let foo = if c { Some(a.alloc()) } else { None };
315 if let bar @ Some(_) = foo {
322 fn bindings_after_at_dynamic_drop_ref(a: &Allocator, c: bool) {
323 let foo = if c { Some(a.alloc()) } else { None };
325 if let bar @ Some(_baz) = &foo {
332 fn move_ref_pattern(a: &Allocator) {
333 let mut tup = (a.alloc(), a.alloc(), a.alloc(), a.alloc());
334 let (ref _a, ref mut _b, _c, mut _d) = tup;
337 fn panic_after_return(a: &Allocator) -> Ptr<'_> {
338 // Panic in the drop of `p` or `q` can leak
339 let exceptions = vec![8, 9];
345 // FIXME (#47949) We leak values when we panic in a destructor after
346 // evaluating an expression with `rustc_mir::build::Builder::into`.
347 a.alloc_leaked(exceptions)
351 fn panic_after_return_expr(a: &Allocator) -> Ptr<'_> {
352 // Panic in the drop of `p` or `q` can leak
353 let exceptions = vec![8, 9];
360 return a.alloc_leaked(exceptions);
364 fn panic_after_init(a: &Allocator) {
365 // Panic in the drop of `r` can leak
366 let exceptions = vec![8];
373 a.alloc_leaked(exceptions)
377 fn panic_after_init_temp(a: &Allocator) {
378 // Panic in the drop of `r` can leak
379 let exceptions = vec![8];
386 a.alloc_leaked(exceptions)
390 fn panic_after_init_by_loop(a: &Allocator) {
391 // Panic in the drop of `r` can leak
392 let exceptions = vec![8];
399 break a.alloc_leaked(exceptions);
403 fn run_test<F>(mut f: F)
404 where F: FnMut(&Allocator)
406 let first_alloc = Allocator::new(usize::MAX);
409 for failing_op in 1..first_alloc.cur_ops.get()+1 {
410 let alloc = Allocator::new(failing_op);
412 let f = panic::AssertUnwindSafe(&mut f);
413 let result = panic::catch_unwind(move || {
417 Ok(..) => panic!("test executed {} ops but now {}",
418 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
420 if e.downcast_ref::<InjectedFailure>().is_none() {
421 panic::resume_unwind(e);
428 fn run_test_nopanic<F>(mut f: F)
429 where F: FnMut(&Allocator)
431 let first_alloc = Allocator::new(usize::MAX);
436 run_test(|a| dynamic_init(a, false));
437 run_test(|a| dynamic_init(a, true));
438 run_test(|a| dynamic_drop(a, false));
439 run_test(|a| dynamic_drop(a, true));
441 run_test(|a| assignment2(a, false, false));
442 run_test(|a| assignment2(a, false, true));
443 run_test(|a| assignment2(a, true, false));
444 run_test(|a| assignment2(a, true, true));
446 run_test(|a| assignment1(a, false));
447 run_test(|a| assignment1(a, true));
449 run_test(|a| array_simple(a));
450 run_test(|a| vec_simple(a));
451 run_test(|a| vec_unreachable(a));
453 run_test(|a| struct_dynamic_drop(a, false, false, false));
454 run_test(|a| struct_dynamic_drop(a, false, false, true));
455 run_test(|a| struct_dynamic_drop(a, false, true, false));
456 run_test(|a| struct_dynamic_drop(a, false, true, true));
457 run_test(|a| struct_dynamic_drop(a, true, false, false));
458 run_test(|a| struct_dynamic_drop(a, true, false, true));
459 run_test(|a| struct_dynamic_drop(a, true, true, false));
460 run_test(|a| struct_dynamic_drop(a, true, true, true));
462 run_test(|a| field_assignment(a, false));
463 run_test(|a| field_assignment(a, true));
465 run_test(|a| generator(a, 0));
466 run_test(|a| generator(a, 1));
467 run_test(|a| generator(a, 2));
468 run_test(|a| generator(a, 3));
470 run_test(|a| mixed_drop_and_nondrop(a));
472 run_test(|a| slice_pattern_first(a));
473 run_test(|a| slice_pattern_middle(a));
474 run_test(|a| slice_pattern_two(a));
475 run_test(|a| slice_pattern_last(a));
476 run_test(|a| slice_pattern_one_of(a, 0));
477 run_test(|a| slice_pattern_one_of(a, 1));
478 run_test(|a| slice_pattern_one_of(a, 2));
479 run_test(|a| slice_pattern_one_of(a, 3));
481 run_test(|a| subslice_pattern_from_end(a, true));
482 run_test(|a| subslice_pattern_from_end(a, false));
483 run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
484 run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
485 run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
486 run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
487 run_test(|a| slice_pattern_reassign(a));
488 run_test(|a| subslice_pattern_reassign(a));
490 run_test(|a| index_field_mixed_ends(a));
491 run_test(|a| subslice_mixed_min_lengths(a, 0));
492 run_test(|a| subslice_mixed_min_lengths(a, 1));
493 run_test(|a| subslice_mixed_min_lengths(a, 2));
494 run_test(|a| subslice_mixed_min_lengths(a, 3));
495 run_test(|a| subslice_mixed_min_lengths(a, 4));
496 run_test(|a| subslice_mixed_min_lengths(a, 5));
497 run_test(|a| subslice_mixed_min_lengths(a, 6));
498 run_test(|a| subslice_mixed_min_lengths(a, 7));
500 run_test(|a| move_ref_pattern(a));
503 panic_after_return(a);
506 panic_after_return_expr(a);
508 run_test(|a| panic_after_init(a));
509 run_test(|a| panic_after_init_temp(a));
510 run_test(|a| panic_after_init_by_loop(a));
512 run_test(|a| bindings_after_at_dynamic_init_move(a, true));
513 run_test(|a| bindings_after_at_dynamic_init_move(a, false));
514 run_test(|a| bindings_after_at_dynamic_init_ref(a, true));
515 run_test(|a| bindings_after_at_dynamic_init_ref(a, false));
516 run_test(|a| bindings_after_at_dynamic_drop_move(a, true));
517 run_test(|a| bindings_after_at_dynamic_drop_move(a, false));
518 run_test(|a| bindings_after_at_dynamic_drop_ref(a, true));
519 run_test(|a| bindings_after_at_dynamic_drop_ref(a, false));
521 run_test_nopanic(|a| union1(a));