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))]
8 #![feature(unwind_attributes)]
10 use std::panic::{catch_unwind, AssertUnwindSafe};
12 struct DropCheck<'a>(&'a mut bool);
13 impl<'a> Drop for DropCheck<'a> {
15 println!("DropCheck::drop");
21 fn throw_cxx_exception();
24 fn cxx_catch_callback(cb: extern "C" fn(), ok: *mut bool);
29 extern "C" fn rust_catch_callback(cb: extern "C" fn(), rust_ok: &mut bool) {
30 let _caught_unwind = catch_unwind(AssertUnwindSafe(|| {
31 let _drop = DropCheck(rust_ok);
33 unreachable!("should have unwound instead of returned");
35 unreachable!("catch_unwind should not have caught foreign exception");
38 fn throw_rust_panic() {
40 extern "C" fn callback() {
41 println!("throwing rust panic");
45 let mut dropped = false;
46 let mut cxx_ok = false;
47 let caught_unwind = catch_unwind(AssertUnwindSafe(|| {
48 let _drop = DropCheck(&mut dropped);
50 cxx_catch_callback(callback, &mut cxx_ok);
52 unreachable!("should have unwound instead of returned");
54 println!("caught rust panic");
56 assert!(caught_unwind.is_err());
57 let panic_obj = caught_unwind.unwrap_err();
58 let panic_int = *panic_obj.downcast_ref::<i32>().unwrap();
59 assert_eq!(panic_int, 1234);
64 unsafe { throw_cxx_exception() };