Skip to content

Commit

Permalink
trycatch: do not try catch when no exceptions is enabled
Browse files Browse the repository at this point in the history
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()`.
  • Loading branch information
ahayzen-kdab committed Nov 18, 2024
1 parent 547a0ed commit 7c3fde0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions book/src/binding/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,16 @@ you'd like for the Rust error to have.
...namespace behavior {
...
template <typename Try, typename Fail>
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
Expand Down
12 changes: 9 additions & 3 deletions gen/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,16 @@ pub(super) fn write(out: &mut OutFile) {
" ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
);
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"));
}
Expand Down

0 comments on commit 7c3fde0

Please sign in to comment.