1 // Tests that C++ exceptions can unwind through Rust code run destructors and
2 // are caught by catch_unwind. Also tests that Rust panics can unwind through
7 use std::panic::{catch_unwind, AssertUnwindSafe};
9 struct DropCheck<'a>(&'a mut bool);
10 impl<'a> Drop for DropCheck<'a> {
12 println!("DropCheck::drop");
18 fn test_cxx_exception();
22 fn cxx_catch_callback(cb: extern "C-unwind" fn(), ok: *mut bool);
26 extern "C-unwind" fn rust_catch_callback(cb: extern "C-unwind" fn(), rust_ok: &mut bool) {
27 let _drop = DropCheck(rust_ok);
29 unreachable!("should have unwound instead of returned");
32 fn test_rust_panic() {
33 extern "C-unwind" fn callback() {
34 println!("throwing rust panic");
38 let mut dropped = false;
39 let mut cxx_ok = false;
40 let caught_unwind = catch_unwind(AssertUnwindSafe(|| {
41 let _drop = DropCheck(&mut dropped);
43 cxx_catch_callback(callback, &mut cxx_ok);
45 unreachable!("should have unwound instead of returned");
47 println!("caught rust panic");
49 assert!(caught_unwind.is_err());
50 let panic_obj = caught_unwind.unwrap_err();
51 let panic_int = *panic_obj.downcast_ref::<i32>().unwrap();
52 assert_eq!(panic_int, 1234);
57 unsafe { test_cxx_exception() };