1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 // revisions:lexical nll
13 #![cfg_attr(nll, feature(nll))]
15 // ignore-wasm32-bare compiled with panic=abort by default
17 #![feature(generators, generator_trait, untagged_unions)]
18 #![feature(slice_patterns)]
20 use std::cell::{Cell, RefCell};
21 use std::ops::Generator;
25 struct InjectedFailure;
28 data: RefCell<Vec<bool>>,
33 impl panic::UnwindSafe for Allocator {}
34 impl panic::RefUnwindSafe for Allocator {}
36 impl Drop for Allocator {
38 let data = self.data.borrow();
39 if data.iter().any(|d| *d) {
40 panic!("missing free: {:?}", data);
46 fn new(failing_op: usize) -> Self {
48 failing_op: failing_op,
49 cur_ops: Cell::new(0),
50 data: RefCell::new(vec![])
53 fn alloc(&self) -> Ptr {
54 self.cur_ops.set(self.cur_ops.get() + 1);
56 if self.cur_ops.get() == self.failing_op {
57 panic!(InjectedFailure);
60 let mut data = self.data.borrow_mut();
61 let addr = data.len();
67 struct Ptr<'a>(usize, &'a Allocator);
68 impl<'a> Drop for Ptr<'a> {
70 match self.1.data.borrow_mut()[self.0] {
72 panic!("double free at index {:?}", self.0)
74 ref mut d => *d = false
77 self.1.cur_ops.set(self.1.cur_ops.get()+1);
79 if self.1.cur_ops.get() == self.1.failing_op {
80 panic!(InjectedFailure);
85 fn dynamic_init(a: &Allocator, c: bool) {
92 fn dynamic_drop(a: &Allocator, c: bool) {
101 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
102 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
106 if (c0 && i == 0) || (c1 && i == 1) {
107 x = (a.alloc(), a.alloc(), a.alloc());
108 y = TwoPtrs(a.alloc(), a.alloc());
117 fn field_assignment(a: &Allocator, c0: bool) {
118 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
129 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
130 let mut _v = a.alloc();
131 let mut _w = a.alloc();
141 fn assignment1(a: &Allocator, c0: bool) {
142 let mut _v = a.alloc();
143 let mut _w = a.alloc();
150 #[allow(unions_with_drop_fields)]
156 fn union1(a: &Allocator) {
158 let mut u = Boxy { a: a.alloc() };
164 fn array_simple(a: &Allocator) {
165 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
168 fn vec_simple(a: &Allocator) {
169 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
172 fn generator(a: &Allocator, run_count: usize) {
173 assert!(run_count < 4);
182 for _ in 0..run_count {
183 unsafe { gen.resume(); }
187 fn mixed_drop_and_nondrop(a: &Allocator) {
188 // check that destructor panics handle drop
189 // and non-drop blocks in the same scope correctly.
191 // Surprisingly enough, this used to not work.
198 #[allow(unreachable_code)]
199 fn vec_unreachable(a: &Allocator) {
200 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
203 fn slice_pattern_first(a: &Allocator) {
204 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
207 fn slice_pattern_middle(a: &Allocator) {
208 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
211 fn slice_pattern_two(a: &Allocator) {
212 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
215 fn slice_pattern_last(a: &Allocator) {
216 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
219 fn slice_pattern_one_of(a: &Allocator, i: usize) {
220 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
222 0 => { let [a, ..] = array; a }
223 1 => { let [_, a, ..] = array; a }
224 2 => { let [_, _, a, _] = array; a }
225 3 => { let [_, _, _, a] = array; a }
226 _ => panic!("unmatched"),
230 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
231 let a = [a.alloc(), a.alloc(), a.alloc()];
239 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
240 let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
253 fn slice_pattern_reassign(a: &Allocator) {
254 let mut ar = [a.alloc(), a.alloc()];
256 ar = [a.alloc(), a.alloc()];
260 fn subslice_pattern_reassign(a: &Allocator) {
261 let mut ar = [a.alloc(), a.alloc(), a.alloc()];
263 ar = [a.alloc(), a.alloc(), a.alloc()];
267 fn run_test<F>(mut f: F)
268 where F: FnMut(&Allocator)
270 let first_alloc = Allocator::new(usize::MAX);
273 for failing_op in 1..first_alloc.cur_ops.get()+1 {
274 let alloc = Allocator::new(failing_op);
276 let f = panic::AssertUnwindSafe(&mut f);
277 let result = panic::catch_unwind(move || {
281 Ok(..) => panic!("test executed {} ops but now {}",
282 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
284 if e.downcast_ref::<InjectedFailure>().is_none() {
285 panic::resume_unwind(e);
292 fn run_test_nopanic<F>(mut f: F)
293 where F: FnMut(&Allocator)
295 let first_alloc = Allocator::new(usize::MAX);
300 run_test(|a| dynamic_init(a, false));
301 run_test(|a| dynamic_init(a, true));
302 run_test(|a| dynamic_drop(a, false));
303 run_test(|a| dynamic_drop(a, true));
305 run_test(|a| assignment2(a, false, false));
306 run_test(|a| assignment2(a, false, true));
307 run_test(|a| assignment2(a, true, false));
308 run_test(|a| assignment2(a, true, true));
310 run_test(|a| assignment1(a, false));
311 run_test(|a| assignment1(a, true));
313 run_test(|a| array_simple(a));
314 run_test(|a| vec_simple(a));
315 run_test(|a| vec_unreachable(a));
317 run_test(|a| struct_dynamic_drop(a, false, false, false));
318 run_test(|a| struct_dynamic_drop(a, false, false, true));
319 run_test(|a| struct_dynamic_drop(a, false, true, false));
320 run_test(|a| struct_dynamic_drop(a, false, true, true));
321 run_test(|a| struct_dynamic_drop(a, true, false, false));
322 run_test(|a| struct_dynamic_drop(a, true, false, true));
323 run_test(|a| struct_dynamic_drop(a, true, true, false));
324 run_test(|a| struct_dynamic_drop(a, true, true, true));
326 run_test(|a| field_assignment(a, false));
327 run_test(|a| field_assignment(a, true));
329 run_test(|a| generator(a, 0));
330 run_test(|a| generator(a, 1));
331 run_test(|a| generator(a, 2));
332 run_test(|a| generator(a, 3));
334 run_test(|a| mixed_drop_and_nondrop(a));
336 run_test(|a| slice_pattern_first(a));
337 run_test(|a| slice_pattern_middle(a));
338 run_test(|a| slice_pattern_two(a));
339 run_test(|a| slice_pattern_last(a));
340 run_test(|a| slice_pattern_one_of(a, 0));
341 run_test(|a| slice_pattern_one_of(a, 1));
342 run_test(|a| slice_pattern_one_of(a, 2));
343 run_test(|a| slice_pattern_one_of(a, 3));
345 run_test(|a| subslice_pattern_from_end(a, true));
346 run_test(|a| subslice_pattern_from_end(a, false));
347 run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
348 run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
349 run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
350 run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
351 run_test(|a| slice_pattern_reassign(a));
352 run_test(|a| subslice_pattern_reassign(a));
354 run_test_nopanic(|a| union1(a));