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