]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/mir_drop_order.rs
Rollup merge of #47846 - roblabla:bugfix-ocaml, r=kennytm
[rust.git] / src / test / run-pass / mir_drop_order.rs
1 // Copyright 2017 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 use std::cell::RefCell;
14 use std::panic;
15
16 pub struct DropLogger<'a> {
17     id: usize,
18     log: &'a panic::AssertUnwindSafe<RefCell<Vec<usize>>>
19 }
20
21 impl<'a> Drop for DropLogger<'a> {
22     fn drop(&mut self) {
23         self.log.0.borrow_mut().push(self.id);
24     }
25 }
26
27 struct InjectedFailure;
28
29 #[allow(unreachable_code)]
30 fn main() {
31     let log = panic::AssertUnwindSafe(RefCell::new(vec![]));
32     let d = |id| DropLogger { id: id, log: &log };
33     let get = || -> Vec<_> {
34         let mut m = log.0.borrow_mut();
35         let n = m.drain(..);
36         n.collect()
37     };
38
39     {
40         let _x = (d(0), &d(1), d(2), &d(3));
41         // all borrows are extended - nothing has been dropped yet
42         assert_eq!(get(), vec![]);
43     }
44     // in a let-statement, extended places are dropped
45     // *after* the let result (tho they have the same scope
46     // as far as scope-based borrowck goes).
47     assert_eq!(get(), vec![0, 2, 3, 1]);
48
49     let _ = std::panic::catch_unwind(|| {
50         (d(4), &d(5), d(6), &d(7), panic!(InjectedFailure));
51     });
52
53     // here, the temporaries (5/7) live until the end of the
54     // containing statement, which is destroyed after the operands
55     // (4/6) on a panic.
56     assert_eq!(get(), vec![6, 4, 7, 5]);
57 }