Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid further trycatch and throw when exceptions are disabled #1403

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char const *>(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 {
Expand Down