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 #![feature(generators, generator_trait, untagged_unions)]
13 use std::cell::{Cell, RefCell};
14 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 {
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 run_test<F>(mut f: F)
197 where F: FnMut(&Allocator)
199 let first_alloc = Allocator::new(usize::MAX);
202 for failing_op in 1..first_alloc.cur_ops.get()+1 {
203 let alloc = Allocator::new(failing_op);
205 let f = panic::AssertUnwindSafe(&mut f);
206 let result = panic::catch_unwind(move || {
210 Ok(..) => panic!("test executed {} ops but now {}",
211 first_alloc.cur_ops.get(), alloc.cur_ops.get()),
213 if e.downcast_ref::<InjectedFailure>().is_none() {
214 panic::resume_unwind(e);
221 fn run_test_nopanic<F>(mut f: F)
222 where F: FnMut(&Allocator)
224 let first_alloc = Allocator::new(usize::MAX);
229 run_test(|a| dynamic_init(a, false));
230 run_test(|a| dynamic_init(a, true));
231 run_test(|a| dynamic_drop(a, false));
232 run_test(|a| dynamic_drop(a, true));
234 run_test(|a| assignment2(a, false, false));
235 run_test(|a| assignment2(a, false, true));
236 run_test(|a| assignment2(a, true, false));
237 run_test(|a| assignment2(a, true, true));
239 run_test(|a| assignment1(a, false));
240 run_test(|a| assignment1(a, true));
242 run_test(|a| array_simple(a));
243 run_test(|a| vec_simple(a));
244 run_test(|a| vec_unreachable(a));
246 run_test(|a| struct_dynamic_drop(a, false, false, false));
247 run_test(|a| struct_dynamic_drop(a, false, false, true));
248 run_test(|a| struct_dynamic_drop(a, false, true, false));
249 run_test(|a| struct_dynamic_drop(a, false, true, true));
250 run_test(|a| struct_dynamic_drop(a, true, false, false));
251 run_test(|a| struct_dynamic_drop(a, true, false, true));
252 run_test(|a| struct_dynamic_drop(a, true, true, false));
253 run_test(|a| struct_dynamic_drop(a, true, true, true));
255 run_test(|a| field_assignment(a, false));
256 run_test(|a| field_assignment(a, true));
258 run_test(|a| generator(a, 0));
259 run_test(|a| generator(a, 1));
260 run_test(|a| generator(a, 2));
261 run_test(|a| generator(a, 3));
263 run_test(|a| mixed_drop_and_nondrop(a));
265 run_test_nopanic(|a| union1(a));