Skip to content

Commit 7c3fde0

Browse files
committed
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()`.
1 parent 547a0ed commit 7c3fde0

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

book/src/binding/result.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,16 @@ you'd like for the Rust error to have.
137137
...namespace behavior {
138138
...
139139
template <typename Try, typename Fail>
140-
static void trycatch(Try &&func, Fail &&fail) noexcept try {
140+
static void trycatch(Try &&func, Fail &&fail) noexcept {
141+
#if defined(RUST_CXX_NO_EXCEPTIONS)
141142
func();
142-
} catch (const std::exception &e) {
143-
fail(e.what());
143+
#else
144+
try {
145+
func();
146+
} catch (const std::exception &e) {
147+
fail(e.what());
148+
}
149+
#endif
144150
}
145151
...
146152
...} // namespace behavior

gen/src/builtin.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,16 @@ pub(super) fn write(out: &mut OutFile) {
410410
" ::std::is_same<decltype(trycatch(::std::declval<Try>(), ::std::declval<Fail>())),",
411411
);
412412
writeln!(out, " missing>::value>::type");
413-
writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{");
413+
writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept {{");
414+
writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)");
414415
writeln!(out, " func();");
415-
writeln!(out, "}} catch (::std::exception const &e) {{");
416-
writeln!(out, " fail(e.what());");
416+
writeln!(out, "#else");
417+
writeln!(out, " try {{");
418+
writeln!(out, " func();");
419+
writeln!(out, " }} catch (::std::exception const &e) {{");
420+
writeln!(out, " fail(e.what());");
421+
writeln!(out, " }}");
422+
writeln!(out, "#endif");
417423
writeln!(out, "}}");
418424
out.end_block(Block::Namespace("behavior"));
419425
}

0 commit comments

Comments
 (0)