]>
git.proxmox.com Git - rustc.git/blob - src/test/run-pass/dynamic-drop.rs
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(untagged_unions)]
13 use std
::cell
::{Cell, RefCell}
;
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 assignment2(a
: &Allocator
, c0
: bool
, c1
: bool
) {
110 let mut _v
= a
.alloc();
111 let mut _w
= a
.alloc();
121 fn assignment1(a
: &Allocator
, c0
: bool
) {
122 let mut _v
= a
.alloc();
123 let mut _w
= a
.alloc();
130 #[allow(unions_with_drop_fields)]
136 fn union1(a
: &Allocator
) {
138 let mut u
= Boxy { a: a.alloc() }
;
144 fn array_simple(a
: &Allocator
) {
145 let _x
= [a
.alloc(), a
.alloc(), a
.alloc(), a
.alloc()];
148 fn vec_simple(a
: &Allocator
) {
149 let _x
= vec
![a
.alloc(), a
.alloc(), a
.alloc(), a
.alloc()];
152 fn run_test
<F
>(mut f
: F
)
153 where F
: FnMut(&Allocator
)
155 let first_alloc
= Allocator
::new(usize::MAX
);
158 for failing_op
in 1..first_alloc
.cur_ops
.get()+1 {
159 let alloc
= Allocator
::new(failing_op
);
161 let f
= panic
::AssertUnwindSafe(&mut f
);
162 let result
= panic
::catch_unwind(move || {
166 Ok(..) => panic
!("test executed {} ops but now {}",
167 first_alloc
.cur_ops
.get(), alloc
.cur_ops
.get()),
169 if e
.downcast_ref
::<InjectedFailure
>().is_none() {
170 panic
::resume_unwind(e
);
177 fn run_test_nopanic
<F
>(mut f
: F
)
178 where F
: FnMut(&Allocator
)
180 let first_alloc
= Allocator
::new(usize::MAX
);
185 run_test(|a
| dynamic_init(a
, false));
186 run_test(|a
| dynamic_init(a
, true));
187 run_test(|a
| dynamic_drop(a
, false));
188 run_test(|a
| dynamic_drop(a
, true));
190 run_test(|a
| assignment2(a
, false, false));
191 run_test(|a
| assignment2(a
, false, true));
192 run_test(|a
| assignment2(a
, true, false));
193 run_test(|a
| assignment2(a
, true, true));
195 run_test(|a
| assignment1(a
, false));
196 run_test(|a
| assignment1(a
, true));
198 run_test(|a
| array_simple(a
));
199 run_test(|a
| vec_simple(a
));
201 run_test(|a
| struct_dynamic_drop(a
, false, false, false));
202 run_test(|a
| struct_dynamic_drop(a
, false, false, true));
203 run_test(|a
| struct_dynamic_drop(a
, false, true, false));
204 run_test(|a
| struct_dynamic_drop(a
, false, true, true));
205 run_test(|a
| struct_dynamic_drop(a
, true, false, false));
206 run_test(|a
| struct_dynamic_drop(a
, true, false, true));
207 run_test(|a
| struct_dynamic_drop(a
, true, true, false));
208 run_test(|a
| struct_dynamic_drop(a
, true, true, true));
210 run_test_nopanic(|a
| union1(a
));