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