2 #![allow(unused_assignments)]
3 #![allow(unused_variables)]
5 // ignore-wasm32-bare compiled with panic=abort by default
7 #![feature(generators, generator_trait, untagged_unions)]
8 #![feature(slice_patterns)]
10 use std::cell::{Cell, RefCell};
11 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();
155 #[allow(unions_with_drop_fields)]
161 fn union1(a: &Allocator) {
163 let mut u = Boxy { a: a.alloc() };
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 panic_after_return(a: &Allocator) -> Ptr<'_> {
273 // Panic in the drop of `p` or `q` can leak
274 let exceptions = vec![8, 9];
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)
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];
295 return a.alloc_leaked(exceptions);
299 fn panic_after_init(a: &Allocator) {
300 // Panic in the drop of `r` can leak
301 let exceptions = vec![8];
308 a.alloc_leaked(exceptions)
312 fn panic_after_init_temp(a: &Allocator) {
313 // Panic in the drop of `r` can leak
314 let exceptions = vec![8];
321 a.alloc_leaked(exceptions)
325 fn panic_after_init_by_loop(a: &Allocator) {
326 // Panic in the drop of `r` can leak
327 let exceptions = vec![8];
334 break a.alloc_leaked(exceptions);
338 fn run_test<F>(mut f: F)
339 where F: FnMut(&Allocator)
341 let first_alloc = Allocator::new(usize::MAX);
344 for failing_op in 1..first_alloc.cur_ops.get()+1 {
345 let alloc = Allocator::new(failing_op);
347 let f = panic::AssertUnwindSafe(&mut f);
348 let result = panic::catch_unwind(move || {
352 Ok(..) => panic!("test executed {} ops but now {}",
353 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
355 if e.downcast_ref::<InjectedFailure>().is_none() {
356 panic::resume_unwind(e);
363 fn run_test_nopanic<F>(mut f: F)
364 where F: FnMut(&Allocator)
366 let first_alloc = Allocator::new(usize::MAX);
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));
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));
381 run_test(|a| assignment1(a, false));
382 run_test(|a| assignment1(a, true));
384 run_test(|a| array_simple(a));
385 run_test(|a| vec_simple(a));
386 run_test(|a| vec_unreachable(a));
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));
397 run_test(|a| field_assignment(a, false));
398 run_test(|a| field_assignment(a, true));
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));
405 run_test(|a| mixed_drop_and_nondrop(a));
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));
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));
426 panic_after_return(a);
429 panic_after_return_expr(a);
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));
435 run_test_nopanic(|a| union1(a));