2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2021-12-03 15:32:51 +00:00
|
|
|
// needs-unwind
|
2017-10-23 03:01:00 +00:00
|
|
|
|
2017-02-26 14:21:26 +00:00
|
|
|
use std::cell::RefCell;
|
2017-02-26 14:21:08 +00:00
|
|
|
use std::panic;
|
2017-02-26 14:21:26 +00:00
|
|
|
|
|
|
|
pub struct DropLogger<'a> {
|
|
|
|
id: usize,
|
2017-02-26 14:21:08 +00:00
|
|
|
log: &'a panic::AssertUnwindSafe<RefCell<Vec<usize>>>
|
2017-02-26 14:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Drop for DropLogger<'a> {
|
|
|
|
fn drop(&mut self) {
|
2017-02-26 14:21:08 +00:00
|
|
|
self.log.0.borrow_mut().push(self.id);
|
2017-02-26 14:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 14:21:08 +00:00
|
|
|
struct InjectedFailure;
|
|
|
|
|
|
|
|
#[allow(unreachable_code)]
|
2017-02-26 14:21:26 +00:00
|
|
|
fn main() {
|
2017-02-26 14:21:08 +00:00
|
|
|
let log = panic::AssertUnwindSafe(RefCell::new(vec![]));
|
2017-02-26 14:21:26 +00:00
|
|
|
let d = |id| DropLogger { id: id, log: &log };
|
|
|
|
let get = || -> Vec<_> {
|
2017-02-26 14:21:08 +00:00
|
|
|
let mut m = log.0.borrow_mut();
|
2017-02-26 14:21:26 +00:00
|
|
|
let n = m.drain(..);
|
|
|
|
n.collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
let _x = (d(0), &d(1), d(2), &d(3));
|
|
|
|
// all borrows are extended - nothing has been dropped yet
|
|
|
|
assert_eq!(get(), vec![]);
|
|
|
|
}
|
2018-01-28 23:59:34 +00:00
|
|
|
// in a let-statement, extended places are dropped
|
2017-02-26 14:21:26 +00:00
|
|
|
// *after* the let result (tho they have the same scope
|
|
|
|
// as far as scope-based borrowck goes).
|
|
|
|
assert_eq!(get(), vec![0, 2, 3, 1]);
|
2017-02-26 14:21:08 +00:00
|
|
|
|
|
|
|
let _ = std::panic::catch_unwind(|| {
|
2021-02-01 23:40:17 +00:00
|
|
|
(d(4), &d(5), d(6), &d(7), panic::panic_any(InjectedFailure));
|
2017-02-26 14:21:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// here, the temporaries (5/7) live until the end of the
|
|
|
|
// containing statement, which is destroyed after the operands
|
|
|
|
// (4/6) on a panic.
|
|
|
|
assert_eq!(get(), vec![6, 4, 7, 5]);
|
2017-02-26 14:21:26 +00:00
|
|
|
}
|