1 // Tests that C++ exceptions can unwind through Rust code, run destructors and
2 // are ignored by catch_unwind. Also tests that Rust panics can unwind through
5 #![feature(unwind_attributes)]
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 throw_cxx_exception();
21 fn cxx_catch_callback(cb: extern "C" fn(), ok: *mut bool);
26 extern "C" fn rust_catch_callback(cb: extern "C" fn(), rust_ok: &mut bool) {
27 let _caught_unwind = catch_unwind(AssertUnwindSafe(|| {
28 let _drop = DropCheck(rust_ok);
30 unreachable!("should have unwound instead of returned");
32 unreachable!("catch_unwind should not have caught foreign exception");
35 fn throw_rust_panic() {
37 extern "C" fn callback() {
38 println!("throwing rust panic");
42 let mut dropped = false;
43 let mut cxx_ok = false;
44 let caught_unwind = catch_unwind(AssertUnwindSafe(|| {
45 let _drop = DropCheck(&mut dropped);
47 cxx_catch_callback(callback, &mut cxx_ok);
49 unreachable!("should have unwound instead of returned");
51 println!("caught rust panic");
53 assert!(caught_unwind.is_err());
54 let panic_obj = caught_unwind.unwrap_err();
55 let panic_int = *panic_obj.downcast_ref::<i32>().unwrap();
56 assert_eq!(panic_int, 1234);
61 unsafe { throw_cxx_exception() };