]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/mir_drop_order.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / test / run-pass / mir_drop_order.rs
CommitLineData
8bb4bdeb
XL
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
abe05a73
XL
11// ignore-wasm32-bare compiled with panic=abort by default
12
8bb4bdeb
XL
13use std::cell::RefCell;
14use std::panic;
15
16pub struct DropLogger<'a> {
17 id: usize,
18 log: &'a panic::AssertUnwindSafe<RefCell<Vec<usize>>>
19}
20
21impl<'a> Drop for DropLogger<'a> {
22 fn drop(&mut self) {
23 self.log.0.borrow_mut().push(self.id);
24 }
25}
26
27struct InjectedFailure;
28
29#[allow(unreachable_code)]
30fn 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 }
2c00a5a8 44 // in a let-statement, extended places are dropped
8bb4bdeb
XL
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}