From 7a0f3f2e3c8dc44f9c74b2f6e6165354239b63a8 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Fri, 15 Nov 2024 15:54:45 +0000 Subject: [PATCH] result: without exception do not throw when Result Err just abort For WASM targets the `cc` crate changed to always set `-fno-exceptions`, before only `-fignore-exceptions` was being set elsewhere, so now programs fail to build when using `cxx` if any `throw` or `try ... catch` are used. > error: cannot use 'throw' with exceptions disabled Instead of throwing just abort. --- gen/src/write.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gen/src/write.rs b/gen/src/write.rs index 77e1da0b2..7d3d5380d 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -1123,7 +1123,19 @@ fn write_rust_function_shim_impl( if sig.throws { out.builtin.rust_error = true; writeln!(out, " if (error$.ptr) {{"); + writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)"); + writeln!( + out, + " const auto msg = static_cast(error$.ptr);" + ); + writeln!( + out, + " std::fprintf(stderr, \"Error: %s Aborting.\\n\", msg);" + ); + writeln!(out, " std::abort();"); + writeln!(out, "#else"); writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);"); + writeln!(out, "#endif"); writeln!(out, " }}"); } if indirect_return {