2017-08-08 17:13:12 +02:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-10-22 20:01:00 -07:00
|
|
|
// ignore-wasm32-bare compiled with panic=abort by default
|
|
|
|
|
2017-08-08 17:13:12 +02:00
|
|
|
// Test that builtin implementations of `Clone` cleanup everything
|
|
|
|
// in case of unwinding.
|
|
|
|
|
|
|
|
use std::thread;
|
2017-08-09 13:55:27 +02:00
|
|
|
use std::rc::Rc;
|
2017-08-08 17:13:12 +02:00
|
|
|
|
2017-08-09 13:55:27 +02:00
|
|
|
struct S(Rc<()>);
|
2017-08-08 17:13:12 +02:00
|
|
|
|
|
|
|
impl Clone for S {
|
|
|
|
fn clone(&self) -> Self {
|
2017-08-09 13:55:27 +02:00
|
|
|
if Rc::strong_count(&self.0) == 7 {
|
2017-08-08 17:13:12 +02:00
|
|
|
panic!("oops");
|
|
|
|
}
|
|
|
|
|
|
|
|
S(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2017-08-09 13:55:27 +02:00
|
|
|
let counter = Rc::new(());
|
2017-08-08 17:13:12 +02:00
|
|
|
|
|
|
|
// Unwinding with tuples...
|
|
|
|
let ccounter = counter.clone();
|
2017-08-09 13:55:27 +02:00
|
|
|
let result = std::panic::catch_unwind(move || {
|
2017-08-08 17:13:12 +02:00
|
|
|
let _ = (
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter)
|
|
|
|
).clone();
|
|
|
|
});
|
|
|
|
|
2017-08-09 13:55:27 +02:00
|
|
|
assert!(result.is_err());
|
2017-08-08 17:13:12 +02:00
|
|
|
assert_eq!(
|
|
|
|
1,
|
2017-08-09 13:55:27 +02:00
|
|
|
Rc::strong_count(&counter)
|
2017-08-08 17:13:12 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// ... and with arrays.
|
|
|
|
let ccounter = counter.clone();
|
2017-08-09 13:55:27 +02:00
|
|
|
let child = std::panic::catch_unwind(move || {
|
2017-08-08 17:13:12 +02:00
|
|
|
let _ = [
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter.clone()),
|
|
|
|
S(ccounter)
|
|
|
|
].clone();
|
|
|
|
});
|
|
|
|
|
2017-08-09 13:55:27 +02:00
|
|
|
assert!(result.is_err());
|
2017-08-08 17:13:12 +02:00
|
|
|
assert_eq!(
|
|
|
|
1,
|
2017-08-09 13:55:27 +02:00
|
|
|
Rc::strong_count(&counter)
|
2017-08-08 17:13:12 +02:00
|
|
|
);
|
|
|
|
}
|