2 // ignore-wasm32-bare compiled with panic=abort by default
4 #![feature(generators, generator_trait)]
5 #![feature(bindings_after_at)]
6 #![allow(unused_assignments)]
7 #![allow(unused_variables)]
9 use std::cell::{Cell, RefCell};
10 use std::mem::ManuallyDrop;
11 use std::ops::Generator;
15 struct InjectedFailure;
18 data: RefCell<Vec<bool>>,
24 impl panic::UnwindSafe for Allocator {}
25 impl panic::RefUnwindSafe for Allocator {}
27 impl Drop for Allocator {
29 let data = self.data.borrow();
30 if data.iter().any(|d| *d) {
31 panic!("missing free in {:?}: {:?}", self.name, data);
37 fn new(failing_op: usize, name: &'static str) -> Self {
39 failing_op: failing_op,
40 cur_ops: Cell::new(0),
41 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::panic_any(InjectedFailure);
52 let mut data = self.data.borrow_mut();
53 let addr = data.len();
59 struct Ptr<'a>(usize, &'a Allocator);
60 impl<'a> Drop for Ptr<'a> {
62 match self.1.data.borrow_mut()[self.0] {
63 false => panic!("double free in {:?} at index {:?}", self.1.name, self.0),
64 ref mut d => *d = false,
67 self.1.cur_ops.set(self.1.cur_ops.get() + 1);
69 if self.1.cur_ops.get() == self.1.failing_op {
70 panic::panic_any(InjectedFailure);
75 fn dynamic_init(a: &Allocator, c: bool) {
82 fn dynamic_drop(a: &Allocator, c: bool) {
91 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
92 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
96 if (c0 && i == 0) || (c1 && i == 1) {
97 x = (a.alloc(), a.alloc(), a.alloc());
98 y = TwoPtrs(a.alloc(), a.alloc());
107 fn field_assignment(a: &Allocator, c0: bool) {
108 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
119 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
120 let mut _v = a.alloc();
121 let mut _w = a.alloc();
131 fn assignment1(a: &Allocator, c0: bool) {
132 let mut _v = a.alloc();
133 let mut _w = a.alloc();
145 fn union1(a: &Allocator) {
147 let mut u = Boxy { a: ManuallyDrop::new(a.alloc()) };
148 *u.b = a.alloc(); // drops first alloc
149 drop(ManuallyDrop::into_inner(u.a));
153 fn array_simple(a: &Allocator) {
154 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
157 fn vec_simple(a: &Allocator) {
158 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
161 fn generator(a: &Allocator, run_count: usize) {
162 assert!(run_count < 4);
165 (a.alloc(), yield a.alloc(), a.alloc(), yield a.alloc());
167 for _ in 0..run_count {
168 Pin::new(&mut gen).resume(());
172 fn mixed_drop_and_nondrop(a: &Allocator) {
173 // check that destructor panics handle drop
174 // and non-drop blocks in the same scope correctly.
176 // Surprisingly enough, this used to not work.
183 #[allow(unreachable_code)]
184 fn vec_unreachable(a: &Allocator) {
185 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
188 fn slice_pattern_first(a: &Allocator) {
189 let [_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
192 fn slice_pattern_middle(a: &Allocator) {
193 let [_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
196 fn slice_pattern_two(a: &Allocator) {
197 let [_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
200 fn slice_pattern_last(a: &Allocator) {
201 let [.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
204 fn slice_pattern_one_of(a: &Allocator, i: usize) {
205 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
212 let [_, a, ..] = array;
216 let [_, _, a, _] = array;
220 let [_, _, _, a] = array;
223 _ => panic!("unmatched"),
227 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
228 let a = [a.alloc(), a.alloc(), a.alloc()];
232 let [_, _y @ ..] = a;
236 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
237 let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
246 let [_, _y @ ..] = a;
250 fn slice_pattern_reassign(a: &Allocator) {
251 let mut ar = [a.alloc(), a.alloc()];
253 ar = [a.alloc(), a.alloc()];
257 fn subslice_pattern_reassign(a: &Allocator) {
258 let mut ar = [a.alloc(), a.alloc(), a.alloc()];
260 ar = [a.alloc(), a.alloc(), a.alloc()];
261 let [_, _y @ ..] = ar;
264 fn index_field_mixed_ends(a: &Allocator) {
265 let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())];
266 let [(_x, _), ..] = ar;
267 let [(_, _y), _] = ar;
268 let [_, (_, _w)] = ar;
269 let [.., (_z, _)] = ar;
272 fn subslice_mixed_min_lengths(a: &Allocator, c: i32) {
273 let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())];
279 let [_x, _, ..] = ar;
285 let [(_x, _), _, ..] = ar;
288 let [.., (_x, _)] = ar;
291 let [.., (_x, _), _] = ar;
297 let [_y @ .., _] = ar;
302 fn bindings_after_at_dynamic_init_move(a: &Allocator, c: bool) {
303 let foo = if c { Some(a.alloc()) } else { None };
306 if let bar @ Some(_) = foo {
311 fn bindings_after_at_dynamic_init_ref(a: &Allocator, c: bool) {
312 let foo = if c { Some(a.alloc()) } else { None };
315 if let bar @ Some(_baz) = &foo {
320 fn bindings_after_at_dynamic_drop_move(a: &Allocator, c: bool) {
321 let foo = if c { Some(a.alloc()) } else { None };
323 if let bar @ Some(_) = foo {
330 fn bindings_after_at_dynamic_drop_ref(a: &Allocator, c: bool) {
331 let foo = if c { Some(a.alloc()) } else { None };
333 if let bar @ Some(_baz) = &foo {
340 fn move_ref_pattern(a: &Allocator) {
341 let mut tup = (a.alloc(), a.alloc(), a.alloc(), a.alloc());
342 let (ref _a, ref mut _b, _c, mut _d) = tup;
345 fn panic_after_return(a: &Allocator) -> Ptr<'_> {
355 fn panic_after_return_expr(a: &Allocator) -> Ptr<'_> {
365 fn panic_after_init(a: &Allocator) {
375 fn panic_after_init_temp(a: &Allocator) {
385 fn panic_after_init_by_loop(a: &Allocator) {
395 fn panic_after_init_by_match(a: &Allocator, b: bool) {
415 fn panic_after_init_by_match_with_guard(a: &Allocator, b: bool) {
418 let q = match a.alloc() {
432 fn panic_after_init_by_match_with_bindings_and_guard(a: &Allocator, b: bool) {
435 let q = match a.alloc() {
449 fn panic_after_init_by_match_with_ref_bindings_and_guard(a: &Allocator, b: bool) {
452 let q = match a.alloc() {
466 fn panic_after_init_by_break_if(a: &Allocator, b: bool) {
480 fn run_test<F>(mut f: F, name: &'static str)
482 F: FnMut(&Allocator),
484 let first_alloc = Allocator::new(usize::MAX, name);
487 for failing_op in 1..first_alloc.cur_ops.get() + 1 {
488 let alloc = Allocator::new(failing_op, name);
490 let f = panic::AssertUnwindSafe(&mut f);
491 let result = panic::catch_unwind(move || {
496 "test executed {} ops but now {}",
497 first_alloc.cur_ops.get(),
501 if e.downcast_ref::<InjectedFailure>().is_none() {
502 panic::resume_unwind(e);
509 fn run_test_nopanic<F>(mut f: F, name: &'static str)
511 F: FnMut(&Allocator),
513 let first_alloc = Allocator::new(usize::MAX, name);
517 macro_rules! run_test {
519 run_test($e, stringify!($e));
524 run_test!(|a| dynamic_init(a, false));
525 run_test!(|a| dynamic_init(a, true));
526 run_test!(|a| dynamic_drop(a, false));
527 run_test!(|a| dynamic_drop(a, true));
529 run_test!(|a| assignment2(a, false, false));
530 run_test!(|a| assignment2(a, false, true));
531 run_test!(|a| assignment2(a, true, false));
532 run_test!(|a| assignment2(a, true, true));
534 run_test!(|a| assignment1(a, false));
535 run_test!(|a| assignment1(a, true));
537 run_test!(|a| array_simple(a));
538 run_test!(|a| vec_simple(a));
539 run_test!(|a| vec_unreachable(a));
541 run_test!(|a| struct_dynamic_drop(a, false, false, false));
542 run_test!(|a| struct_dynamic_drop(a, false, false, true));
543 run_test!(|a| struct_dynamic_drop(a, false, true, false));
544 run_test!(|a| struct_dynamic_drop(a, false, true, true));
545 run_test!(|a| struct_dynamic_drop(a, true, false, false));
546 run_test!(|a| struct_dynamic_drop(a, true, false, true));
547 run_test!(|a| struct_dynamic_drop(a, true, true, false));
548 run_test!(|a| struct_dynamic_drop(a, true, true, true));
550 run_test!(|a| field_assignment(a, false));
551 run_test!(|a| field_assignment(a, true));
553 run_test!(|a| generator(a, 0));
554 run_test!(|a| generator(a, 1));
555 run_test!(|a| generator(a, 2));
556 run_test!(|a| generator(a, 3));
558 run_test!(|a| mixed_drop_and_nondrop(a));
560 run_test!(|a| slice_pattern_first(a));
561 run_test!(|a| slice_pattern_middle(a));
562 run_test!(|a| slice_pattern_two(a));
563 run_test!(|a| slice_pattern_last(a));
564 run_test!(|a| slice_pattern_one_of(a, 0));
565 run_test!(|a| slice_pattern_one_of(a, 1));
566 run_test!(|a| slice_pattern_one_of(a, 2));
567 run_test!(|a| slice_pattern_one_of(a, 3));
569 run_test!(|a| subslice_pattern_from_end(a, true));
570 run_test!(|a| subslice_pattern_from_end(a, false));
571 run_test!(|a| subslice_pattern_from_end_with_drop(a, true, true));
572 run_test!(|a| subslice_pattern_from_end_with_drop(a, true, false));
573 run_test!(|a| subslice_pattern_from_end_with_drop(a, false, true));
574 run_test!(|a| subslice_pattern_from_end_with_drop(a, false, false));
575 run_test!(|a| slice_pattern_reassign(a));
576 run_test!(|a| subslice_pattern_reassign(a));
578 run_test!(|a| index_field_mixed_ends(a));
579 run_test!(|a| subslice_mixed_min_lengths(a, 0));
580 run_test!(|a| subslice_mixed_min_lengths(a, 1));
581 run_test!(|a| subslice_mixed_min_lengths(a, 2));
582 run_test!(|a| subslice_mixed_min_lengths(a, 3));
583 run_test!(|a| subslice_mixed_min_lengths(a, 4));
584 run_test!(|a| subslice_mixed_min_lengths(a, 5));
585 run_test!(|a| subslice_mixed_min_lengths(a, 6));
586 run_test!(|a| subslice_mixed_min_lengths(a, 7));
588 run_test!(|a| move_ref_pattern(a));
591 panic_after_return(a);
594 panic_after_return_expr(a);
596 run_test!(|a| panic_after_init(a));
597 run_test!(|a| panic_after_init_temp(a));
598 run_test!(|a| panic_after_init_by_loop(a));
599 run_test!(|a| panic_after_init_by_match(a, false));
600 run_test!(|a| panic_after_init_by_match(a, true));
601 run_test!(|a| panic_after_init_by_match_with_guard(a, false));
602 run_test!(|a| panic_after_init_by_match_with_guard(a, true));
603 run_test!(|a| panic_after_init_by_match_with_bindings_and_guard(a, false));
604 run_test!(|a| panic_after_init_by_match_with_bindings_and_guard(a, true));
605 run_test!(|a| panic_after_init_by_match_with_ref_bindings_and_guard(a, false));
606 run_test!(|a| panic_after_init_by_match_with_ref_bindings_and_guard(a, true));
607 run_test!(|a| panic_after_init_by_break_if(a, false));
608 run_test!(|a| panic_after_init_by_break_if(a, true));
610 run_test!(|a| bindings_after_at_dynamic_init_move(a, true));
611 run_test!(|a| bindings_after_at_dynamic_init_move(a, false));
612 run_test!(|a| bindings_after_at_dynamic_init_ref(a, true));
613 run_test!(|a| bindings_after_at_dynamic_init_ref(a, false));
614 run_test!(|a| bindings_after_at_dynamic_drop_move(a, true));
615 run_test!(|a| bindings_after_at_dynamic_drop_move(a, false));
616 run_test!(|a| bindings_after_at_dynamic_drop_ref(a, true));
617 run_test!(|a| bindings_after_at_dynamic_drop_ref(a, false));
619 run_test_nopanic(|a| union1(a), "|a| union1(a)");