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