]>
Commit | Line | Data |
---|---|---|
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 | ||
b7449926 | 11 | // run-pass |
abe05a73 XL |
12 | // ignore-wasm32-bare compiled with panic=abort by default |
13 | ||
8bb4bdeb XL |
14 | use std::cell::RefCell; |
15 | use std::panic; | |
16 | ||
17 | pub struct DropLogger<'a> { | |
18 | id: usize, | |
19 | log: &'a panic::AssertUnwindSafe<RefCell<Vec<usize>>> | |
20 | } | |
21 | ||
22 | impl<'a> Drop for DropLogger<'a> { | |
23 | fn drop(&mut self) { | |
24 | self.log.0.borrow_mut().push(self.id); | |
25 | } | |
26 | } | |
27 | ||
28 | struct InjectedFailure; | |
29 | ||
30 | #[allow(unreachable_code)] | |
31 | fn main() { | |
32 | let log = panic::AssertUnwindSafe(RefCell::new(vec![])); | |
33 | let d = |id| DropLogger { id: id, log: &log }; | |
34 | let get = || -> Vec<_> { | |
35 | let mut m = log.0.borrow_mut(); | |
36 | let n = m.drain(..); | |
37 | n.collect() | |
38 | }; | |
39 | ||
40 | { | |
41 | let _x = (d(0), &d(1), d(2), &d(3)); | |
42 | // all borrows are extended - nothing has been dropped yet | |
43 | assert_eq!(get(), vec![]); | |
44 | } | |
2c00a5a8 | 45 | // in a let-statement, extended places are dropped |
8bb4bdeb XL |
46 | // *after* the let result (tho they have the same scope |
47 | // as far as scope-based borrowck goes). | |
48 | assert_eq!(get(), vec![0, 2, 3, 1]); | |
49 | ||
50 | let _ = std::panic::catch_unwind(|| { | |
51 | (d(4), &d(5), d(6), &d(7), panic!(InjectedFailure)); | |
52 | }); | |
53 | ||
54 | // here, the temporaries (5/7) live until the end of the | |
55 | // containing statement, which is destroyed after the operands | |
56 | // (4/6) on a panic. | |
57 | assert_eq!(get(), vec![6, 4, 7, 5]); | |
58 | } |