]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/dynamic-drop.rs
Auto merge of #45393 - alexcrichton:update-musl, r=Mark-Simulacrum
[rust.git] / 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.
4 //
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.
10
11 #![feature(generators, generator_trait, untagged_unions)]
12
13 use std::cell::{Cell, RefCell};
14 use std::ops::Generator;
15 use std::panic;
16 use std::usize;
17
18 struct InjectedFailure;
19
20 struct Allocator {
21     data: RefCell<Vec<bool>>,
22     failing_op: usize,
23     cur_ops: Cell<usize>,
24 }
25
26 impl panic::UnwindSafe for Allocator {}
27 impl panic::RefUnwindSafe for Allocator {}
28
29 impl Drop for Allocator {
30     fn drop(&mut self) {
31         let data = self.data.borrow();
32         if data.iter().any(|d| *d) {
33             panic!("missing free: {:?}", data);
34         }
35     }
36 }
37
38 impl Allocator {
39     fn new(failing_op: usize) -> Self {
40         Allocator {
41             failing_op: failing_op,
42             cur_ops: Cell::new(0),
43             data: RefCell::new(vec![])
44         }
45     }
46     fn alloc(&self) -> Ptr {
47         self.cur_ops.set(self.cur_ops.get() + 1);
48
49         if self.cur_ops.get() == self.failing_op {
50             panic!(InjectedFailure);
51         }
52
53         let mut data = self.data.borrow_mut();
54         let addr = data.len();
55         data.push(true);
56         Ptr(addr, self)
57     }
58 }
59
60 struct Ptr<'a>(usize, &'a Allocator);
61 impl<'a> Drop for Ptr<'a> {
62     fn drop(&mut self) {
63         match self.1.data.borrow_mut()[self.0] {
64             false => {
65                 panic!("double free at index {:?}", self.0)
66             }
67             ref mut d => *d = false
68         }
69
70         self.1.cur_ops.set(self.1.cur_ops.get()+1);
71
72         if self.1.cur_ops.get() == self.1.failing_op {
73             panic!(InjectedFailure);
74         }
75     }
76 }
77
78 fn dynamic_init(a: &Allocator, c: bool) {
79     let _x;
80     if c {
81         _x = Some(a.alloc());
82     }
83 }
84
85 fn dynamic_drop(a: &Allocator, c: bool) {
86     let x = a.alloc();
87     if c {
88         Some(x)
89     } else {
90         None
91     };
92 }
93
94 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
95 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
96     for i in 0..2 {
97         let x;
98         let y;
99         if (c0 && i == 0) || (c1 && i == 1) {
100             x = (a.alloc(), a.alloc(), a.alloc());
101             y = TwoPtrs(a.alloc(), a.alloc());
102             if c {
103                 drop(x.1);
104                 drop(y.0);
105             }
106         }
107     }
108 }
109
110 fn field_assignment(a: &Allocator, c0: bool) {
111     let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
112
113     x.1 = a.alloc();
114     x.1 = a.alloc();
115
116     let f = (x.0).0;
117     if c0 {
118         (x.0).0 = f;
119     }
120 }
121
122 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
123     let mut _v = a.alloc();
124     let mut _w = a.alloc();
125     if c0 {
126         drop(_v);
127     }
128     _v = _w;
129     if c1 {
130         _w = a.alloc();
131     }
132 }
133
134 fn assignment1(a: &Allocator, c0: bool) {
135     let mut _v = a.alloc();
136     let mut _w = a.alloc();
137     if c0 {
138         drop(_v);
139     }
140     _v = _w;
141 }
142
143 #[allow(unions_with_drop_fields)]
144 union Boxy<T> {
145     a: T,
146     b: T,
147 }
148
149 fn union1(a: &Allocator) {
150     unsafe {
151         let mut u = Boxy { a: a.alloc() };
152         u.b = a.alloc();
153         drop(u.a);
154     }
155 }
156
157 fn array_simple(a: &Allocator) {
158     let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
159 }
160
161 fn vec_simple(a: &Allocator) {
162     let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
163 }
164
165 fn generator(a: &Allocator, run_count: usize) {
166     assert!(run_count < 4);
167
168     let mut gen = || {
169         (a.alloc(),
170          yield a.alloc(),
171          a.alloc(),
172          yield a.alloc()
173          );
174     };
175     for _ in 0..run_count {
176         gen.resume();
177     }
178 }
179
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.
183     //
184     // Surprisingly enough, this used to not work.
185     let (x, y, z);
186     x = a.alloc();
187     y = 5;
188     z = a.alloc();
189 }
190
191 #[allow(unreachable_code)]
192 fn vec_unreachable(a: &Allocator) {
193     let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
194 }
195
196 fn run_test<F>(mut f: F)
197     where F: FnMut(&Allocator)
198 {
199     let first_alloc = Allocator::new(usize::MAX);
200     f(&first_alloc);
201
202     for failing_op in 1..first_alloc.cur_ops.get()+1 {
203         let alloc = Allocator::new(failing_op);
204         let alloc = &alloc;
205         let f = panic::AssertUnwindSafe(&mut f);
206         let result = panic::catch_unwind(move || {
207             f.0(alloc);
208         });
209         match result {
210             Ok(..) => panic!("test executed {} ops but now {}",
211                              first_alloc.cur_ops.get(), alloc.cur_ops.get()),
212             Err(e) => {
213                 if e.downcast_ref::<InjectedFailure>().is_none() {
214                     panic::resume_unwind(e);
215                 }
216             }
217         }
218     }
219 }
220
221 fn run_test_nopanic<F>(mut f: F)
222     where F: FnMut(&Allocator)
223 {
224     let first_alloc = Allocator::new(usize::MAX);
225     f(&first_alloc);
226 }
227
228 fn main() {
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));
233
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));
238
239     run_test(|a| assignment1(a, false));
240     run_test(|a| assignment1(a, true));
241
242     run_test(|a| array_simple(a));
243     run_test(|a| vec_simple(a));
244     run_test(|a| vec_unreachable(a));
245
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));
254
255     run_test(|a| field_assignment(a, false));
256     run_test(|a| field_assignment(a, true));
257
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));
262
263     run_test(|a| mixed_drop_and_nondrop(a));
264
265     run_test_nopanic(|a| union1(a));
266 }