]> git.proxmox.com Git - rustc.git/blob - tests/ui/mir/mir_drop_order.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / mir / mir_drop_order.rs
1 // run-pass
2 // needs-unwind
3
4 use std::cell::RefCell;
5 use std::panic;
6
7 pub struct DropLogger<'a> {
8 id: usize,
9 log: &'a panic::AssertUnwindSafe<RefCell<Vec<usize>>>
10 }
11
12 impl<'a> Drop for DropLogger<'a> {
13 fn drop(&mut self) {
14 self.log.0.borrow_mut().push(self.id);
15 }
16 }
17
18 struct InjectedFailure;
19
20 #[allow(unreachable_code)]
21 fn main() {
22 let log = panic::AssertUnwindSafe(RefCell::new(vec![]));
23 let d = |id| DropLogger { id: id, log: &log };
24 let get = || -> Vec<_> {
25 let mut m = log.0.borrow_mut();
26 let n = m.drain(..);
27 n.collect()
28 };
29
30 {
31 let _x = (d(0), &d(1), d(2), &d(3));
32 // all borrows are extended - nothing has been dropped yet
33 assert_eq!(get(), vec![]);
34 }
35 // in a let-statement, extended places are dropped
36 // *after* the let result (tho they have the same scope
37 // as far as scope-based borrowck goes).
38 assert_eq!(get(), vec![0, 2, 3, 1]);
39
40 let _ = std::panic::catch_unwind(|| {
41 (d(4), &d(5), d(6), &d(7), panic::panic_any(InjectedFailure));
42 });
43
44 // here, the temporaries (5/7) live until the end of the
45 // containing statement, which is destroyed after the operands
46 // (4/6) on a panic.
47 assert_eq!(get(), vec![6, 4, 7, 5]);
48 }