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 // For linking libstdc++ on MinGW
6 #![cfg_attr(all(windows, target_env = "gnu"), feature(static_nobundle))]
7 #![feature(unwind_attributes)]
9 use std::panic::{catch_unwind, AssertUnwindSafe};
11 struct DropCheck<'a>(&'a mut bool);
12 impl<'a> Drop for DropCheck<'a> {
14 println!("DropCheck::drop");
20 fn throw_cxx_exception();
23 fn cxx_catch_callback(cb: extern "C" fn(), ok: *mut bool);
28 extern "C" fn rust_catch_callback(cb: extern "C" fn(), rust_ok: &mut bool) {
29 let _caught_unwind = catch_unwind(AssertUnwindSafe(|| {
30 let _drop = DropCheck(rust_ok);
32 unreachable!("should have unwound instead of returned");
34 unreachable!("catch_unwind should not have caught foreign exception");
37 fn throw_rust_panic() {
39 extern "C" fn callback() {
40 println!("throwing rust panic");
44 let mut dropped = false;
45 let mut cxx_ok = false;
46 let caught_unwind = catch_unwind(AssertUnwindSafe(|| {
47 let _drop = DropCheck(&mut dropped);
49 cxx_catch_callback(callback, &mut cxx_ok);
51 unreachable!("should have unwound instead of returned");
53 println!("caught rust panic");
55 assert!(caught_unwind.is_err());
56 let panic_obj = caught_unwind.unwrap_err();
57 let panic_int = *panic_obj.downcast_ref::<i32>().unwrap();
58 assert_eq!(panic_int, 1234);
63 unsafe { throw_cxx_exception() };