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.
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.
11 // ignore-wasm32-bare compiled with panic=abort by default
13 use std::cell::RefCell;
16 pub struct DropLogger<'a> {
18 log: &'a panic::AssertUnwindSafe<RefCell<Vec<usize>>>
21 impl<'a> Drop for DropLogger<'a> {
23 self.log.0.borrow_mut().push(self.id);
27 struct InjectedFailure;
29 #[allow(unreachable_code)]
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();
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![]);
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]);
49 let _ = std::panic::catch_unwind(|| {
50 (d(4), &d(5), d(6), &d(7), panic!(InjectedFailure));
53 // here, the temporaries (5/7) live until the end of the
54 // containing statement, which is destroyed after the operands
56 assert_eq!(get(), vec![6, 4, 7, 5]);