]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/catch_panic.rs
Disable panic tests on Windows
[rust.git] / tests / run-pass / catch_panic.rs
1 // ignore-windows: Unwind panicking does not currently work on Windows
2 use std::panic::catch_unwind;
3 use std::cell::Cell;
4
5 thread_local! {
6     static MY_COUNTER: Cell<usize> = Cell::new(0);
7     static DROPPED: Cell<bool> = Cell::new(false);
8     static HOOK_CALLED: Cell<bool> = Cell::new(false);
9 }
10
11 struct DropTester;
12
13 impl Drop for DropTester {
14     fn drop(&mut self) {
15         DROPPED.with(|c| {
16             c.set(true);
17         });
18     }
19 }
20
21 fn do_panic_counter() {
22     // If this gets leaked, it will be easy to spot
23     // in Miri's leak report
24     let _string = "LEAKED FROM do_panic_counter".to_string();
25
26     // When we panic, this should get dropped during unwinding
27     let _drop_tester = DropTester;
28
29     // Check for bugs in Miri's panic implementation.
30     // If do_panic_counter() somehow gets called more than once,
31     // we'll generate a different panic message
32     let old_val = MY_COUNTER.with(|c| {
33         let val = c.get();
34         c.set(val + 1);
35         val
36     });
37     panic!(format!("Hello from panic: {:?}", old_val));
38 }
39
40 fn main() {
41     std::panic::set_hook(Box::new(|_panic_info| {
42         HOOK_CALLED.with(|h| h.set(true));
43     }));
44     let res = catch_unwind(|| {
45         let _string = "LEAKED FROM CLOSURE".to_string();
46         do_panic_counter()
47     });
48     let expected: Box<String> = Box::new("Hello from panic: 0".to_string());
49     let actual = res.expect_err("do_panic() did not panic!")
50         .downcast::<String>().expect("Failed to cast to string!");
51         
52     assert_eq!(expected, actual);
53     DROPPED.with(|c| {
54         // This should have been set to 'true' by DropTester
55         assert!(c.get());
56     });
57
58     HOOK_CALLED.with(|h| {
59         assert!(h.get());
60     });
61 }
62