Skip to content

Commit 3f0c097

Browse files
committed
Add a test to check that swallowed Rust panics are dropped properly.
1 parent 040ab35 commit 3f0c097

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/test/run-make-fulldeps/foreign-exceptions/foo.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,21 @@ extern "C" {
5757
throw;
5858
}
5959
}
60+
61+
void swallow_exception(void (*cb)()) {
62+
try {
63+
// Do a rethrow to ensure that the exception is only dropped once.
64+
// This is necessary since we don't support copying exceptions.
65+
try {
66+
cb();
67+
} catch (...) {
68+
println("rethrowing Rust panic");
69+
throw;
70+
};
71+
} catch (rust_panic e) {
72+
assert(false && "shouldn't be able to catch a rust panic");
73+
} catch (...) {
74+
println("swallowing foreign exception in catch (...)");
75+
}
76+
}
6077
}

src/test/run-make-fulldeps/foreign-exceptions/foo.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
// For linking libstdc++ on MinGW
66
#![cfg_attr(all(windows, target_env = "gnu"), feature(static_nobundle))]
7-
87
#![feature(unwind_attributes)]
98

109
use std::panic::{catch_unwind, AssertUnwindSafe};
@@ -20,6 +19,8 @@ impl<'a> Drop for DropCheck<'a> {
2019
extern "C" {
2120
fn throw_cxx_exception();
2221

22+
fn swallow_exception(cb: extern "C" fn());
23+
2324
#[unwind(allowed)]
2425
fn cxx_catch_callback(cb: extern "C" fn(), ok: *mut bool);
2526
}
@@ -60,7 +61,34 @@ fn throw_rust_panic() {
6061
assert!(cxx_ok);
6162
}
6263

64+
fn check_exception_drop() {
65+
static mut DROP_COUNT: usize = 0;
66+
67+
struct CountDrop;
68+
impl Drop for CountDrop {
69+
fn drop(&mut self) {
70+
println!("CountDrop::drop");
71+
unsafe {
72+
DROP_COUNT += 1;
73+
}
74+
}
75+
}
76+
77+
78+
#[unwind(allowed)]
79+
extern "C" fn callback() {
80+
println!("throwing rust panic #2");
81+
panic!(CountDrop);
82+
}
83+
84+
unsafe {
85+
swallow_exception(callback);
86+
assert_eq!(DROP_COUNT, 1);
87+
}
88+
}
89+
6390
fn main() {
6491
unsafe { throw_cxx_exception() };
6592
throw_rust_panic();
93+
check_exception_drop();
6694
}

0 commit comments

Comments
 (0)