]> git.lizzy.rs Git - rust.git/blob - src/test/ui/builtin-clone-unwind.rs
Rollup merge of #100418 - tbodt:stabilize-backtrace, r=dtolnay
[rust.git] / src / test / ui / builtin-clone-unwind.rs
1 // run-pass
2 // needs-unwind
3
4 #![allow(unused_variables)]
5 #![allow(unused_imports)]
6 // ignore-wasm32-bare compiled with panic=abort by default
7
8 // Test that builtin implementations of `Clone` cleanup everything
9 // in case of unwinding.
10
11 use std::thread;
12 use std::rc::Rc;
13
14 struct S(Rc<()>);
15
16 impl Clone for S {
17     fn clone(&self) -> Self {
18         if Rc::strong_count(&self.0) == 7 {
19             panic!("oops");
20         }
21
22         S(self.0.clone())
23     }
24 }
25
26 fn main() {
27     let counter = Rc::new(());
28
29     // Unwinding with tuples...
30     let ccounter = counter.clone();
31     let result = std::panic::catch_unwind(move || {
32         let _ = (
33             S(ccounter.clone()),
34             S(ccounter.clone()),
35             S(ccounter.clone()),
36             S(ccounter)
37         ).clone();
38     });
39
40     assert!(result.is_err());
41     assert_eq!(
42         1,
43         Rc::strong_count(&counter)
44     );
45
46     // ... and with arrays.
47     let ccounter = counter.clone();
48     let child = std::panic::catch_unwind(move || {
49         let _ = [
50             S(ccounter.clone()),
51             S(ccounter.clone()),
52             S(ccounter.clone()),
53             S(ccounter)
54         ].clone();
55     });
56
57     assert!(child.is_err());
58     assert_eq!(
59         1,
60         Rc::strong_count(&counter)
61     );
62 }