2 #![allow(unused_assignments)]
3 #![allow(unused_variables)]
4 // revisions:lexical nll
5 #![cfg_attr(nll, feature(nll))]
7 // ignore-wasm32-bare compiled with panic=abort by default
9 #![feature(generators, generator_trait, untagged_unions)]
10 #![feature(slice_patterns)]
12 use std::cell::{Cell, RefCell};
13 use std::ops::Generator;
18 struct InjectedFailure;
21 data: RefCell<Vec<bool>>,
26 impl panic::UnwindSafe for Allocator {}
27 impl panic::RefUnwindSafe for Allocator {}
29 impl Drop for Allocator {
31 let data = self.data.borrow();
32 if data.iter().any(|d| *d) {
33 panic!("missing free: {:?}", data);
39 fn new(failing_op: usize) -> Self {
41 failing_op: failing_op,
42 cur_ops: Cell::new(0),
43 data: RefCell::new(vec![])
46 fn alloc(&self) -> Ptr {
47 self.cur_ops.set(self.cur_ops.get() + 1);
49 if self.cur_ops.get() == self.failing_op {
50 panic!(InjectedFailure);
53 let mut data = self.data.borrow_mut();
54 let addr = data.len();
60 struct Ptr<'a>(usize, &'a Allocator);
61 impl<'a> Drop for Ptr<'a> {
63 match self.1.data.borrow_mut()[self.0] {
65 panic!("double free at index {:?}", self.0)
67 ref mut d => *d = false
70 self.1.cur_ops.set(self.1.cur_ops.get()+1);
72 if self.1.cur_ops.get() == self.1.failing_op {
73 panic!(InjectedFailure);
78 fn dynamic_init(a: &Allocator, c: bool) {
85 fn dynamic_drop(a: &Allocator, c: bool) {
94 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
95 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
99 if (c0 && i == 0) || (c1 && i == 1) {
100 x = (a.alloc(), a.alloc(), a.alloc());
101 y = TwoPtrs(a.alloc(), a.alloc());
110 fn field_assignment(a: &Allocator, c0: bool) {
111 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
122 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
123 let mut _v = a.alloc();
124 let mut _w = a.alloc();
134 fn assignment1(a: &Allocator, c0: bool) {
135 let mut _v = a.alloc();
136 let mut _w = a.alloc();
143 #[allow(unions_with_drop_fields)]
149 fn union1(a: &Allocator) {
151 let mut u = Boxy { a: a.alloc() };
157 fn array_simple(a: &Allocator) {
158 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
161 fn vec_simple(a: &Allocator) {
162 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
165 fn generator(a: &Allocator, run_count: usize) {
166 assert!(run_count < 4);
175 for _ in 0..run_count {
176 Pin::new(&mut gen).resume();
180 fn mixed_drop_and_nondrop(a: &Allocator) {
181 // check that destructor panics handle drop
182 // and non-drop blocks in the same scope correctly.
184 // Surprisingly enough, this used to not work.
191 #[allow(unreachable_code)]
192 fn vec_unreachable(a: &Allocator) {
193 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
196 fn slice_pattern_first(a: &Allocator) {
197 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
200 fn slice_pattern_middle(a: &Allocator) {
201 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
204 fn slice_pattern_two(a: &Allocator) {
205 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
208 fn slice_pattern_last(a: &Allocator) {
209 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
212 fn slice_pattern_one_of(a: &Allocator, i: usize) {
213 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
215 0 => { let [a, ..] = array; a }
216 1 => { let [_, a, ..] = array; a }
217 2 => { let [_, _, a, _] = array; a }
218 3 => { let [_, _, _, a] = array; a }
219 _ => panic!("unmatched"),
223 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
224 let a = [a.alloc(), a.alloc(), a.alloc()];
232 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
233 let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
246 fn slice_pattern_reassign(a: &Allocator) {
247 let mut ar = [a.alloc(), a.alloc()];
249 ar = [a.alloc(), a.alloc()];
253 fn subslice_pattern_reassign(a: &Allocator) {
254 let mut ar = [a.alloc(), a.alloc(), a.alloc()];
256 ar = [a.alloc(), a.alloc(), a.alloc()];
260 fn run_test<F>(mut f: F)
261 where F: FnMut(&Allocator)
263 let first_alloc = Allocator::new(usize::MAX);
266 for failing_op in 1..first_alloc.cur_ops.get()+1 {
267 let alloc = Allocator::new(failing_op);
269 let f = panic::AssertUnwindSafe(&mut f);
270 let result = panic::catch_unwind(move || {
274 Ok(..) => panic!("test executed {} ops but now {}",
275 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
277 if e.downcast_ref::<InjectedFailure>().is_none() {
278 panic::resume_unwind(e);
285 fn run_test_nopanic<F>(mut f: F)
286 where F: FnMut(&Allocator)
288 let first_alloc = Allocator::new(usize::MAX);
293 run_test(|a| dynamic_init(a, false));
294 run_test(|a| dynamic_init(a, true));
295 run_test(|a| dynamic_drop(a, false));
296 run_test(|a| dynamic_drop(a, true));
298 run_test(|a| assignment2(a, false, false));
299 run_test(|a| assignment2(a, false, true));
300 run_test(|a| assignment2(a, true, false));
301 run_test(|a| assignment2(a, true, true));
303 run_test(|a| assignment1(a, false));
304 run_test(|a| assignment1(a, true));
306 run_test(|a| array_simple(a));
307 run_test(|a| vec_simple(a));
308 run_test(|a| vec_unreachable(a));
310 run_test(|a| struct_dynamic_drop(a, false, false, false));
311 run_test(|a| struct_dynamic_drop(a, false, false, true));
312 run_test(|a| struct_dynamic_drop(a, false, true, false));
313 run_test(|a| struct_dynamic_drop(a, false, true, true));
314 run_test(|a| struct_dynamic_drop(a, true, false, false));
315 run_test(|a| struct_dynamic_drop(a, true, false, true));
316 run_test(|a| struct_dynamic_drop(a, true, true, false));
317 run_test(|a| struct_dynamic_drop(a, true, true, true));
319 run_test(|a| field_assignment(a, false));
320 run_test(|a| field_assignment(a, true));
322 run_test(|a| generator(a, 0));
323 run_test(|a| generator(a, 1));
324 run_test(|a| generator(a, 2));
325 run_test(|a| generator(a, 3));
327 run_test(|a| mixed_drop_and_nondrop(a));
329 run_test(|a| slice_pattern_first(a));
330 run_test(|a| slice_pattern_middle(a));
331 run_test(|a| slice_pattern_two(a));
332 run_test(|a| slice_pattern_last(a));
333 run_test(|a| slice_pattern_one_of(a, 0));
334 run_test(|a| slice_pattern_one_of(a, 1));
335 run_test(|a| slice_pattern_one_of(a, 2));
336 run_test(|a| slice_pattern_one_of(a, 3));
338 run_test(|a| subslice_pattern_from_end(a, true));
339 run_test(|a| subslice_pattern_from_end(a, false));
340 run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
341 run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
342 run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
343 run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
344 run_test(|a| slice_pattern_reassign(a));
345 run_test(|a| subslice_pattern_reassign(a));
347 run_test_nopanic(|a| union1(a));