summaryrefslogtreecommitdiffstats
path: root/tests/run-make-fulldeps/foreign-exceptions/foo.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/run-make-fulldeps/foreign-exceptions/foo.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/run-make-fulldeps/foreign-exceptions/foo.rs')
-rw-r--r--tests/run-make-fulldeps/foreign-exceptions/foo.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/run-make-fulldeps/foreign-exceptions/foo.rs b/tests/run-make-fulldeps/foreign-exceptions/foo.rs
new file mode 100644
index 000000000..dd3b7c76f
--- /dev/null
+++ b/tests/run-make-fulldeps/foreign-exceptions/foo.rs
@@ -0,0 +1,59 @@
+// Tests that C++ exceptions can unwind through Rust code run destructors and
+// are caught by catch_unwind. Also tests that Rust panics can unwind through
+// C++ code.
+
+#![feature(c_unwind)]
+
+use std::panic::{catch_unwind, AssertUnwindSafe};
+
+struct DropCheck<'a>(&'a mut bool);
+impl<'a> Drop for DropCheck<'a> {
+ fn drop(&mut self) {
+ println!("DropCheck::drop");
+ *self.0 = true;
+ }
+}
+
+extern "C" {
+ fn test_cxx_exception();
+}
+
+extern "C-unwind" {
+ fn cxx_catch_callback(cb: extern "C-unwind" fn(), ok: *mut bool);
+}
+
+#[no_mangle]
+extern "C-unwind" fn rust_catch_callback(cb: extern "C-unwind" fn(), rust_ok: &mut bool) {
+ let _drop = DropCheck(rust_ok);
+ cb();
+ unreachable!("should have unwound instead of returned");
+}
+
+fn test_rust_panic() {
+ extern "C-unwind" fn callback() {
+ println!("throwing rust panic");
+ panic!(1234i32);
+ }
+
+ let mut dropped = false;
+ let mut cxx_ok = false;
+ let caught_unwind = catch_unwind(AssertUnwindSafe(|| {
+ let _drop = DropCheck(&mut dropped);
+ unsafe {
+ cxx_catch_callback(callback, &mut cxx_ok);
+ }
+ unreachable!("should have unwound instead of returned");
+ }));
+ println!("caught rust panic");
+ assert!(dropped);
+ assert!(caught_unwind.is_err());
+ let panic_obj = caught_unwind.unwrap_err();
+ let panic_int = *panic_obj.downcast_ref::<i32>().unwrap();
+ assert_eq!(panic_int, 1234);
+ assert!(cxx_ok);
+}
+
+fn main() {
+ unsafe { test_cxx_exception() };
+ test_rust_panic();
+}