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();
58 struct Ptr<'a>(usize, &'a Allocator);
59 impl<'a> Drop for Ptr<'a> {
61 match self.1.data.borrow_mut()[self.0] {
63 panic!("double free at index {:?}", self.0)
65 ref mut d => *d = false
68 self.1.cur_ops.set(self.1.cur_ops.get()+1);
70 if self.1.cur_ops.get() == self.1.failing_op {
71 panic!(InjectedFailure);
76 fn dynamic_init(a: &Allocator, c: bool) {
83 fn dynamic_drop(a: &Allocator, c: bool) {
92 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
93 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
97 if (c0 && i == 0) || (c1 && i == 1) {
98 x = (a.alloc(), a.alloc(), a.alloc());
99 y = TwoPtrs(a.alloc(), a.alloc());
108 fn field_assignment(a: &Allocator, c0: bool) {
109 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
120 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
121 let mut _v = a.alloc();
122 let mut _w = a.alloc();
132 fn assignment1(a: &Allocator, c0: bool) {
133 let mut _v = a.alloc();
134 let mut _w = a.alloc();
141 #[allow(unions_with_drop_fields)]
147 fn union1(a: &Allocator) {
149 let mut u = Boxy { a: a.alloc() };
155 fn array_simple(a: &Allocator) {
156 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
159 fn vec_simple(a: &Allocator) {
160 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
163 fn generator(a: &Allocator, run_count: usize) {
164 assert!(run_count < 4);
173 for _ in 0..run_count {
174 Pin::new(&mut gen).resume();
178 fn mixed_drop_and_nondrop(a: &Allocator) {
179 // check that destructor panics handle drop
180 // and non-drop blocks in the same scope correctly.
182 // Surprisingly enough, this used to not work.
189 #[allow(unreachable_code)]
190 fn vec_unreachable(a: &Allocator) {
191 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
194 fn slice_pattern_first(a: &Allocator) {
195 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
198 fn slice_pattern_middle(a: &Allocator) {
199 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
202 fn slice_pattern_two(a: &Allocator) {
203 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
206 fn slice_pattern_last(a: &Allocator) {
207 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
210 fn slice_pattern_one_of(a: &Allocator, i: usize) {
211 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
213 0 => { let [a, ..] = array; a }
214 1 => { let [_, a, ..] = array; a }
215 2 => { let [_, _, a, _] = array; a }
216 3 => { let [_, _, _, a] = array; a }
217 _ => panic!("unmatched"),
221 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
222 let a = [a.alloc(), a.alloc(), a.alloc()];
230 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
231 let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
244 fn slice_pattern_reassign(a: &Allocator) {
245 let mut ar = [a.alloc(), a.alloc()];
247 ar = [a.alloc(), a.alloc()];
251 fn subslice_pattern_reassign(a: &Allocator) {
252 let mut ar = [a.alloc(), a.alloc(), a.alloc()];
254 ar = [a.alloc(), a.alloc(), a.alloc()];
258 fn run_test<F>(mut f: F)
259 where F: FnMut(&Allocator)
261 let first_alloc = Allocator::new(usize::MAX);
264 for failing_op in 1..first_alloc.cur_ops.get()+1 {
265 let alloc = Allocator::new(failing_op);
267 let f = panic::AssertUnwindSafe(&mut f);
268 let result = panic::catch_unwind(move || {
272 Ok(..) => panic!("test executed {} ops but now {}",
273 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
275 if e.downcast_ref::<InjectedFailure>().is_none() {
276 panic::resume_unwind(e);
283 fn run_test_nopanic<F>(mut f: F)
284 where F: FnMut(&Allocator)
286 let first_alloc = Allocator::new(usize::MAX);
291 run_test(|a| dynamic_init(a, false));
292 run_test(|a| dynamic_init(a, true));
293 run_test(|a| dynamic_drop(a, false));
294 run_test(|a| dynamic_drop(a, true));
296 run_test(|a| assignment2(a, false, false));
297 run_test(|a| assignment2(a, false, true));
298 run_test(|a| assignment2(a, true, false));
299 run_test(|a| assignment2(a, true, true));
301 run_test(|a| assignment1(a, false));
302 run_test(|a| assignment1(a, true));
304 run_test(|a| array_simple(a));
305 run_test(|a| vec_simple(a));
306 run_test(|a| vec_unreachable(a));
308 run_test(|a| struct_dynamic_drop(a, false, false, false));
309 run_test(|a| struct_dynamic_drop(a, false, false, true));
310 run_test(|a| struct_dynamic_drop(a, false, true, false));
311 run_test(|a| struct_dynamic_drop(a, false, true, true));
312 run_test(|a| struct_dynamic_drop(a, true, false, false));
313 run_test(|a| struct_dynamic_drop(a, true, false, true));
314 run_test(|a| struct_dynamic_drop(a, true, true, false));
315 run_test(|a| struct_dynamic_drop(a, true, true, true));
317 run_test(|a| field_assignment(a, false));
318 run_test(|a| field_assignment(a, true));
320 run_test(|a| generator(a, 0));
321 run_test(|a| generator(a, 1));
322 run_test(|a| generator(a, 2));
323 run_test(|a| generator(a, 3));
325 run_test(|a| mixed_drop_and_nondrop(a));
327 run_test(|a| slice_pattern_first(a));
328 run_test(|a| slice_pattern_middle(a));
329 run_test(|a| slice_pattern_two(a));
330 run_test(|a| slice_pattern_last(a));
331 run_test(|a| slice_pattern_one_of(a, 0));
332 run_test(|a| slice_pattern_one_of(a, 1));
333 run_test(|a| slice_pattern_one_of(a, 2));
334 run_test(|a| slice_pattern_one_of(a, 3));
336 run_test(|a| subslice_pattern_from_end(a, true));
337 run_test(|a| subslice_pattern_from_end(a, false));
338 run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
339 run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
340 run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
341 run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
342 run_test(|a| slice_pattern_reassign(a));
343 run_test(|a| subslice_pattern_reassign(a));
345 run_test_nopanic(|a| union1(a));