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