]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | // run-pass |
abe05a73 XL |
2 | // ignore-wasm32-bare compiled with panic=abort by default |
3 | ||
0531ce1d | 4 | #![feature(generators, generator_trait, untagged_unions)] |
74b04a01 XL |
5 | #![feature(move_ref_pattern)] |
6 | #![feature(bindings_after_at)] | |
dfeec247 XL |
7 | |
8 | #![allow(unused_assignments)] | |
9 | #![allow(unused_variables)] | |
3157f602 XL |
10 | |
11 | use std::cell::{Cell, RefCell}; | |
e74abb32 | 12 | use std::mem::ManuallyDrop; |
abe05a73 | 13 | use std::ops::Generator; |
3157f602 | 14 | use std::panic; |
9fa01778 | 15 | use std::pin::Pin; |
3157f602 XL |
16 | use std::usize; |
17 | ||
18 | struct InjectedFailure; | |
19 | ||
20 | struct Allocator { | |
21 | data: RefCell<Vec<bool>>, | |
22 | failing_op: usize, | |
23 | cur_ops: Cell<usize>, | |
24 | } | |
25 | ||
26 | impl panic::UnwindSafe for Allocator {} | |
27 | impl panic::RefUnwindSafe for Allocator {} | |
28 | ||
29 | impl Drop for Allocator { | |
30 | fn drop(&mut self) { | |
31 | let data = self.data.borrow(); | |
32 | if data.iter().any(|d| *d) { | |
33 | panic!("missing free: {:?}", data); | |
34 | } | |
35 | } | |
36 | } | |
37 | ||
38 | impl Allocator { | |
39 | fn new(failing_op: usize) -> Self { | |
40 | Allocator { | |
41 | failing_op: failing_op, | |
42 | cur_ops: Cell::new(0), | |
43 | data: RefCell::new(vec![]) | |
44 | } | |
45 | } | |
dc9dc135 | 46 | fn alloc(&self) -> Ptr<'_> { |
3157f602 XL |
47 | self.cur_ops.set(self.cur_ops.get() + 1); |
48 | ||
49 | if self.cur_ops.get() == self.failing_op { | |
50 | panic!(InjectedFailure); | |
51 | } | |
52 | ||
53 | let mut data = self.data.borrow_mut(); | |
54 | let addr = data.len(); | |
55 | data.push(true); | |
56 | Ptr(addr, self) | |
57 | } | |
dc9dc135 XL |
58 | // FIXME(#47949) Any use of this indicates a bug in rustc: we should never |
59 | // be leaking values in the cases here. | |
60 | // | |
61 | // Creates a `Ptr<'_>` and checks that the allocated value is leaked if the | |
62 | // `failing_op` is in the list of exception. | |
63 | fn alloc_leaked(&self, exceptions: Vec<usize>) -> Ptr<'_> { | |
64 | let ptr = self.alloc(); | |
65 | ||
66 | if exceptions.iter().any(|operation| *operation == self.failing_op) { | |
67 | let mut data = self.data.borrow_mut(); | |
68 | data[ptr.0] = false; | |
69 | } | |
70 | ptr | |
71 | } | |
3157f602 XL |
72 | } |
73 | ||
74 | struct Ptr<'a>(usize, &'a Allocator); | |
75 | impl<'a> Drop for Ptr<'a> { | |
76 | fn drop(&mut self) { | |
77 | match self.1.data.borrow_mut()[self.0] { | |
78 | false => { | |
79 | panic!("double free at index {:?}", self.0) | |
80 | } | |
81 | ref mut d => *d = false | |
82 | } | |
83 | ||
84 | self.1.cur_ops.set(self.1.cur_ops.get()+1); | |
85 | ||
86 | if self.1.cur_ops.get() == self.1.failing_op { | |
87 | panic!(InjectedFailure); | |
88 | } | |
89 | } | |
90 | } | |
91 | ||
3157f602 XL |
92 | fn dynamic_init(a: &Allocator, c: bool) { |
93 | let _x; | |
94 | if c { | |
95 | _x = Some(a.alloc()); | |
96 | } | |
97 | } | |
98 | ||
3157f602 XL |
99 | fn dynamic_drop(a: &Allocator, c: bool) { |
100 | let x = a.alloc(); | |
101 | if c { | |
102 | Some(x) | |
103 | } else { | |
104 | None | |
105 | }; | |
106 | } | |
107 | ||
7cac9316 XL |
108 | struct TwoPtrs<'a>(Ptr<'a>, Ptr<'a>); |
109 | fn struct_dynamic_drop(a: &Allocator, c0: bool, c1: bool, c: bool) { | |
110 | for i in 0..2 { | |
111 | let x; | |
112 | let y; | |
113 | if (c0 && i == 0) || (c1 && i == 1) { | |
114 | x = (a.alloc(), a.alloc(), a.alloc()); | |
115 | y = TwoPtrs(a.alloc(), a.alloc()); | |
116 | if c { | |
117 | drop(x.1); | |
118 | drop(y.0); | |
119 | } | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
041b39d2 XL |
124 | fn field_assignment(a: &Allocator, c0: bool) { |
125 | let mut x = (TwoPtrs(a.alloc(), a.alloc()), a.alloc()); | |
126 | ||
127 | x.1 = a.alloc(); | |
128 | x.1 = a.alloc(); | |
129 | ||
130 | let f = (x.0).0; | |
131 | if c0 { | |
132 | (x.0).0 = f; | |
133 | } | |
134 | } | |
135 | ||
3157f602 XL |
136 | fn assignment2(a: &Allocator, c0: bool, c1: bool) { |
137 | let mut _v = a.alloc(); | |
138 | let mut _w = a.alloc(); | |
139 | if c0 { | |
140 | drop(_v); | |
141 | } | |
142 | _v = _w; | |
143 | if c1 { | |
144 | _w = a.alloc(); | |
145 | } | |
146 | } | |
147 | ||
3157f602 XL |
148 | fn assignment1(a: &Allocator, c0: bool) { |
149 | let mut _v = a.alloc(); | |
150 | let mut _w = a.alloc(); | |
151 | if c0 { | |
152 | drop(_v); | |
153 | } | |
154 | _v = _w; | |
155 | } | |
156 | ||
9e0c209e | 157 | union Boxy<T> { |
e74abb32 XL |
158 | a: ManuallyDrop<T>, |
159 | b: ManuallyDrop<T>, | |
9e0c209e SL |
160 | } |
161 | ||
162 | fn union1(a: &Allocator) { | |
163 | unsafe { | |
e74abb32 XL |
164 | let mut u = Boxy { a: ManuallyDrop::new(a.alloc()) }; |
165 | *u.b = a.alloc(); // drops first alloc | |
166 | drop(ManuallyDrop::into_inner(u.a)); | |
9e0c209e SL |
167 | } |
168 | } | |
169 | ||
7cac9316 XL |
170 | fn array_simple(a: &Allocator) { |
171 | let _x = [a.alloc(), a.alloc(), a.alloc(), a.alloc()]; | |
172 | } | |
173 | ||
174 | fn vec_simple(a: &Allocator) { | |
175 | let _x = vec![a.alloc(), a.alloc(), a.alloc(), a.alloc()]; | |
176 | } | |
177 | ||
abe05a73 XL |
178 | fn generator(a: &Allocator, run_count: usize) { |
179 | assert!(run_count < 4); | |
180 | ||
181 | let mut gen = || { | |
182 | (a.alloc(), | |
183 | yield a.alloc(), | |
184 | a.alloc(), | |
185 | yield a.alloc() | |
186 | ); | |
187 | }; | |
188 | for _ in 0..run_count { | |
74b04a01 | 189 | Pin::new(&mut gen).resume(()); |
abe05a73 XL |
190 | } |
191 | } | |
192 | ||
193 | fn mixed_drop_and_nondrop(a: &Allocator) { | |
194 | // check that destructor panics handle drop | |
195 | // and non-drop blocks in the same scope correctly. | |
196 | // | |
197 | // Surprisingly enough, this used to not work. | |
198 | let (x, y, z); | |
199 | x = a.alloc(); | |
200 | y = 5; | |
201 | z = a.alloc(); | |
202 | } | |
203 | ||
3b2f2976 XL |
204 | #[allow(unreachable_code)] |
205 | fn vec_unreachable(a: &Allocator) { | |
206 | let _x = vec![a.alloc(), a.alloc(), a.alloc(), return]; | |
207 | } | |
208 | ||
ff7c6d11 XL |
209 | fn slice_pattern_first(a: &Allocator) { |
210 | let[_x, ..] = [a.alloc(), a.alloc(), a.alloc()]; | |
211 | } | |
212 | ||
213 | fn slice_pattern_middle(a: &Allocator) { | |
214 | let[_, _x, _] = [a.alloc(), a.alloc(), a.alloc()]; | |
215 | } | |
216 | ||
217 | fn slice_pattern_two(a: &Allocator) { | |
218 | let[_x, _, _y, _] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()]; | |
219 | } | |
220 | ||
221 | fn slice_pattern_last(a: &Allocator) { | |
222 | let[.., _y] = [a.alloc(), a.alloc(), a.alloc(), a.alloc()]; | |
223 | } | |
224 | ||
225 | fn slice_pattern_one_of(a: &Allocator, i: usize) { | |
226 | let array = [a.alloc(), a.alloc(), a.alloc(), a.alloc()]; | |
227 | let _x = match i { | |
228 | 0 => { let [a, ..] = array; a } | |
229 | 1 => { let [_, a, ..] = array; a } | |
230 | 2 => { let [_, _, a, _] = array; a } | |
231 | 3 => { let [_, _, _, a] = array; a } | |
232 | _ => panic!("unmatched"), | |
233 | }; | |
234 | } | |
235 | ||
0531ce1d XL |
236 | fn subslice_pattern_from_end(a: &Allocator, arg: bool) { |
237 | let a = [a.alloc(), a.alloc(), a.alloc()]; | |
238 | if arg { | |
239 | let[.., _x, _] = a; | |
240 | } else { | |
416331ca | 241 | let[_, _y @ ..] = a; |
0531ce1d XL |
242 | } |
243 | } | |
244 | ||
245 | fn subslice_pattern_from_end_with_drop(a: &Allocator, arg: bool, arg2: bool) { | |
246 | let a = [a.alloc(), a.alloc(), a.alloc(), a.alloc(), a.alloc()]; | |
247 | if arg2 { | |
248 | drop(a); | |
249 | return; | |
250 | } | |
251 | ||
252 | if arg { | |
253 | let[.., _x, _] = a; | |
254 | } else { | |
416331ca | 255 | let[_, _y @ ..] = a; |
0531ce1d XL |
256 | } |
257 | } | |
258 | ||
259 | fn slice_pattern_reassign(a: &Allocator) { | |
260 | let mut ar = [a.alloc(), a.alloc()]; | |
261 | let[_, _x] = ar; | |
262 | ar = [a.alloc(), a.alloc()]; | |
263 | let[.., _y] = ar; | |
264 | } | |
265 | ||
266 | fn subslice_pattern_reassign(a: &Allocator) { | |
267 | let mut ar = [a.alloc(), a.alloc(), a.alloc()]; | |
268 | let[_, _, _x] = ar; | |
269 | ar = [a.alloc(), a.alloc(), a.alloc()]; | |
416331ca | 270 | let[_, _y @ ..] = ar; |
0531ce1d XL |
271 | } |
272 | ||
60c5eb7d XL |
273 | fn index_field_mixed_ends(a: &Allocator) { |
274 | let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())]; | |
275 | let[(_x, _), ..] = ar; | |
276 | let[(_, _y), _] = ar; | |
277 | let[_, (_, _w)] = ar; | |
278 | let[.., (_z, _)] = ar; | |
279 | } | |
280 | ||
281 | fn subslice_mixed_min_lengths(a: &Allocator, c: i32) { | |
282 | let ar = [(a.alloc(), a.alloc()), (a.alloc(), a.alloc())]; | |
283 | match c { | |
284 | 0 => { let[_x, ..] = ar; } | |
285 | 1 => { let[_x, _, ..] = ar; } | |
286 | 2 => { let[_x, _] = ar; } | |
287 | 3 => { let[(_x, _), _, ..] = ar; } | |
288 | 4 => { let[.., (_x, _)] = ar; } | |
289 | 5 => { let[.., (_x, _), _] = ar; } | |
290 | 6 => { let [_y @ ..] = ar; } | |
291 | _ => { let [_y @ .., _] = ar; } | |
292 | } | |
293 | } | |
294 | ||
74b04a01 XL |
295 | fn bindings_after_at_dynamic_init_move(a: &Allocator, c: bool) { |
296 | let foo = if c { Some(a.alloc()) } else { None }; | |
297 | let _x; | |
298 | ||
299 | if let bar @ Some(_) = foo { | |
300 | _x = bar; | |
301 | } | |
302 | } | |
303 | ||
304 | fn bindings_after_at_dynamic_init_ref(a: &Allocator, c: bool) { | |
305 | let foo = if c { Some(a.alloc()) } else { None }; | |
306 | let _x; | |
307 | ||
308 | if let bar @ Some(_baz) = &foo { | |
309 | _x = bar; | |
310 | } | |
311 | } | |
312 | ||
313 | fn bindings_after_at_dynamic_drop_move(a: &Allocator, c: bool) { | |
314 | let foo = if c { Some(a.alloc()) } else { None }; | |
315 | ||
316 | if let bar @ Some(_) = foo { | |
317 | bar | |
318 | } else { | |
319 | None | |
320 | }; | |
321 | } | |
322 | ||
323 | fn bindings_after_at_dynamic_drop_ref(a: &Allocator, c: bool) { | |
324 | let foo = if c { Some(a.alloc()) } else { None }; | |
325 | ||
326 | if let bar @ Some(_baz) = &foo { | |
327 | bar | |
328 | } else { | |
329 | &None | |
330 | }; | |
331 | } | |
332 | ||
333 | fn move_ref_pattern(a: &Allocator) { | |
334 | let mut tup = (a.alloc(), a.alloc(), a.alloc(), a.alloc()); | |
335 | let (ref _a, ref mut _b, _c, mut _d) = tup; | |
336 | } | |
337 | ||
dc9dc135 XL |
338 | fn panic_after_return(a: &Allocator) -> Ptr<'_> { |
339 | // Panic in the drop of `p` or `q` can leak | |
340 | let exceptions = vec![8, 9]; | |
341 | a.alloc(); | |
342 | let p = a.alloc(); | |
343 | { | |
344 | a.alloc(); | |
345 | let p = a.alloc(); | |
346 | // FIXME (#47949) We leak values when we panic in a destructor after | |
347 | // evaluating an expression with `rustc_mir::build::Builder::into`. | |
348 | a.alloc_leaked(exceptions) | |
349 | } | |
350 | } | |
351 | ||
352 | fn panic_after_return_expr(a: &Allocator) -> Ptr<'_> { | |
353 | // Panic in the drop of `p` or `q` can leak | |
354 | let exceptions = vec![8, 9]; | |
355 | a.alloc(); | |
356 | let p = a.alloc(); | |
357 | { | |
358 | a.alloc(); | |
359 | let q = a.alloc(); | |
360 | // FIXME (#47949) | |
361 | return a.alloc_leaked(exceptions); | |
362 | } | |
363 | } | |
364 | ||
365 | fn panic_after_init(a: &Allocator) { | |
366 | // Panic in the drop of `r` can leak | |
367 | let exceptions = vec![8]; | |
368 | a.alloc(); | |
369 | let p = a.alloc(); | |
370 | let q = { | |
371 | a.alloc(); | |
372 | let r = a.alloc(); | |
373 | // FIXME (#47949) | |
374 | a.alloc_leaked(exceptions) | |
375 | }; | |
376 | } | |
377 | ||
378 | fn panic_after_init_temp(a: &Allocator) { | |
379 | // Panic in the drop of `r` can leak | |
380 | let exceptions = vec![8]; | |
381 | a.alloc(); | |
382 | let p = a.alloc(); | |
383 | { | |
384 | a.alloc(); | |
385 | let r = a.alloc(); | |
386 | // FIXME (#47949) | |
387 | a.alloc_leaked(exceptions) | |
388 | }; | |
389 | } | |
390 | ||
391 | fn panic_after_init_by_loop(a: &Allocator) { | |
392 | // Panic in the drop of `r` can leak | |
393 | let exceptions = vec![8]; | |
394 | a.alloc(); | |
395 | let p = a.alloc(); | |
396 | let q = loop { | |
397 | a.alloc(); | |
398 | let r = a.alloc(); | |
399 | // FIXME (#47949) | |
400 | break a.alloc_leaked(exceptions); | |
401 | }; | |
402 | } | |
403 | ||
3157f602 XL |
404 | fn run_test<F>(mut f: F) |
405 | where F: FnMut(&Allocator) | |
406 | { | |
407 | let first_alloc = Allocator::new(usize::MAX); | |
408 | f(&first_alloc); | |
409 | ||
410 | for failing_op in 1..first_alloc.cur_ops.get()+1 { | |
411 | let alloc = Allocator::new(failing_op); | |
412 | let alloc = &alloc; | |
413 | let f = panic::AssertUnwindSafe(&mut f); | |
414 | let result = panic::catch_unwind(move || { | |
415 | f.0(alloc); | |
416 | }); | |
417 | match result { | |
418 | Ok(..) => panic!("test executed {} ops but now {}", | |
419 | first_alloc.cur_ops.get(), alloc.cur_ops.get()), | |
420 | Err(e) => { | |
421 | if e.downcast_ref::<InjectedFailure>().is_none() { | |
422 | panic::resume_unwind(e); | |
423 | } | |
424 | } | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
9e0c209e SL |
429 | fn run_test_nopanic<F>(mut f: F) |
430 | where F: FnMut(&Allocator) | |
431 | { | |
432 | let first_alloc = Allocator::new(usize::MAX); | |
433 | f(&first_alloc); | |
434 | } | |
435 | ||
3157f602 XL |
436 | fn main() { |
437 | run_test(|a| dynamic_init(a, false)); | |
438 | run_test(|a| dynamic_init(a, true)); | |
439 | run_test(|a| dynamic_drop(a, false)); | |
440 | run_test(|a| dynamic_drop(a, true)); | |
441 | ||
442 | run_test(|a| assignment2(a, false, false)); | |
443 | run_test(|a| assignment2(a, false, true)); | |
444 | run_test(|a| assignment2(a, true, false)); | |
445 | run_test(|a| assignment2(a, true, true)); | |
446 | ||
447 | run_test(|a| assignment1(a, false)); | |
448 | run_test(|a| assignment1(a, true)); | |
9e0c209e | 449 | |
7cac9316 XL |
450 | run_test(|a| array_simple(a)); |
451 | run_test(|a| vec_simple(a)); | |
3b2f2976 | 452 | run_test(|a| vec_unreachable(a)); |
7cac9316 XL |
453 | |
454 | run_test(|a| struct_dynamic_drop(a, false, false, false)); | |
455 | run_test(|a| struct_dynamic_drop(a, false, false, true)); | |
456 | run_test(|a| struct_dynamic_drop(a, false, true, false)); | |
457 | run_test(|a| struct_dynamic_drop(a, false, true, true)); | |
458 | run_test(|a| struct_dynamic_drop(a, true, false, false)); | |
459 | run_test(|a| struct_dynamic_drop(a, true, false, true)); | |
460 | run_test(|a| struct_dynamic_drop(a, true, true, false)); | |
461 | run_test(|a| struct_dynamic_drop(a, true, true, true)); | |
462 | ||
041b39d2 XL |
463 | run_test(|a| field_assignment(a, false)); |
464 | run_test(|a| field_assignment(a, true)); | |
465 | ||
abe05a73 XL |
466 | run_test(|a| generator(a, 0)); |
467 | run_test(|a| generator(a, 1)); | |
468 | run_test(|a| generator(a, 2)); | |
469 | run_test(|a| generator(a, 3)); | |
470 | ||
471 | run_test(|a| mixed_drop_and_nondrop(a)); | |
472 | ||
ff7c6d11 XL |
473 | run_test(|a| slice_pattern_first(a)); |
474 | run_test(|a| slice_pattern_middle(a)); | |
475 | run_test(|a| slice_pattern_two(a)); | |
476 | run_test(|a| slice_pattern_last(a)); | |
477 | run_test(|a| slice_pattern_one_of(a, 0)); | |
478 | run_test(|a| slice_pattern_one_of(a, 1)); | |
479 | run_test(|a| slice_pattern_one_of(a, 2)); | |
480 | run_test(|a| slice_pattern_one_of(a, 3)); | |
481 | ||
0531ce1d XL |
482 | run_test(|a| subslice_pattern_from_end(a, true)); |
483 | run_test(|a| subslice_pattern_from_end(a, false)); | |
484 | run_test(|a| subslice_pattern_from_end_with_drop(a, true, true)); | |
485 | run_test(|a| subslice_pattern_from_end_with_drop(a, true, false)); | |
486 | run_test(|a| subslice_pattern_from_end_with_drop(a, false, true)); | |
487 | run_test(|a| subslice_pattern_from_end_with_drop(a, false, false)); | |
488 | run_test(|a| slice_pattern_reassign(a)); | |
489 | run_test(|a| subslice_pattern_reassign(a)); | |
490 | ||
60c5eb7d XL |
491 | run_test(|a| index_field_mixed_ends(a)); |
492 | run_test(|a| subslice_mixed_min_lengths(a, 0)); | |
493 | run_test(|a| subslice_mixed_min_lengths(a, 1)); | |
494 | run_test(|a| subslice_mixed_min_lengths(a, 2)); | |
495 | run_test(|a| subslice_mixed_min_lengths(a, 3)); | |
496 | run_test(|a| subslice_mixed_min_lengths(a, 4)); | |
497 | run_test(|a| subslice_mixed_min_lengths(a, 5)); | |
498 | run_test(|a| subslice_mixed_min_lengths(a, 6)); | |
499 | run_test(|a| subslice_mixed_min_lengths(a, 7)); | |
500 | ||
74b04a01 XL |
501 | run_test(|a| move_ref_pattern(a)); |
502 | ||
dc9dc135 XL |
503 | run_test(|a| { |
504 | panic_after_return(a); | |
505 | }); | |
506 | run_test(|a| { | |
507 | panic_after_return_expr(a); | |
508 | }); | |
509 | run_test(|a| panic_after_init(a)); | |
510 | run_test(|a| panic_after_init_temp(a)); | |
511 | run_test(|a| panic_after_init_by_loop(a)); | |
512 | ||
74b04a01 XL |
513 | run_test(|a| bindings_after_at_dynamic_init_move(a, true)); |
514 | run_test(|a| bindings_after_at_dynamic_init_move(a, false)); | |
515 | run_test(|a| bindings_after_at_dynamic_init_ref(a, true)); | |
516 | run_test(|a| bindings_after_at_dynamic_init_ref(a, false)); | |
517 | run_test(|a| bindings_after_at_dynamic_drop_move(a, true)); | |
518 | run_test(|a| bindings_after_at_dynamic_drop_move(a, false)); | |
519 | run_test(|a| bindings_after_at_dynamic_drop_ref(a, true)); | |
520 | run_test(|a| bindings_after_at_dynamic_drop_ref(a, false)); | |
521 | ||
9e0c209e | 522 | run_test_nopanic(|a| union1(a)); |
3157f602 | 523 | } |