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