From 7c3fde0837281962f052004bb6ed7eb64739d9b7 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Fri, 15 Nov 2024 15:39:07 +0000 Subject: [PATCH] trycatch: do not try catch when no exceptions is enabled 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 'try' with exceptions disabled Instead for trycatch just call `func()`. --- book/src/binding/result.md | 12 +++++++++--- gen/src/builtin.rs | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/book/src/binding/result.md b/book/src/binding/result.md index 733212a63..856d49ae7 100644 --- a/book/src/binding/result.md +++ b/book/src/binding/result.md @@ -137,10 +137,16 @@ you'd like for the Rust error to have. ...namespace behavior { ... template -static void trycatch(Try &&func, Fail &&fail) noexcept try { +static void trycatch(Try &&func, Fail &&fail) noexcept { +#if defined(RUST_CXX_NO_EXCEPTIONS) func(); -} catch (const std::exception &e) { - fail(e.what()); +#else + try { + func(); + } catch (const std::exception &e) { + fail(e.what()); + } +#endif } ... ...} // namespace behavior diff --git a/gen/src/builtin.rs b/gen/src/builtin.rs index d38473afc..37c33709b 100644 --- a/gen/src/builtin.rs +++ b/gen/src/builtin.rs @@ -410,10 +410,16 @@ pub(super) fn write(out: &mut OutFile) { " ::std::is_same(), ::std::declval())),", ); writeln!(out, " missing>::value>::type"); - writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); + writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept {{"); + writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)"); writeln!(out, " func();"); - writeln!(out, "}} catch (::std::exception const &e) {{"); - writeln!(out, " fail(e.what());"); + writeln!(out, "#else"); + writeln!(out, " try {{"); + writeln!(out, " func();"); + writeln!(out, " }} catch (::std::exception const &e) {{"); + writeln!(out, " fail(e.what());"); + writeln!(out, " }}"); + writeln!(out, "#endif"); writeln!(out, "}}"); out.end_block(Block::Namespace("behavior")); }