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.
11 // revisions:lexical nll
12 #![cfg_attr(nll, feature(nll))]
14 // ignore-wasm32-bare compiled with panic=abort by default
16 #![feature(generators, generator_trait, untagged_unions, slice_patterns, advanced_slice_patterns)]
18 use std::cell::{Cell, RefCell};
19 use std::ops::Generator;
23 struct InjectedFailure;
26 data: RefCell<Vec<bool>>,
31 impl panic::UnwindSafe for Allocator {}
32 impl panic::RefUnwindSafe for Allocator {}
34 impl Drop for Allocator {
36 let data = self.data.borrow();
37 if data.iter().any(|d| *d) {
38 panic!("missing free: {:?}", data);
44 fn new(failing_op: usize) -> Self {
46 failing_op: failing_op,
47 cur_ops: Cell::new(0),
48 data: RefCell::new(vec![])
51 fn alloc(&self) -> Ptr {
52 self.cur_ops.set(self.cur_ops.get() + 1);
54 if self.cur_ops.get() == self.failing_op {
55 panic!(InjectedFailure);
58 let mut data = self.data.borrow_mut();
59 let addr = data.len();
65 struct Ptr<'a>(usize, &'a Allocator);
66 impl<'a> Drop for Ptr<'a> {
68 match self.1.data.borrow_mut()[self.0] {
70 panic!("double free at index {:?}", self.0)
72 ref mut d => *d = false
75 self.1.cur_ops.set(self.1.cur_ops.get()+1);
77 if self.1.cur_ops.get() == self.1.failing_op {
78 panic!(InjectedFailure);
83 fn dynamic_init(a: &Allocator, c: bool) {
90 fn dynamic_drop(a: &Allocator, c: bool) {
99 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
100 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
104 if (c0 && i == 0) || (c1 && i == 1) {
105 x = (a.alloc(), a.alloc(), a.alloc());
106 y = TwoPtrs(a.alloc(), a.alloc());
115 fn field_assignment(a: &Allocator, c0: bool) {
116 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
127 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
128 let mut _v = a.alloc();
129 let mut _w = a.alloc();
139 fn assignment1(a: &Allocator, c0: bool) {
140 let mut _v = a.alloc();
141 let mut _w = a.alloc();
148 #[allow(unions_with_drop_fields)]
154 fn union1(a: &Allocator) {
156 let mut u = Boxy { a: a.alloc() };
162 fn array_simple(a: &Allocator) {
163 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
166 fn vec_simple(a: &Allocator) {
167 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
170 fn generator(a: &Allocator, run_count: usize) {
171 assert!(run_count < 4);
180 for _ in 0..run_count {
185 fn mixed_drop_and_nondrop(a: &Allocator) {
186 // check that destructor panics handle drop
187 // and non-drop blocks in the same scope correctly.
189 // Surprisingly enough, this used to not work.
196 #[allow(unreachable_code)]
197 fn vec_unreachable(a: &Allocator) {
198 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
201 fn slice_pattern_first(a: &Allocator) {
202 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
205 fn slice_pattern_middle(a: &Allocator) {
206 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
209 fn slice_pattern_two(a: &Allocator) {
210 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
213 fn slice_pattern_last(a: &Allocator) {
214 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
217 fn slice_pattern_one_of(a: &Allocator, i: usize) {
218 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
220 0 => { let [a, ..] = array; a }
221 1 => { let [_, a, ..] = array; a }
222 2 => { let [_, _, a, _] = array; a }
223 3 => { let [_, _, _, a] = array; a }
224 _ => panic!("unmatched"),
228 fn run_test<F>(mut f: F)
229 where F: FnMut(&Allocator)
231 let first_alloc = Allocator::new(usize::MAX);
234 for failing_op in 1..first_alloc.cur_ops.get()+1 {
235 let alloc = Allocator::new(failing_op);
237 let f = panic::AssertUnwindSafe(&mut f);
238 let result = panic::catch_unwind(move || {
242 Ok(..) => panic!("test executed {} ops but now {}",
243 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
245 if e.downcast_ref::<InjectedFailure>().is_none() {
246 panic::resume_unwind(e);
253 fn run_test_nopanic<F>(mut f: F)
254 where F: FnMut(&Allocator)
256 let first_alloc = Allocator::new(usize::MAX);
261 run_test(|a| dynamic_init(a, false));
262 run_test(|a| dynamic_init(a, true));
263 run_test(|a| dynamic_drop(a, false));
264 run_test(|a| dynamic_drop(a, true));
266 run_test(|a| assignment2(a, false, false));
267 run_test(|a| assignment2(a, false, true));
268 run_test(|a| assignment2(a, true, false));
269 run_test(|a| assignment2(a, true, true));
271 run_test(|a| assignment1(a, false));
272 run_test(|a| assignment1(a, true));
274 run_test(|a| array_simple(a));
275 run_test(|a| vec_simple(a));
276 run_test(|a| vec_unreachable(a));
278 run_test(|a| struct_dynamic_drop(a, false, false, false));
279 run_test(|a| struct_dynamic_drop(a, false, false, true));
280 run_test(|a| struct_dynamic_drop(a, false, true, false));
281 run_test(|a| struct_dynamic_drop(a, false, true, true));
282 run_test(|a| struct_dynamic_drop(a, true, false, false));
283 run_test(|a| struct_dynamic_drop(a, true, false, true));
284 run_test(|a| struct_dynamic_drop(a, true, true, false));
285 run_test(|a| struct_dynamic_drop(a, true, true, true));
287 run_test(|a| field_assignment(a, false));
288 run_test(|a| field_assignment(a, true));
290 run_test(|a| generator(a, 0));
291 run_test(|a| generator(a, 1));
292 run_test(|a| generator(a, 2));
293 run_test(|a| generator(a, 3));
295 run_test(|a| mixed_drop_and_nondrop(a));
297 run_test(|a| slice_pattern_first(a));
298 run_test(|a| slice_pattern_middle(a));
299 run_test(|a| slice_pattern_two(a));
300 run_test(|a| slice_pattern_last(a));
301 run_test(|a| slice_pattern_one_of(a, 0));
302 run_test(|a| slice_pattern_one_of(a, 1));
303 run_test(|a| slice_pattern_one_of(a, 2));
304 run_test(|a| slice_pattern_one_of(a, 3));
306 run_test_nopanic(|a| union1(a));