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