2019-07-26 21:54:25 +00:00
|
|
|
// run-pass
|
2021-12-03 15:32:51 +00:00
|
|
|
// needs-unwind
|
2019-07-26 21:54:25 +00:00
|
|
|
|
2018-09-14 10:20:28 +00:00
|
|
|
#![allow(unused_variables)]
|
|
|
|
#![allow(unused_imports)]
|
2017-10-23 03:01:00 +00:00
|
|
|
|
2017-08-08 15:13:12 +00:00
|
|
|
// Test that builtin implementations of `Clone` cleanup everything
|
|
|
|
// in case of unwinding.
|
|
|
|
|
|
|
|
use std::thread;
|
2017-08-09 11:55:27 +00:00
|
|
|
use std::rc::Rc;
|
2017-08-08 15:13:12 +00:00
|
|
|
|
2017-08-09 11:55:27 +00:00
|
|
|
struct S(Rc<()>);
|
2017-08-08 15:13:12 +00:00
|
|
|
|
|
|
|
impl Clone for S {
|
|
|
|
fn clone(&self) -> Self {
|
2017-08-09 11:55:27 +00:00
|
|
|
if Rc::strong_count(&self.0) == 7 {
|
2017-08-08 15:13:12 +00:00
|
|
|
panic!("oops");
|
|
|
|
}
|
|
|
|
|
|
|
|
S(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2017-08-09 11:55:27 +00:00
|
|
|
let counter = Rc::new(());
|
2017-08-08 15:13:12 +00:00
|
|
|
|
|
|
|
// Unwinding with tuples...
|
|
|
|
let ccounter = counter.clone();
|
2017-08-09 11:55:27 +00:00
|
|
|
let result = std::panic::catch_unwind(move || {
|
2017-08-08 15:13:12 +00:00
|
|
|
let _ = (
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter)
|
|
|
|
).clone();
|
|
|
|
});
|
|
|
|
|
2017-08-09 11:55:27 +00:00
|
|
|
assert!(result.is_err());
|
2017-08-08 15:13:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
1,
|
2017-08-09 11:55:27 +00:00
|
|
|
Rc::strong_count(&counter)
|
2017-08-08 15:13:12 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// ... and with arrays.
|
|
|
|
let ccounter = counter.clone();
|
2017-08-09 11:55:27 +00:00
|
|
|
let child = std::panic::catch_unwind(move || {
|
2017-08-08 15:13:12 +00:00
|
|
|
let _ = [
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter)
|
|
|
|
].clone();
|
|
|
|
});
|
|
|
|
|
2021-06-05 21:17:35 +00:00
|
|
|
assert!(child.is_err());
|
2017-08-08 15:13:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
1,
|
2017-08-09 11:55:27 +00:00
|
|
|
Rc::strong_count(&counter)
|
2017-08-08 15:13:12 +00:00
|
|
|
);
|
|
|
|
}
|