2 // ignore-wasm32-bare compiled with panic=abort by default
4 #![feature(generators, generator_trait)]
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;
16 struct InjectedFailure;
19 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: {:?}", data);
37 fn new(failing_op: usize) -> Self {
39 failing_op: failing_op,
40 cur_ops: Cell::new(0),
41 data: RefCell::new(vec![])
44 fn alloc(&self) -> Ptr<'_> {
45 self.cur_ops.set(self.cur_ops.get() + 1);
47 if self.cur_ops.get() == self.failing_op {
48 panic!(InjectedFailure);
51 let mut data = self.data.borrow_mut();
52 let addr = data.len();
56 // FIXME(#47949) Any use of this indicates a bug in rustc: we should never
57 // be leaking values in the cases here.
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();
64 if exceptions.iter().any(|operation| *operation == self.failing_op) {
65 let mut data = self.data.borrow_mut();
72 struct Ptr<'a>(usize, &'a Allocator);
73 impl<'a> Drop for Ptr<'a> {
75 match self.1.data.borrow_mut()[self.0] {
77 panic!("double free at index {:?}", self.0)
79 ref mut d => *d = false
82 self.1.cur_ops.set(self.1.cur_ops.get()+1);
84 if self.1.cur_ops.get() == self.1.failing_op {
85 panic!(InjectedFailure);
90 fn dynamic_init(a: &Allocator, c: bool) {
97 fn dynamic_drop(a: &Allocator, c: bool) {
106 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
107 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
111 if (c0 && i == 0) || (c1 && i == 1) {
112 x = (a.alloc(), a.alloc(), a.alloc());
113 y = TwoPtrs(a.alloc(), a.alloc());
122 fn field_assignment(a: &Allocator, c0: bool) {
123 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
134 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
135 let mut _v = a.alloc();
136 let mut _w = a.alloc();
146 fn assignment1(a: &Allocator, c0: bool) {
147 let mut _v = a.alloc();
148 let mut _w = a.alloc();
160 fn union1(a: &Allocator) {
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));
168 fn array_simple(a: &Allocator) {
169 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
172 fn vec_simple(a: &Allocator) {
173 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
176 fn generator(a: &Allocator, run_count: usize) {
177 assert!(run_count < 4);
186 for _ in 0..run_count {
187 Pin::new(&mut gen).resume(());
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.
195 // Surprisingly enough, this used to not work.
202 #[allow(unreachable_code)]
203 fn vec_unreachable(a: &Allocator) {
204 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
207 fn slice_pattern_first(a: &Allocator) {
208 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
211 fn slice_pattern_middle(a: &Allocator) {
212 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
215 fn slice_pattern_two(a: &Allocator) {
216 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
219 fn slice_pattern_last(a: &Allocator) {
220 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
223 fn slice_pattern_one_of(a: &Allocator, i: usize) {
224 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
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"),
234 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
235 let a = [a.alloc(), a.alloc(), a.alloc()];
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()];
257 fn slice_pattern_reassign(a: &Allocator) {
258 let mut ar = [a.alloc(), a.alloc()];
260 ar = [a.alloc(), a.alloc()];
264 fn subslice_pattern_reassign(a: &Allocator) {
265 let mut ar = [a.alloc(), a.alloc(), a.alloc()];
267 ar = [a.alloc(), a.alloc(), a.alloc()];
268 let[_, _y @ ..] = ar;
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;
279 fn subslice_mixed_min_lengths(a: &Allocator, c: i32) {
280 let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())];
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; }
293 fn bindings_after_at_dynamic_init_move(a: &Allocator, c: bool) {
294 let foo = if c { Some(a.alloc()) } else { None };
297 if let bar @ Some(_) = foo {
302 fn bindings_after_at_dynamic_init_ref(a: &Allocator, c: bool) {
303 let foo = if c { Some(a.alloc()) } else { None };
306 if let bar @ Some(_baz) = &foo {
311 fn bindings_after_at_dynamic_drop_move(a: &Allocator, c: bool) {
312 let foo = if c { Some(a.alloc()) } else { None };
314 if let bar @ Some(_) = foo {
321 fn bindings_after_at_dynamic_drop_ref(a: &Allocator, c: bool) {
322 let foo = if c { Some(a.alloc()) } else { None };
324 if let bar @ Some(_baz) = &foo {
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;
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];
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)
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];
359 return a.alloc_leaked(exceptions);
363 fn panic_after_init(a: &Allocator) {
364 // Panic in the drop of `r` can leak
365 let exceptions = vec![8];
372 a.alloc_leaked(exceptions)
376 fn panic_after_init_temp(a: &Allocator) {
377 // Panic in the drop of `r` can leak
378 let exceptions = vec![8];
385 a.alloc_leaked(exceptions)
389 fn panic_after_init_by_loop(a: &Allocator) {
390 // Panic in the drop of `r` can leak
391 let exceptions = vec![8];
398 break a.alloc_leaked(exceptions);
402 fn run_test<F>(mut f: F)
403 where F: FnMut(&Allocator)
405 let first_alloc = Allocator::new(usize::MAX);
408 for failing_op in 1..first_alloc.cur_ops.get()+1 {
409 let alloc = Allocator::new(failing_op);
411 let f = panic::AssertUnwindSafe(&mut f);
412 let result = panic::catch_unwind(move || {
416 Ok(..) => panic!("test executed {} ops but now {}",
417 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
419 if e.downcast_ref::<InjectedFailure>().is_none() {
420 panic::resume_unwind(e);
427 fn run_test_nopanic<F>(mut f: F)
428 where F: FnMut(&Allocator)
430 let first_alloc = Allocator::new(usize::MAX);
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));
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));
445 run_test(|a| assignment1(a, false));
446 run_test(|a| assignment1(a, true));
448 run_test(|a| array_simple(a));
449 run_test(|a| vec_simple(a));
450 run_test(|a| vec_unreachable(a));
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));
461 run_test(|a| field_assignment(a, false));
462 run_test(|a| field_assignment(a, true));
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));
469 run_test(|a| mixed_drop_and_nondrop(a));
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));
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));
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));
499 run_test(|a| move_ref_pattern(a));
502 panic_after_return(a);
505 panic_after_return_expr(a);
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));
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));
520 run_test_nopanic(|a| union1(a));