]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/drop/dynamic-drop.rs
Migrate `src/test/ui/run-pass/*` back to `src/test/run-pass/`.
[rust.git] / src / test / run-pass / drop / 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 // run-pass
12 // revisions:lexical nll
13 #![cfg_attr(nll, feature(nll))]
14
15 // ignore-wasm32-bare compiled with panic=abort by default
16
17 #![feature(generators, generator_trait, untagged_unions)]
18 #![feature(slice_patterns)]
19
20 use std::cell::{Cell, RefCell};
21 use std::ops::Generator;
22 use std::panic;
23 use std::usize;
24
25 struct InjectedFailure;
26
27 struct Allocator {
28     data: RefCell<Vec<bool>>,
29     failing_op: usize,
30     cur_ops: Cell<usize>,
31 }
32
33 impl panic::UnwindSafe for Allocator {}
34 impl panic::RefUnwindSafe for Allocator {}
35
36 impl Drop for Allocator {
37     fn drop(&mut self) {
38         let data = self.data.borrow();
39         if data.iter().any(|d| *d) {
40             panic!("missing free: {:?}", data);
41         }
42     }
43 }
44
45 impl Allocator {
46     fn new(failing_op: usize) -> Self {
47         Allocator {
48             failing_op: failing_op,
49             cur_ops: Cell::new(0),
50             data: RefCell::new(vec![])
51         }
52     }
53     fn alloc(&self) -> Ptr {
54         self.cur_ops.set(self.cur_ops.get() + 1);
55
56         if self.cur_ops.get() == self.failing_op {
57             panic!(InjectedFailure);
58         }
59
60         let mut data = self.data.borrow_mut();
61         let addr = data.len();
62         data.push(true);
63         Ptr(addr, self)
64     }
65 }
66
67 struct Ptr<'a>(usize, &'a Allocator);
68 impl<'a> Drop for Ptr<'a> {
69     fn drop(&mut self) {
70         match self.1.data.borrow_mut()[self.0] {
71             false => {
72                 panic!("double free at index {:?}", self.0)
73             }
74             ref mut d => *d = false
75         }
76
77         self.1.cur_ops.set(self.1.cur_ops.get()+1);
78
79         if self.1.cur_ops.get() == self.1.failing_op {
80             panic!(InjectedFailure);
81         }
82     }
83 }
84
85 fn dynamic_init(a: &Allocator, c: bool) {
86     let _x;
87     if c {
88         _x = Some(a.alloc());
89     }
90 }
91
92 fn dynamic_drop(a: &Allocator, c: bool) {
93     let x = a.alloc();
94     if c {
95         Some(x)
96     } else {
97         None
98     };
99 }
100
101 struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>);
102 fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) {
103     for i in 0..2 {
104         let x;
105         let y;
106         if (c0 && i == 0) || (c1 && i == 1) {
107             x = (a.alloc(), a.alloc(), a.alloc());
108             y = TwoPtrs(a.alloc(), a.alloc());
109             if c {
110                 drop(x.1);
111                 drop(y.0);
112             }
113         }
114     }
115 }
116
117 fn field_assignment(a: &Allocator, c0: bool) {
118     let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc());
119
120     x.1 = a.alloc();
121     x.1 = a.alloc();
122
123     let f = (x.0).0;
124     if c0 {
125         (x.0).0 = f;
126     }
127 }
128
129 fn assignment2(a: &Allocator, c0: bool, c1: bool) {
130     let mut _v = a.alloc();
131     let mut _w = a.alloc();
132     if c0 {
133         drop(_v);
134     }
135     _v = _w;
136     if c1 {
137         _w = a.alloc();
138     }
139 }
140
141 fn assignment1(a: &Allocator, c0: bool) {
142     let mut _v = a.alloc();
143     let mut _w = a.alloc();
144     if c0 {
145         drop(_v);
146     }
147     _v = _w;
148 }
149
150 #[allow(unions_with_drop_fields)]
151 union Boxy<T> {
152     a: T,
153     b: T,
154 }
155
156 fn union1(a: &Allocator) {
157     unsafe {
158         let mut u = Boxy { a: a.alloc() };
159         u.b = a.alloc();
160         drop(u.a);
161     }
162 }
163
164 fn array_simple(a: &Allocator) {
165     let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
166 }
167
168 fn vec_simple(a: &Allocator) {
169     let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()];
170 }
171
172 fn generator(a: &Allocator, run_count: usize) {
173     assert!(run_count < 4);
174
175     let mut gen = || {
176         (a.alloc(),
177          yield a.alloc(),
178          a.alloc(),
179          yield a.alloc()
180          );
181     };
182     for _ in 0..run_count {
183         unsafe { gen.resume(); }
184     }
185 }
186
187 fn mixed_drop_and_nondrop(a: &Allocator) {
188     // check that destructor panics handle drop
189     // and non-drop blocks in the same scope correctly.
190     //
191     // Surprisingly enough, this used to not work.
192     let (x, y, z);
193     x = a.alloc();
194     y = 5;
195     z = a.alloc();
196 }
197
198 #[allow(unreachable_code)]
199 fn vec_unreachable(a: &Allocator) {
200     let _x = vec![a.alloc(), a.alloc(), a.alloc(), return];
201 }
202
203 fn slice_pattern_first(a: &Allocator) {
204     let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()];
205 }
206
207 fn slice_pattern_middle(a: &Allocator) {
208     let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()];
209 }
210
211 fn slice_pattern_two(a: &Allocator) {
212     let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
213 }
214
215 fn slice_pattern_last(a: &Allocator) {
216     let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
217 }
218
219 fn slice_pattern_one_of(a: &Allocator, i: usize) {
220     let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()];
221     let _x = match i {
222         0 => { let [a, ..] = array; a }
223         1 => { let [_, a, ..] = array; a }
224         2 => { let [_, _, a, _] = array; a }
225         3 => { let [_, _, _, a] = array; a }
226         _ => panic!("unmatched"),
227     };
228 }
229
230 fn subslice_pattern_from_end(a: &Allocator, arg: bool) {
231     let a = [a.alloc(), a.alloc(), a.alloc()];
232     if arg {
233         let[.., _x, _] = a;
234     } else {
235         let[_, _y..] = a;
236     }
237 }
238
239 fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) {
240     let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()];
241     if arg2 {
242         drop(a);
243         return;
244     }
245
246     if arg {
247         let[.., _x, _] = a;
248     } else {
249         let[_, _y..] = a;
250     }
251 }
252
253 fn slice_pattern_reassign(a: &Allocator) {
254     let mut ar = [a.alloc(), a.alloc()];
255     let[_, _x] = ar;
256     ar = [a.alloc(), a.alloc()];
257     let[.., _y] = ar;
258 }
259
260 fn subslice_pattern_reassign(a: &Allocator) {
261     let mut ar = [a.alloc(), a.alloc(), a.alloc()];
262     let[_, _, _x] = ar;
263     ar = [a.alloc(), a.alloc(), a.alloc()];
264     let[_, _y..] = ar;
265 }
266
267 fn run_test<F>(mut f: F)
268     where F: FnMut(&Allocator)
269 {
270     let first_alloc = Allocator::new(usize::MAX);
271     f(&first_alloc);
272
273     for failing_op in 1..first_alloc.cur_ops.get()+1 {
274         let alloc = Allocator::new(failing_op);
275         let alloc = &alloc;
276         let f = panic::AssertUnwindSafe(&mut f);
277         let result = panic::catch_unwind(move || {
278             f.0(alloc);
279         });
280         match result {
281             Ok(..) => panic!("test executed {} ops but now {}",
282                              first_alloc.cur_ops.get(), alloc.cur_ops.get()),
283             Err(e) => {
284                 if e.downcast_ref::<InjectedFailure>().is_none() {
285                     panic::resume_unwind(e);
286                 }
287             }
288         }
289     }
290 }
291
292 fn run_test_nopanic<F>(mut f: F)
293     where F: FnMut(&Allocator)
294 {
295     let first_alloc = Allocator::new(usize::MAX);
296     f(&first_alloc);
297 }
298
299 fn main() {
300     run_test(|a| dynamic_init(a, false));
301     run_test(|a| dynamic_init(a, true));
302     run_test(|a| dynamic_drop(a, false));
303     run_test(|a| dynamic_drop(a, true));
304
305     run_test(|a| assignment2(a, false, false));
306     run_test(|a| assignment2(a, false, true));
307     run_test(|a| assignment2(a, true, false));
308     run_test(|a| assignment2(a, true, true));
309
310     run_test(|a| assignment1(a, false));
311     run_test(|a| assignment1(a, true));
312
313     run_test(|a| array_simple(a));
314     run_test(|a| vec_simple(a));
315     run_test(|a| vec_unreachable(a));
316
317     run_test(|a| struct_dynamic_drop(a, false, false, false));
318     run_test(|a| struct_dynamic_drop(a, false, false, true));
319     run_test(|a| struct_dynamic_drop(a, false, true, false));
320     run_test(|a| struct_dynamic_drop(a, false, true, true));
321     run_test(|a| struct_dynamic_drop(a, true, false, false));
322     run_test(|a| struct_dynamic_drop(a, true, false, true));
323     run_test(|a| struct_dynamic_drop(a, true, true, false));
324     run_test(|a| struct_dynamic_drop(a, true, true, true));
325
326     run_test(|a| field_assignment(a, false));
327     run_test(|a| field_assignment(a, true));
328
329     run_test(|a| generator(a, 0));
330     run_test(|a| generator(a, 1));
331     run_test(|a| generator(a, 2));
332     run_test(|a| generator(a, 3));
333
334     run_test(|a| mixed_drop_and_nondrop(a));
335
336     run_test(|a| slice_pattern_first(a));
337     run_test(|a| slice_pattern_middle(a));
338     run_test(|a| slice_pattern_two(a));
339     run_test(|a| slice_pattern_last(a));
340     run_test(|a| slice_pattern_one_of(a, 0));
341     run_test(|a| slice_pattern_one_of(a, 1));
342     run_test(|a| slice_pattern_one_of(a, 2));
343     run_test(|a| slice_pattern_one_of(a, 3));
344
345     run_test(|a| subslice_pattern_from_end(a, true));
346     run_test(|a| subslice_pattern_from_end(a, false));
347     run_test(|a| subslice_pattern_from_end_with_drop(a, true, true));
348     run_test(|a| subslice_pattern_from_end_with_drop(a, true, false));
349     run_test(|a| subslice_pattern_from_end_with_drop(a, false, true));
350     run_test(|a| subslice_pattern_from_end_with_drop(a, false, false));
351     run_test(|a| slice_pattern_reassign(a));
352     run_test(|a| subslice_pattern_reassign(a));
353
354     run_test_nopanic(|a| union1(a));
355 }