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 // ignore-wasm32-bare compiled with panic=abort by default
13 #![feature(generators, generator_trait, untagged_unions, slice_patterns, advanced_slice_patterns)]
15 use std::cell::{Cell, RefCell};
16 use std::ops::Generator;
20 struct InjectedFailure;
23 data: RefCell<Vec<bool>>,
28 impl panic::UnwindSafe for Allocator {}
29 impl panic::RefUnwindSafe for Allocator {}
31 impl Drop for Allocator {
33 let data = self.data.borrow();
34 if data.iter().any(|d| *d) {
35 panic!("missing free: {:?}", data);
41 fn new(failing_op: usize) -> Self {
43 failing_op: failing_op,
44 cur_ops: Cell::new(0),
45 data: RefCell::new(vec![])
48 fn alloc(&self) -> Ptr {
49 self.cur_ops.set(self.cur_ops.get() + 1);
51 if self.cur_ops.get() == self.failing_op {
52 panic!(InjectedFailure);
55 let mut data = self.data.borrow_mut();
56 let addr = data.len();
62 struct Ptr<'a>(usize, &'a Allocator);
63 impl<'a> Drop for Ptr<'a> {
65 match self.1.data.borrow_mut()[self.0] {
67 panic!("double free at index {:?}", self.0)
69 ref mut d => *d = false
72 self.1.cur_ops.set(self.1.cur_ops.get()+1);
74 if self.1.cur_ops.get() == self.1.failing_op {
75 panic!(InjectedFailure);
80 fn dynamic_init(a: &Allocator, c: bool) {
87 fn dynamic_drop(a: &Allocator, c: bool) {
96 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
97 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
101 if (c0 && i == 0) || (c1 && i == 1) {
102 x = (a.alloc(), a.alloc(), a.alloc());
103 y = TwoPtrs(a.alloc(), a.alloc());
112 fn field_assignment(a: &Allocator, c0: bool) {
113 let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
124 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
125 let mut _v = a.alloc();
126 let mut _w = a.alloc();
136 fn assignment1(a: &Allocator, c0: bool) {
137 let mut _v = a.alloc();
138 let mut _w = a.alloc();
145 #[allow(unions_with_drop_fields)]
151 fn union1(a: &Allocator) {
153 let mut u = Boxy { a: a.alloc() };
159 fn array_simple(a: &Allocator) {
160 let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
163 fn vec_simple(a: &Allocator) {
164 let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
167 fn generator(a: &Allocator, run_count: usize) {
168 assert!(run_count < 4);
177 for _ in 0..run_count {
182 fn mixed_drop_and_nondrop(a: &Allocator) {
183 // check that destructor panics handle drop
184 // and non-drop blocks in the same scope correctly.
186 // Surprisingly enough, this used to not work.
193 #[allow(unreachable_code)]
194 fn vec_unreachable(a: &Allocator) {
195 let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
198 fn slice_pattern_first(a: &Allocator) {
199 let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
202 fn slice_pattern_middle(a: &Allocator) {
203 let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
206 fn slice_pattern_two(a: &Allocator) {
207 let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
210 fn slice_pattern_last(a: &Allocator) {
211 let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
214 fn slice_pattern_one_of(a: &Allocator, i: usize) {
215 let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
217 0 => { let [a, ..] = array; a }
218 1 => { let [_, a, ..] = array; a }
219 2 => { let [_, _, a, _] = array; a }
220 3 => { let [_, _, _, a] = array; a }
221 _ => panic!("unmatched"),
225 fn run_test<F>(mut f: F)
226 where F: FnMut(&Allocator)
228 let first_alloc = Allocator::new(usize::MAX);
231 for failing_op in 1..first_alloc.cur_ops.get()+1 {
232 let alloc = Allocator::new(failing_op);
234 let f = panic::AssertUnwindSafe(&mut f);
235 let result = panic::catch_unwind(move || {
239 Ok(..) => panic!("test executed {} ops but now {}",
240 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
242 if e.downcast_ref::<InjectedFailure>().is_none() {
243 panic::resume_unwind(e);
250 fn run_test_nopanic<F>(mut f: F)
251 where F: FnMut(&Allocator)
253 let first_alloc = Allocator::new(usize::MAX);
258 run_test(|a| dynamic_init(a, false));
259 run_test(|a| dynamic_init(a, true));
260 run_test(|a| dynamic_drop(a, false));
261 run_test(|a| dynamic_drop(a, true));
263 run_test(|a| assignment2(a, false, false));
264 run_test(|a| assignment2(a, false, true));
265 run_test(|a| assignment2(a, true, false));
266 run_test(|a| assignment2(a, true, true));
268 run_test(|a| assignment1(a, false));
269 run_test(|a| assignment1(a, true));
271 run_test(|a| array_simple(a));
272 run_test(|a| vec_simple(a));
273 run_test(|a| vec_unreachable(a));
275 run_test(|a| struct_dynamic_drop(a, false, false, false));
276 run_test(|a| struct_dynamic_drop(a, false, false, true));
277 run_test(|a| struct_dynamic_drop(a, false, true, false));
278 run_test(|a| struct_dynamic_drop(a, false, true, true));
279 run_test(|a| struct_dynamic_drop(a, true, false, false));
280 run_test(|a| struct_dynamic_drop(a, true, false, true));
281 run_test(|a| struct_dynamic_drop(a, true, true, false));
282 run_test(|a| struct_dynamic_drop(a, true, true, true));
284 run_test(|a| field_assignment(a, false));
285 run_test(|a| field_assignment(a, true));
287 run_test(|a| generator(a, 0));
288 run_test(|a| generator(a, 1));
289 run_test(|a| generator(a, 2));
290 run_test(|a| generator(a, 3));
292 run_test(|a| mixed_drop_and_nondrop(a));
294 run_test(|a| slice_pattern_first(a));
295 run_test(|a| slice_pattern_middle(a));
296 run_test(|a| slice_pattern_two(a));
297 run_test(|a| slice_pattern_last(a));
298 run_test(|a| slice_pattern_one_of(a, 0));
299 run_test(|a| slice_pattern_one_of(a, 1));
300 run_test(|a| slice_pattern_one_of(a, 2));
301 run_test(|a| slice_pattern_one_of(a, 3));
303 run_test_nopanic(|a| union1(a));