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;
17 struct InjectedFailure;
20 data: RefCell<Vec<bool>>,
25 impl panic::UnwindSafe for Allocator {}
26 impl panic::RefUnwindSafe for Allocator {}
28 impl Drop for Allocator {
30 let data = self.data.borrow();
31 if data.iter().any(|d| *d) {
32 panic!("missing free: {:?}", data);
38 fn new(failing_op: usize) -> Self {
40 failing_op: failing_op,
41 cur_ops: Cell::new(0),
42 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!(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] {
64 panic!("double free at index {:?}", self.0)
66 ref mut d => *d = false
69 self.1.cur_ops.set(self.1.cur_ops.get()+1);
71 if self.1.cur_ops.get() == self.1.failing_op {
72 panic!(InjectedFailure);
77 fn dynamic_init(a: &Allocator, c: bool) {
84 fn dynamic_drop(a: &Allocator, c: bool) {
93 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
94 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
98 if (c0 && i == 0) || (c1 && i == 1) {
99 x = (a.alloc(), a.alloc(), a.alloc());
100 y = TwoPtrs(a.alloc(), a.alloc());
109 fn field_assignment(a: &Allocator, c0: bool) {
110 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
121 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
122 let mut _v = a.alloc();
123 let mut _w = a.alloc();
133 fn assignment1(a: &Allocator, c0: bool) {
134 let mut _v = a.alloc();
135 let mut _w = a.alloc();
142 #[allow(unions_with_drop_fields)]
148 fn union1(a: &Allocator) {
150 let mut u = Boxy { a: a.alloc() };
156 fn array_simple(a: &Allocator) {
157 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
160 fn vec_simple(a: &Allocator) {
161 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
164 fn generator(a: &Allocator, run_count: usize) {
165 assert!(run_count < 4);
174 for _ in 0..run_count {
175 unsafe { gen.resume(); }
179 fn mixed_drop_and_nondrop(a: &Allocator) {
180 // check that destructor panics handle drop
181 // and non-drop blocks in the same scope correctly.
183 // Surprisingly enough, this used to not work.
190 #[allow(unreachable_code)]
191 fn vec_unreachable(a: &Allocator) {
192 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
195 fn slice_pattern_first(a: &Allocator) {
196 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
199 fn slice_pattern_middle(a: &Allocator) {
200 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
203 fn slice_pattern_two(a: &Allocator) {
204 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
207 fn slice_pattern_last(a: &Allocator) {
208 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
211 fn slice_pattern_one_of(a: &Allocator, i: usize) {
212 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
214 0 => { let [a, ..] = array; a }
215 1 => { let [_, a, ..] = array; a }
216 2 => { let [_, _, a, _] = array; a }
217 3 => { let [_, _, _, a] = array; a }
218 _ => panic!("unmatched"),
222 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
223 let a = [a.alloc(), a.alloc(), a.alloc()];
231 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
232 let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
245 fn slice_pattern_reassign(a: &Allocator) {
246 let mut ar = [a.alloc(), a.alloc()];
248 ar = [a.alloc(), a.alloc()];
252 fn subslice_pattern_reassign(a: &Allocator) {
253 let mut ar = [a.alloc(), a.alloc(), a.alloc()];
255 ar = [a.alloc(), a.alloc(), a.alloc()];
259 fn run_test<F>(mut f: F)
260 where F: FnMut(&Allocator)
262 let first_alloc = Allocator::new(usize::MAX);
265 for failing_op in 1..first_alloc.cur_ops.get()+1 {
266 let alloc = Allocator::new(failing_op);
268 let f = panic::AssertUnwindSafe(&mut f);
269 let result = panic::catch_unwind(move || {
273 Ok(..) => panic!("test executed {} ops but now {}",
274 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
276 if e.downcast_ref::<InjectedFailure>().is_none() {
277 panic::resume_unwind(e);
284 fn run_test_nopanic<F>(mut f: F)
285 where F: FnMut(&Allocator)
287 let first_alloc = Allocator::new(usize::MAX);
292 run_test(|a| dynamic_init(a, false));
293 run_test(|a| dynamic_init(a, true));
294 run_test(|a| dynamic_drop(a, false));
295 run_test(|a| dynamic_drop(a, true));
297 run_test(|a| assignment2(a, false, false));
298 run_test(|a| assignment2(a, false, true));
299 run_test(|a| assignment2(a, true, false));
300 run_test(|a| assignment2(a, true, true));
302 run_test(|a| assignment1(a, false));
303 run_test(|a| assignment1(a, true));
305 run_test(|a| array_simple(a));
306 run_test(|a| vec_simple(a));
307 run_test(|a| vec_unreachable(a));
309 run_test(|a| struct_dynamic_drop(a, false, false, false));
310 run_test(|a| struct_dynamic_drop(a, false, false, true));
311 run_test(|a| struct_dynamic_drop(a, false, true, false));
312 run_test(|a| struct_dynamic_drop(a, false, true, true));
313 run_test(|a| struct_dynamic_drop(a, true, false, false));
314 run_test(|a| struct_dynamic_drop(a, true, false, true));
315 run_test(|a| struct_dynamic_drop(a, true, true, false));
316 run_test(|a| struct_dynamic_drop(a, true, true, true));
318 run_test(|a| field_assignment(a, false));
319 run_test(|a| field_assignment(a, true));
321 run_test(|a| generator(a, 0));
322 run_test(|a| generator(a, 1));
323 run_test(|a| generator(a, 2));
324 run_test(|a| generator(a, 3));
326 run_test(|a| mixed_drop_and_nondrop(a));
328 run_test(|a| slice_pattern_first(a));
329 run_test(|a| slice_pattern_middle(a));
330 run_test(|a| slice_pattern_two(a));
331 run_test(|a| slice_pattern_last(a));
332 run_test(|a| slice_pattern_one_of(a, 0));
333 run_test(|a| slice_pattern_one_of(a, 1));
334 run_test(|a| slice_pattern_one_of(a, 2));
335 run_test(|a| slice_pattern_one_of(a, 3));
337 run_test(|a| subslice_pattern_from_end(a, true));
338 run_test(|a| subslice_pattern_from_end(a, false));
339 run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
340 run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
341 run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
342 run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
343 run_test(|a| slice_pattern_reassign(a));
344 run_test(|a| subslice_pattern_reassign(a));
346 run_test_nopanic(|a| union1(a));