about summary refs log tree commit diff
path: root/tests/run-make/foreign-exceptions/foo.cpp
diff options
context:
space:
mode:
authorJoshua Nelson <github@jyn.dev>2023-03-30 07:34:55 -0500
committerJoshua Nelson <github@jyn.dev>2023-03-30 07:34:55 -0500
commit433da1fc047bb39a263eefca4bdb2b1972f1d2ce (patch)
tree28540e78fdd5fdf158267e67495121ac64f0866a /tests/run-make/foreign-exceptions/foo.cpp
parentf2d9a3d0771504f1ae776226a5799dcb4408a91a (diff)
downloadrust-433da1fc047bb39a263eefca4bdb2b1972f1d2ce.tar.gz
rust-433da1fc047bb39a263eefca4bdb2b1972f1d2ce.zip
Move almost all run-make-fulldeps to run-make
They pass fine.
Diffstat (limited to 'tests/run-make/foreign-exceptions/foo.cpp')
-rw-r--r--tests/run-make/foreign-exceptions/foo.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/run-make/foreign-exceptions/foo.cpp b/tests/run-make/foreign-exceptions/foo.cpp
new file mode 100644
index 00000000000..8182021a2cc
--- /dev/null
+++ b/tests/run-make/foreign-exceptions/foo.cpp
@@ -0,0 +1,60 @@
+#include <assert.h>
+#include <stddef.h>
+#include <stdio.h>
+
+void println(const char* s) {
+    puts(s);
+    fflush(stdout);
+}
+
+struct exception {};
+struct rust_panic {};
+
+struct drop_check {
+    bool* ok;
+    ~drop_check() {
+        println("~drop_check");
+
+        if (ok)
+            *ok = true;
+    }
+};
+
+extern "C" {
+    void rust_catch_callback(void (*cb)(), bool* rust_ok);
+
+    void throw_cxx_exception() {
+        println("throwing C++ exception");
+        throw exception();
+    }
+
+    void test_cxx_exception() {
+        bool rust_ok = false;
+        try {
+            rust_catch_callback(throw_cxx_exception, &rust_ok);
+            assert(false && "unreachable");
+        } catch (exception e) {
+            println("caught C++ exception");
+            assert(rust_ok);
+            return;
+        }
+        assert(false && "did not catch thrown C++ exception");
+    }
+
+    void cxx_catch_callback(void (*cb)(), bool* cxx_ok) {
+        drop_check x;
+        x.ok = NULL;
+        try {
+            cb();
+        } catch (rust_panic e) {
+            assert(false && "shouldn't be able to catch a rust panic");
+        } catch (...) {
+            println("caught foreign exception in catch (...)");
+            // Foreign exceptions are caught by catch (...). We only set the ok
+            // flag if we successfully caught the panic. The destructor of
+            // drop_check will then set the flag to true if it is executed.
+            x.ok = cxx_ok;
+            throw;
+        }
+    }
+}