From bc16c97087d25d243ae3919528f75b7cf436a8af Mon Sep 17 00:00:00 2001 From: Cra3z Date: Tue, 17 Nov 2026 13:51:24 +0800 Subject: [PATCH 1/2] Support -fno-exception --- examples/sender_demo.cpp | 4 ++ examples/stackoverflow.cpp | 2 + examples/stop_token.cpp | 4 ++ .../execution/detail/allocator_aware_move.hpp | 5 ++- .../beman/execution/detail/as_except_ptr.hpp | 9 +++++ include/beman/execution/detail/bulk.hpp | 5 ++- include/beman/execution/detail/common.hpp | 17 +++++++-- .../execution/detail/completion_storage.hpp | 10 ++--- .../execution/detail/connect_awaitable.hpp | 5 +-- .../beman/execution/detail/continues_on.hpp | 5 ++- .../detail/get_completion_signatures.hpp | 4 +- include/beman/execution/detail/let.hpp | 5 +-- include/beman/execution/detail/new_object.hpp | 7 ++-- .../execution/detail/psched_bulk_sender.hpp | 37 ++++++++++--------- include/beman/execution/detail/read_env.hpp | 10 +++-- .../execution/detail/sender_awaitable.hpp | 9 ++--- include/beman/execution/detail/spawn.hpp | 7 ++-- .../beman/execution/detail/spawn_future.hpp | 12 +++--- include/beman/execution/detail/split.hpp | 15 ++++---- include/beman/execution/detail/sync_wait.hpp | 9 ++--- .../beman/execution/detail/task_scheduler.hpp | 5 +-- include/beman/execution/detail/then.hpp | 5 ++- .../execution/detail/thread_pool_backend.hpp | 5 +-- include/beman/execution/detail/when_all.hpp | 10 +++-- include/beman/execution/detail/within.hpp | 5 ++- tests/beman/execution/exec-bulk.test.cpp | 14 ++++--- .../exec-completion-storage.test.cpp | 6 ++- tests/beman/execution/exec-connect.test.cpp | 8 +++- tests/beman/execution/exec-general.test.cpp | 2 + tests/beman/execution/exec-just.test.cpp | 5 ++- tests/beman/execution/exec-let.test.cpp | 5 ++- .../exec-parallel-scheduler.test.cpp | 4 ++ .../execution/exec-spawn-future.test.cpp | 4 +- .../beman/execution/exec-stopped-err.test.cpp | 6 +++ tests/beman/execution/exec-sync-wait.test.cpp | 15 ++++++-- tests/beman/execution/exec-task.test.cpp | 6 +++ tests/beman/execution/exec-when-all.test.cpp | 5 ++- .../exec-with-awaitable-senders.test.cpp | 12 ++---- tests/beman/execution/exec-within.test.cpp | 4 ++ .../beman/execution/stopsource-cons.test.cpp | 15 ++++++-- .../execution/stopsource-general.test.cpp | 10 +++-- tests/beman/execution/stopsource-mem.test.cpp | 6 +-- tests/beman/execution/stoptoken-mem.test.cpp | 5 ++- 43 files changed, 214 insertions(+), 129 deletions(-) diff --git a/examples/sender_demo.cpp b/examples/sender_demo.cpp index 255b85603..67e5745c4 100644 --- a/examples/sender_demo.cpp +++ b/examples/sender_demo.cpp @@ -64,7 +64,9 @@ static_assert(ex::sender>); static_assert(ex::sender_in>); int main() { +#ifdef __cpp_exceptions try { +#endif auto j = just_sender{std::pmr::string("value")}; auto t = std::move(j) | ex::then([](const std::pmr::string& v) { return v + " then"; }); auto w = ex::when_all(std::move(t)); @@ -79,7 +81,9 @@ int main() { } else std::cout << "operation was cancelled\n"; std::cout << "after start\n"; +#ifdef __cpp_exceptions } catch (const std::exception& ex) { std::cout << "ERROR: " << ex.what() << "\n"; } +#endif } diff --git a/examples/stackoverflow.cpp b/examples/stackoverflow.cpp index 275886d14..cb8c5808e 100644 --- a/examples/stackoverflow.cpp +++ b/examples/stackoverflow.cpp @@ -97,6 +97,7 @@ int main(int ac, char*[]) { for (int i{}; i < n; ++i) { std::cout << "await just=" << (co_await ex::just(i)) << "\n"; } +#ifdef __cpp_exceptions if constexpr (false) for (int i{}; i < n; ++i) { try { @@ -105,6 +106,7 @@ int main(int ac, char*[]) { std::cout << "await error=" << x << "\n"; } } +#endif co_await ex::just_stopped(); }(ac < 2 ? 3 : 30000)); #endif diff --git a/examples/stop_token.cpp b/examples/stop_token.cpp index c61bfb3b5..4d5f1e319 100644 --- a/examples/stop_token.cpp +++ b/examples/stop_token.cpp @@ -106,7 +106,9 @@ auto inactive(Token token) -> void { } // namespace auto main() -> int { +#ifdef __cpp_exceptions try { +#endif exec::stop_source source; ::std::thread act([token = source.get_token()] { active(token); }); @@ -119,7 +121,9 @@ auto main() -> int { act.join(); inact.join(); print("done\n"); +#ifdef __cpp_exceptions } catch (const std::exception& ex) { std::cout << "ERROR: " << ex.what() << "\n"; } +#endif } diff --git a/include/beman/execution/detail/allocator_aware_move.hpp b/include/beman/execution/detail/allocator_aware_move.hpp index 3d54585b1..0c2e38a9d 100644 --- a/include/beman/execution/detail/allocator_aware_move.hpp +++ b/include/beman/execution/detail/allocator_aware_move.hpp @@ -32,7 +32,7 @@ template * \internal */ auto allocator_aware_move(T&& obj, Context&& context) noexcept -> decltype(auto) { - try { + BEMAN_EXECUTION_TRY { if constexpr (requires { ::beman::execution::get_allocator(::beman::execution::get_env(context)); }) { if constexpr (decltype(::beman::execution::detail::is_product_type(obj))()) { return obj.make_from(::beman::execution::get_allocator(::beman::execution::get_env(context)), @@ -44,7 +44,8 @@ auto allocator_aware_move(T&& obj, Context&& context) noexcept -> decltype(auto) } else { return ::std::forward(obj); } - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { ::std::terminate(); //-dk:TODO investigate if that can be avoided } } diff --git a/include/beman/execution/detail/as_except_ptr.hpp b/include/beman/execution/detail/as_except_ptr.hpp index 4e2914d02..4860681a0 100644 --- a/include/beman/execution/detail/as_except_ptr.hpp +++ b/include/beman/execution/detail/as_except_ptr.hpp @@ -27,6 +27,7 @@ namespace beman::execution::detail { template //-dk:TODO add a proper precondition decltype(auto) as_except_ptr(Error&& error) { +#ifdef __cpp_exceptions if constexpr (::std::same_as<::std::exception_ptr, ::std::decay_t>) { return ::std::forward(error); } else if constexpr (::std::same_as<::std::error_code, ::std::decay_t>) { @@ -34,6 +35,14 @@ decltype(auto) as_except_ptr(Error&& error) { } else { return ::std::make_exception_ptr(::std::forward(error)); } +#else + if constexpr (::std::same_as<::std::exception_ptr, ::std::decay_t>) { + return ::std::forward(error); + } else { + ::std::terminate(); + return ::std::exception_ptr(); + } +#endif } } // namespace beman::execution::detail diff --git a/include/beman/execution/detail/bulk.hpp b/include/beman/execution/detail/bulk.hpp index a624f3e41..8449888a1 100644 --- a/include/beman/execution/detail/bulk.hpp +++ b/include/beman/execution/detail/bulk.hpp @@ -179,13 +179,14 @@ struct bulk_algo_t { auto& [policy, shape, f] = state; constexpr bool nothrow = ::beman::execution::detail::bulk_traits::is_nothrow_invocable; - try { + BEMAN_EXECUTION_TRY { [&]() noexcept(nothrow) { ::beman::execution::detail::bulk_traits::invoke( f, shape, args...); Tag()(::std::move(rcvr), ::std::forward(args)...); }(); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (!nothrow) { ::beman::execution::set_error(::std::move(rcvr), ::std::current_exception()); } diff --git a/include/beman/execution/detail/common.hpp b/include/beman/execution/detail/common.hpp index 40f562889..77cc1a34f 100644 --- a/include/beman/execution/detail/common.hpp +++ b/include/beman/execution/detail/common.hpp @@ -21,11 +21,22 @@ #define BEMAN_EXECUTION_WITH_DEFAULT_PARALLEL_SCHEDULER_BACKEND 1 #endif +#ifdef __cpp_exceptions +#define BEMAN_EXECUTION_TRY try +#define BEMAN_EXECUTION_CATCH(e) catch (e) +#define BEMAN_EXECUTION_THROW(...) throw __VA_ARGS__ +#define BEMAN_EXECUTION_RETHROW(ep) ::std::rethrow_exception(ep) +#else +#define BEMAN_EXECUTION_TRY if constexpr (true) +#define BEMAN_EXECUTION_CATCH(e) else +#define BEMAN_EXECUTION_THROW(...) ::std::terminate() +#define BEMAN_EXECUTION_RETHROW(ep) ::std::terminate() +#endif + #define BEMAN_EXECUTION_TRY_EVAL(rcvr, expr) \ do { \ - try { \ - (expr); \ - } catch (...) { \ + BEMAN_EXECUTION_TRY { (expr); } \ + BEMAN_EXECUTION_CATCH(...) { \ if constexpr (!noexcept((expr))) { \ ::beman::execution::set_error(::std::move((rcvr)), ::std::current_exception()); \ } \ diff --git a/include/beman/execution/detail/completion_storage.hpp b/include/beman/execution/detail/completion_storage.hpp index 0f4beba23..f143a44c0 100644 --- a/include/beman/execution/detail/completion_storage.hpp +++ b/include/beman/execution/detail/completion_storage.hpp @@ -63,9 +63,8 @@ struct variant_for_signatures<::beman::execution::completion_signatures template static auto store_into_variant(type& storage, Tag t, Args&&... args) noexcept { using tuple_type = decltype(select_tuple(t, ::std::forward(args)...)); - try { - storage.template emplace(t, ::std::forward(args)...); - } catch (...) { + BEMAN_EXECUTION_TRY { storage.template emplace(t, ::std::forward(args)...); } + BEMAN_EXECUTION_CATCH(...) { if constexpr (!std::is_nothrow_constructible_v) { store_into_variant(storage, ::beman::execution::set_error_t{}, ::std::current_exception()); } else { @@ -82,7 +81,7 @@ struct variant_for_signatures<::beman::execution::completion_signatures std::visit( [&](auto&& tuple_data) { using tuple_type = ::std::remove_cvref_t; - try { + BEMAN_EXECUTION_TRY { if constexpr (std::is_same_v) { ::beman::execution::detail::unreachable(); } else { @@ -93,7 +92,8 @@ struct variant_for_signatures<::beman::execution::completion_signatures // Make a copy, in case the tuple is destroyed by the completion call. tuple_type{::std::move(tuple_data)}); } - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (!std::is_nothrow_constructible_v) { ::beman::execution::set_error(::std::forward(receiver), ::std::current_exception()); } else { diff --git a/include/beman/execution/detail/connect_awaitable.hpp b/include/beman/execution/detail/connect_awaitable.hpp index 25e6d9dcc..94666116e 100644 --- a/include/beman/execution/detail/connect_awaitable.hpp +++ b/include/beman/execution/detail/connect_awaitable.hpp @@ -88,7 +88,7 @@ auto connect_awaitable(Awaiter awaiter, Receiver receiver) ::beman::execution::detail::connect_awaitable_promise>; ::std::exception_ptr ep; - try { + BEMAN_EXECUTION_TRY { if constexpr (std::same_as) { co_await ::std::move(awaiter); co_await ::beman::execution::detail::suspend_complete(::beman::execution::set_value, @@ -97,9 +97,8 @@ auto connect_awaitable(Awaiter awaiter, Receiver receiver) co_await ::beman::execution::detail::suspend_complete( ::beman::execution::set_value, ::std::move(receiver), co_await ::std::move(awaiter)); } - } catch (...) { - ep = ::std::current_exception(); } + BEMAN_EXECUTION_CATCH(...) { ep = ::std::current_exception(); } co_await ::beman::execution::detail::suspend_complete(::beman::execution::set_error, ::std::move(receiver), ep); } } // namespace beman::execution::detail diff --git a/include/beman/execution/detail/continues_on.hpp b/include/beman/execution/detail/continues_on.hpp index d292619d3..3c7021614 100644 --- a/include/beman/execution/detail/continues_on.hpp +++ b/include/beman/execution/detail/continues_on.hpp @@ -328,11 +328,12 @@ struct continues_on_t { using result_t = ::beman::execution::detail::decayed_tuple; constexpr bool nothrow = (::std::is_nothrow_constructible_v<::std::decay_t, Args> && ...); - try { + BEMAN_EXECUTION_TRY { [&]() noexcept(nothrow) { state.async_result.template emplace(Tag(), ::std::forward(args)...); }(); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (!nothrow) { state.async_result .template emplace<::std::tuple<::beman::execution::set_error_t, ::std::exception_ptr>>( diff --git a/include/beman/execution/detail/get_completion_signatures.hpp b/include/beman/execution/detail/get_completion_signatures.hpp index 9acc05cb2..eda810297 100644 --- a/include/beman/execution/detail/get_completion_signatures.hpp +++ b/include/beman/execution/detail/get_completion_signatures.hpp @@ -110,10 +110,10 @@ consteval auto get_completion_signatures() -> ::beman::execution::detail::valid_ ::beman::execution::set_stopped_t()>{}; } } else if constexpr (sizeof...(Env) == 0) - return (throw ::beman::execution::detail::dependent_sender_error(), + return (BEMAN_EXECUTION_THROW(::beman::execution::detail::dependent_sender_error()), ::beman::execution::completion_signatures<>{}); else - return (throw ::beman::execution::detail::get_completion_signatures_error(), + return (BEMAN_EXECUTION_THROW(::beman::execution::detail::get_completion_signatures_error()), ::beman::execution::completion_signatures<>{}); } } // namespace beman::execution diff --git a/include/beman/execution/detail/let.hpp b/include/beman/execution/detail/let.hpp index 1502e118c..47df59b42 100644 --- a/include/beman/execution/detail/let.hpp +++ b/include/beman/execution/detail/let.hpp @@ -367,9 +367,8 @@ struct let_t { template auto operator()(auto, auto& state, Receiver& receiver, Tag, Args&&... args) const { if constexpr (::std::same_as) { - try { - (let_bind)(state, receiver, ::std::forward(args)...); - } catch (...) { + BEMAN_EXECUTION_TRY { (let_bind)(state, receiver, ::std::forward(args)...); } + BEMAN_EXECUTION_CATCH(...) { if constexpr (not nothrow) { ::beman::execution::set_error(::std::move(receiver), ::std::current_exception()); } diff --git a/include/beman/execution/detail/new_object.hpp b/include/beman/execution/detail/new_object.hpp index 1c9038397..b1fcc703a 100644 --- a/include/beman/execution/detail/new_object.hpp +++ b/include/beman/execution/detail/new_object.hpp @@ -21,12 +21,13 @@ template using alloc_traits = ::std::allocator_traits; alloc_t alloc(allocator); auto ptr = alloc_traits::allocate(alloc, 1); - try { + BEMAN_EXECUTION_TRY { alloc_traits::construct(alloc, ptr, ::std::forward(args)...); return ptr; - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { alloc_traits::deallocate(alloc, ptr, 1); - throw; + BEMAN_EXECUTION_THROW(); } } diff --git a/include/beman/execution/detail/psched_bulk_sender.hpp b/include/beman/execution/detail/psched_bulk_sender.hpp index 30228f745..3d0affae4 100644 --- a/include/beman/execution/detail/psched_bulk_sender.hpp +++ b/include/beman/execution/detail/psched_bulk_sender.hpp @@ -108,7 +108,7 @@ struct psched_bulk_sender { const Shape first = is_parallel_policy ? static_cast(begin) : Shape(0); const Shape last = is_parallel_policy ? static_cast(end) : shape; const auto call_fn = [=, this](Args&... args) { - try { + BEMAN_EXECUTION_TRY { if constexpr (IsChunked) { static_assert(::std::invocable); fn(first, last, args...); @@ -118,7 +118,8 @@ struct psched_bulk_sender { fn(i, args...); } } - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if (!has_exception.exchange(true, ::std::memory_order_relaxed)) { exception = ::std::current_exception(); } @@ -143,7 +144,7 @@ struct psched_bulk_sender { const auto call_set_value = [this](Args&&... args) { ::beman::execution::set_value(::std::move(rcvr), ::std::forward(args)...); }; - try { + BEMAN_EXECUTION_TRY { ::std::visit( [&](T tpl) { if constexpr (::std::same_as) { @@ -153,9 +154,8 @@ struct psched_bulk_sender { } }, ::std::move(result)); - } catch (...) { - this->set_error(::std::current_exception()); } + BEMAN_EXECUTION_CATCH(...) { this->set_error(::std::current_exception()); } } template @@ -195,20 +195,21 @@ struct psched_bulk_sender { using receiver_concept = ::beman::execution::receiver_tag; template - auto set_value(Args&&... args) noexcept -> void try { - using arg_t = ::beman::execution::detail::decayed_tuple; - proxy->result.template emplace(::std::forward(args)...); - if (!proxy->backend) [[unlikely]] { - ::std::terminate(); - } - const ::std::size_t s = is_parallel_policy ? proxy->shape : 1uz; - if constexpr (IsChunked) { - proxy->backend->schedule_bulk_chunked(s, *proxy, proxy->storage); - } else { - proxy->backend->schedule_bulk_unchunked(s, *proxy, proxy->storage); + auto set_value(Args&&... args) noexcept -> void { + BEMAN_EXECUTION_TRY { + using arg_t = ::beman::execution::detail::decayed_tuple; + proxy->result.template emplace(::std::forward(args)...); + if (!proxy->backend) [[unlikely]] { + ::std::terminate(); + } + const ::std::size_t s = is_parallel_policy ? proxy->shape : 1uz; + if constexpr (IsChunked) { + proxy->backend->schedule_bulk_chunked(s, *proxy, proxy->storage); + } else { + proxy->backend->schedule_bulk_unchunked(s, *proxy, proxy->storage); + } } - } catch (...) { - proxy->set_error(::std::current_exception()); + BEMAN_EXECUTION_CATCH(...) { proxy->set_error(::std::current_exception()); } } template diff --git a/include/beman/execution/detail/read_env.hpp b/include/beman/execution/detail/read_env.hpp index d2dc3a263..5a707351e 100644 --- a/include/beman/execution/detail/read_env.hpp +++ b/include/beman/execution/detail/read_env.hpp @@ -113,10 +113,11 @@ struct read_env_t { struct start_impl { template auto operator()(Query query, Receiver& receiver) const noexcept -> void { - try { + BEMAN_EXECUTION_TRY { auto env{::beman::execution::get_env(receiver)}; ::beman::execution::set_value(::std::move(receiver), query(env)); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (!std::is_nothrow_invocable_v>) { ::beman::execution::set_error(::std::move(receiver), ::std::current_exception()); } @@ -129,9 +130,10 @@ struct read_env_t { static consteval void check_types() { using query_type = ::std::decay_t<::beman::execution::detail::data_type>; if constexpr (!requires(const Env& env) { query_type()(env); }) { - throw ::std::logic_error("query is not invocable with the environment of the receiver"); + BEMAN_EXECUTION_THROW( + ::std::logic_error("query is not invocable with the environment of the receiver")); } else if constexpr (::std::same_as<::std::invoke_result_t, void>) { - throw ::std::logic_error("query returns void with the environment of the receiver"); + BEMAN_EXECUTION_THROW(::std::logic_error("query returns void with the environment of the receiver")); } } }; diff --git a/include/beman/execution/detail/sender_awaitable.hpp b/include/beman/execution/detail/sender_awaitable.hpp index 88ab82e68..0d09be8c4 100644 --- a/include/beman/execution/detail/sender_awaitable.hpp +++ b/include/beman/execution/detail/sender_awaitable.hpp @@ -68,11 +68,8 @@ class sender_awaitable { template requires ::std::constructible_from void set_value(Args&&... args) && noexcept { - try { - ::std::get<0>(*result_ptr_).template emplace<1>(::std::forward(args)...); - } catch (...) { - ::std::get<0>(*result_ptr_).template emplace<2>(::std::current_exception()); - } + BEMAN_EXECUTION_TRY { ::std::get<0>(*result_ptr_).template emplace<1>(::std::forward(args)...); } + BEMAN_EXECUTION_CATCH(...) { ::std::get<0>(*result_ptr_).template emplace<2>(::std::current_exception()); } this->resume(); } template @@ -120,7 +117,7 @@ class sender_awaitable { } value_type await_resume() { if (::std::holds_alternative<::std::exception_ptr>(::std::get<0>(result))) { - ::std::rethrow_exception(::std::get<::std::exception_ptr>(::std::get<0>(result))); + BEMAN_EXECUTION_RETHROW(::std::get<::std::exception_ptr>(::std::get<0>(result))); } if constexpr (::std::is_void_v) { return; diff --git a/include/beman/execution/detail/spawn.hpp b/include/beman/execution/detail/spawn.hpp index b85eaefde..8ae50a9bd 100644 --- a/include/beman/execution/detail/spawn.hpp +++ b/include/beman/execution/detail/spawn.hpp @@ -108,11 +108,12 @@ struct spawn_t { using traits_t = ::std::allocator_traits; alloc_t alloc(all); state_t* op{traits_t::allocate(alloc, 1u)}; - try { + BEMAN_EXECUTION_TRY { traits_t::construct(alloc, op, all, ::beman::execution::write_env(::std::move(new_sender), senv), tok); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { traits_t::deallocate(alloc, op, 1u); - throw; + BEMAN_EXECUTION_THROW(); } } template <::beman::execution::sender Sender, ::beman::execution::scope_token Token> diff --git a/include/beman/execution/detail/spawn_future.hpp b/include/beman/execution/detail/spawn_future.hpp index df9aa8352..2e8ea11d2 100644 --- a/include/beman/execution/detail/spawn_future.hpp +++ b/include/beman/execution/detail/spawn_future.hpp @@ -123,10 +123,11 @@ struct spawn_future_receiver { template auto set_complete(T&&... t) noexcept { - try { + BEMAN_EXECUTION_TRY { this->state->result.template emplace<::beman::execution::detail::decayed_tuple>( Tag(), ::std::forward(t)...); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (!state_t::has_non_throwing_args_copy) { this->state->result .template emplace<::std::tuple<::beman::execution::set_error_t, ::std::exception_ptr>>( @@ -260,11 +261,10 @@ class spawn_future_t { using state_traits_t = ::std::allocator_traits; state_alloc_t state_alloc(alloc); state_t* op{state_traits_t::allocate(state_alloc, 1u)}; - try { - state_traits_t::construct(state_alloc, op, alloc, make(), tok, senv); - } catch (...) { + BEMAN_EXECUTION_TRY { state_traits_t::construct(state_alloc, op, alloc, make(), tok, senv); } + BEMAN_EXECUTION_CATCH(...) { state_traits_t::deallocate(state_alloc, op, 1u); - throw; + BEMAN_EXECUTION_THROW(); } using deleter = decltype([](state_t* p) noexcept { p->abandon(); }); diff --git a/include/beman/execution/detail/split.hpp b/include/beman/execution/detail/split.hpp index c85f2c07f..2b98eb082 100644 --- a/include/beman/execution/detail/split.hpp +++ b/include/beman/execution/detail/split.hpp @@ -124,9 +124,8 @@ struct impls_for : ::beman::execution::detail::default_impls { template void complete(Tag, Args&&... args) noexcept { using tuple_t = ::beman::execution::detail::decayed_tuple; - try { - sh_state->result.template emplace(Tag(), ::std::forward(args)...); - } catch (...) { + BEMAN_EXECUTION_TRY { sh_state->result.template emplace(Tag(), ::std::forward(args)...); } + BEMAN_EXECUTION_CATCH(...) { using tuple_err = ::std::tuple<::beman::execution::set_error_t, ::std::exception_ptr>; sh_state->result.template emplace(::beman::execution::set_error, ::std::current_exception()); @@ -172,11 +171,12 @@ struct impls_for : ::beman::execution::detail::default_impls { using child_operation_state = ::beman::execution::connect_result_t>; explicit shared_state(Sndr&& sndr) { - try { + BEMAN_EXECUTION_TRY { op_state.emplace(::beman::execution::detail::emplace_from{[&] { return ::beman::execution::connect(::std::forward(sndr), split_receiver{this}); }}); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { using error_tuple_t = ::std::tuple<::beman::execution::set_error_t, ::std::exception_ptr>; result.template emplace(::beman::execution::set_error, ::std::current_exception()); [[maybe_unused]] auto queue = waiting_states.pop_all_and_shutdown(); @@ -277,7 +277,7 @@ struct impls_for : ::beman::execution::detail::default_impls { } else { assert(sh_state->result.index() > 0); assert(!sh_state->result.valueless_by_exception()); - try { + BEMAN_EXECUTION_TRY { ::std::visit( [&](const Arg& arg) noexcept -> void { if constexpr (!::std::same_as<::std::decay_t, ::std::monostate>) { @@ -289,7 +289,8 @@ struct impls_for : ::beman::execution::detail::default_impls { } }, sh_state->result); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // required by clang-tidy although it is not necessary here // see valueless_by_exception() check above std::terminate(); diff --git a/include/beman/execution/detail/sync_wait.hpp b/include/beman/execution/detail/sync_wait.hpp index 1dabcd116..03e608716 100644 --- a/include/beman/execution/detail/sync_wait.hpp +++ b/include/beman/execution/detail/sync_wait.hpp @@ -87,11 +87,8 @@ struct sync_wait_receiver { auto set_stopped() && noexcept -> void { this->state->loop.finish(); } template auto set_value(Args&&... args) && noexcept -> void { - try { - this->state->result.emplace(::std::forward(args)...); - } catch (...) { - this->state->error = ::std::current_exception(); - } + BEMAN_EXECUTION_TRY { this->state->result.emplace(::std::forward(args)...); } + BEMAN_EXECUTION_CATCH(...) { this->state->error = ::std::current_exception(); } this->state->loop.finish(); } @@ -110,7 +107,7 @@ struct sync_wait_t { state.loop.run(); if (state.error) { - ::std::rethrow_exception(state.error); + BEMAN_EXECUTION_RETHROW(state.error); } return ::std::move(state.result); } diff --git a/include/beman/execution/detail/task_scheduler.hpp b/include/beman/execution/detail/task_scheduler.hpp index a5833590c..6fc37fc46 100644 --- a/include/beman/execution/detail/task_scheduler.hpp +++ b/include/beman/execution/detail/task_scheduler.hpp @@ -287,7 +287,7 @@ struct task_scheduler_backend_for : task_scheduler_backend_sched { using allocated_holder = typename base::template allocated_state_holder; constexpr bool inlined = sizeof(inlined_holder) <= psched_storage_size && alignof(inlined_holder) <= psched_storage_alignment; - try { + BEMAN_EXECUTION_TRY { if constexpr (inlined) { auto holder = ::new (static_cast(storage.data())) inlined_holder(::std::move(sndr), proxy); holder->do_start(); @@ -296,9 +296,8 @@ struct task_scheduler_backend_for : task_scheduler_backend_sched { ::beman::execution::detail::new_object(alloc, ::std::move(sndr), proxy, alloc); holder->do_start(); } - } catch (...) { - proxy.set_error(::std::current_exception()); } + BEMAN_EXECUTION_CATCH(...) { proxy.set_error(::std::current_exception()); } } [[no_unique_address]] Alloc alloc; diff --git a/include/beman/execution/detail/then.hpp b/include/beman/execution/detail/then.hpp index 7ebdf093c..5a8cb0779 100644 --- a/include/beman/execution/detail/then.hpp +++ b/include/beman/execution/detail/then.hpp @@ -170,7 +170,7 @@ struct then_t { template auto operator()(auto, auto& fun, auto& receiver, Tag, Args&&... args) const noexcept -> void { if constexpr (::std::same_as) { - try { + BEMAN_EXECUTION_TRY { auto invoke = [&] { return ::std::invoke(::std::move(fun), ::std::forward(args)...); }; if constexpr (::std::same_as) { invoke(); @@ -178,7 +178,8 @@ struct then_t { } else { ::beman::execution::set_value(::std::move(receiver), invoke()); } - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (not noexcept(::std::invoke(::std::move(fun), ::std::forward(args)...) )) { diff --git a/include/beman/execution/detail/thread_pool_backend.hpp b/include/beman/execution/detail/thread_pool_backend.hpp index 2c82c520c..9ba62ef75 100644 --- a/include/beman/execution/detail/thread_pool_backend.hpp +++ b/include/beman/execution/detail/thread_pool_backend.hpp @@ -226,7 +226,7 @@ class thread_pool_backend_base const ::std::size_t threads = num_threads(); const ::std::size_t chunk_length = shape / threads + (shape % threads != 0uz); const ::std::size_t chunk_count = shape / chunk_length + (shape % chunk_length != 0uz); - try { + BEMAN_EXECUTION_TRY { if (chunk_count == 1uz) { submit(::std::construct_at( reinterpret_cast(storage.data()), proxy, shape, one_by_one)); @@ -250,9 +250,8 @@ class thread_pool_backend_base // NOLINTEND(*-reinterpret-cast, *-pointer-arithmetic-on-polymorphic-object, *-ctr56-cpp) submit(head, chunk_count); } - } catch (...) { - proxy.set_error(::std::current_exception()); } + BEMAN_EXECUTION_CATCH(...) { proxy.set_error(::std::current_exception()); } } virtual auto submit(task_base* t, ::std::size_t n = 1uz) noexcept -> void = 0; diff --git a/include/beman/execution/detail/when_all.hpp b/include/beman/execution/detail/when_all.hpp index a0c36bb33..423d95bfb 100644 --- a/include/beman/execution/detail/when_all.hpp +++ b/include/beman/execution/detail/when_all.hpp @@ -373,11 +373,12 @@ struct when_all_t { state.stop_src.request_stop(); using error_t = typename std::type_identity::type; constexpr bool nothrow = ::std::is_nothrow_constructible_v<::std::decay_t, error_t>; - try { + BEMAN_EXECUTION_TRY { [&]() noexcept(nothrow) { state.errors.template emplace<::std::decay_t>(::std::forward(args)...); }(); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (!nothrow) { state.errors.template emplace<::std::exception_ptr>(::std::current_exception()); } @@ -393,9 +394,10 @@ struct when_all_t { auto& opt = ::std::get(state.values); using decayed_tuple_t = typename ::std::decay_t::value_type; constexpr bool nothrow = std::is_nothrow_constructible_v; - try { + BEMAN_EXECUTION_TRY { [&]() noexcept(nothrow) { opt.emplace(::std::forward(args)...); }(); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (!nothrow) { if (disposition::error != state.disp.exchange(disposition::error)) { state.stop_src.request_stop(); diff --git a/include/beman/execution/detail/within.hpp b/include/beman/execution/detail/within.hpp index 523851164..2042abc57 100644 --- a/include/beman/execution/detail/within.hpp +++ b/include/beman/execution/detail/within.hpp @@ -296,7 +296,7 @@ struct within_t::state { const auto p = std::get_if(&state_); assert(p); - try { + BEMAN_EXECUTION_TRY { auto work_sender = ::std::move(p->work); auto& new_state = this->state_.template emplace(::beman::execution::detail::elide([&]() noexcept { @@ -304,7 +304,8 @@ struct within_t::state { ::std::move(exit_sender)}; })); ::beman::execution::start(new_state.op); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { if constexpr (nothrow) { ::beman::execution::detail::unreachable(); } else { diff --git a/tests/beman/execution/exec-bulk.test.cpp b/tests/beman/execution/exec-bulk.test.cpp index 626f1f2db..53a3a1c41 100644 --- a/tests/beman/execution/exec-bulk.test.cpp +++ b/tests/beman/execution/exec-bulk.test.cpp @@ -308,6 +308,7 @@ auto test_bulk_shape_zero() { ASSERT(counter == 0); } +#ifdef __cpp_exceptions auto test_bulk_exception_handling() { bool error_caught = false; @@ -353,6 +354,7 @@ auto test_bulk_unchunked_exception_handling() { } ASSERT(error_caught); } +#endif // __cpp_exceptions auto test_bulk_chunked_noexcept() { auto b0 = test_std::bulk_chunked(test_std::just(), test_std::seq, 1, [](int, int) noexcept {}); @@ -423,7 +425,7 @@ auto test_bulk_multiple_values() { ASSERT(sum_a == 30); ASSERT(sum_b == 60 + 0 + 1 + 2); } -#ifdef __cpp_lib_parallel_algorithm +#if defined(__cpp_lib_parallel_algorithm) && defined(__cpp_exceptions) template struct pstl_for_each_sender { using sender_concept = test_std::sender_tag; @@ -595,7 +597,7 @@ auto test_bulk_attributes() { TEST(exec_bulk) { - try { + BEMAN_EXECUTION_TRY { test_bulk(); test_bulk_predecessor_result(); @@ -609,21 +611,21 @@ TEST(exec_bulk) { test_bulk_unchunked_pipeable(); test_execution_policies(); test_bulk_shape_zero(); +#ifdef __cpp_exceptions test_bulk_exception_handling(); test_bulk_chunked_exception_handling(); test_bulk_unchunked_exception_handling(); +#endif test_bulk_chunked_noexcept(); test_bulk_unchunked_noexcept(); test_bulk_shape_one(); test_bulk_chunked_covers_full_range(); test_bulk_multiple_values(); -#ifdef __cpp_lib_parallel_algorithm +#if defined(__cpp_lib_parallel_algorithm) && defined(__cpp_exceptions) test_bulk_customization(); #endif - } catch (...) { - - ASSERT(nullptr == +"the bulk tests shouldn't throw"); } + BEMAN_EXECUTION_CATCH(...) { ASSERT(nullptr == +"the bulk tests shouldn't throw"); } test_bulk_attributes(); return 0; diff --git a/tests/beman/execution/exec-completion-storage.test.cpp b/tests/beman/execution/exec-completion-storage.test.cpp index 28573f1c7..540323fc6 100644 --- a/tests/beman/execution/exec-completion-storage.test.cpp +++ b/tests/beman/execution/exec-completion-storage.test.cpp @@ -151,12 +151,13 @@ struct throw_on_move { throw_on_move(int xx, int i = 1) : x(xx), iteration(i) {} throw_on_move(throw_on_move&& other) : x(other.x), iteration(other.iteration) { if (--iteration <= 0) { - throw std::runtime_error("throw_on_move"); + BEMAN_EXECUTION_THROW(std::runtime_error("throw_on_move")); } } throw_on_move(const throw_on_move&) = delete; }; +#ifdef __cpp_exceptions /// Checks that when we throw when storing the completion, we store a `set_error` completion with the current /// exception. auto test_throw_on_move_when_storing() -> void { @@ -219,12 +220,15 @@ auto test_throw_on_move_when_completing() -> void { // Assert ASSERT(executed); } +#endif // __cpp_exceptions TEST(exec_completion_storage) { test_store_trivial_set_value(); test_store_trivial_set_error(); test_store_trivial_set_stopped(); test_store_with_complex_signatures(); +#ifdef __cpp_exceptions test_throw_on_move_when_storing(); test_throw_on_move_when_completing(); +#endif } diff --git a/tests/beman/execution/exec-connect.test.cpp b/tests/beman/execution/exec-connect.test.cpp index e61937103..b9b6d6250 100644 --- a/tests/beman/execution/exec-connect.test.cpp +++ b/tests/beman/execution/exec-connect.test.cpp @@ -220,7 +220,7 @@ auto test_connect_awaitable() -> void { default: return result; case 0: - throw ::std::runtime_error("exception"); + BEMAN_EXECUTION_THROW(::std::runtime_error("exception")); } } }; @@ -243,7 +243,8 @@ auto test_connect_awaitable() -> void { this->iv = value; this->bv = true; } - auto set_error(const ::std::exception_ptr& error) && noexcept -> void { + auto set_error([[maybe_unused]] const ::std::exception_ptr& error) && noexcept -> void { +#ifdef __cpp_exceptions try { std::rethrow_exception(error); } catch (const std::runtime_error&) { @@ -251,6 +252,7 @@ auto test_connect_awaitable() -> void { } catch (...) { this->bv = false; } +#endif } auto set_stopped() && noexcept -> void {} }; @@ -271,6 +273,7 @@ auto test_connect_awaitable() -> void { handle.resume(); ASSERT(iv == 42 && bv == true); } +#ifdef __cpp_exceptions { ::std::coroutine_handle<> handle{}; int result{}; @@ -287,6 +290,7 @@ auto test_connect_awaitable() -> void { handle.resume(); ASSERT(iv == 0 && bv == true); } +#endif // __cpp_exceptions { ::std::coroutine_handle<> handle{}; diff --git a/tests/beman/execution/exec-general.test.cpp b/tests/beman/execution/exec-general.test.cpp index 064ae557c..f350a4c5a 100644 --- a/tests/beman/execution/exec-general.test.cpp +++ b/tests/beman/execution/exec-general.test.cpp @@ -86,6 +86,7 @@ auto test_as_except_ptr() -> void { //-dk:TODO set up death test: test_detail::as_except_ptr(::std::exception_ptr()); static_assert(std::same_as); +#ifdef __cpp_exceptions try { std::rethrow_exception(test_detail::as_except_ptr(error{17})); } catch (error& e) { @@ -105,6 +106,7 @@ auto test_as_except_ptr() -> void { // NOLINTNEXTLINE(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "wrong exception type produced by as_except_ptr for error code"); } +#endif // __cpp_exceptions } } // namespace diff --git a/tests/beman/execution/exec-just.test.cpp b/tests/beman/execution/exec-just.test.cpp index 894d9b14b..9a17d4507 100644 --- a/tests/beman/execution/exec-just.test.cpp +++ b/tests/beman/execution/exec-just.test.cpp @@ -250,13 +250,14 @@ TEST(exec_just) { using type = decltype(test_std::get_completion_signatures>()); static_assert(std::same_as, type>); - try { + BEMAN_EXECUTION_TRY { test_just_constraints(); test_just(); test_just_attrs(); test_completion_signatures(); test_just_allocator(); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTNEXTLINE(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "the just tests shouldn't throw"); } diff --git a/tests/beman/execution/exec-let.test.cpp b/tests/beman/execution/exec-let.test.cpp index ae7d2b5e8..87d76f6e8 100644 --- a/tests/beman/execution/exec-let.test.cpp +++ b/tests/beman/execution/exec-let.test.cpp @@ -206,12 +206,13 @@ TEST(exec_let) { static_assert(std::same_as); static_assert(std::same_as); - try { + BEMAN_EXECUTION_TRY { test_let_value(); test_let_value_allocator(); test_let_value_env(); test_completion_signatures(); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTBEGIN(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "let tests are not expected to throw"); // NOLINTEND(cert-dcl03-c,hicpp-static-assert,misc-static-assert) diff --git a/tests/beman/execution/exec-parallel-scheduler.test.cpp b/tests/beman/execution/exec-parallel-scheduler.test.cpp index e0a021806..f530e9a08 100644 --- a/tests/beman/execution/exec-parallel-scheduler.test.cpp +++ b/tests/beman/execution/exec-parallel-scheduler.test.cpp @@ -267,6 +267,7 @@ auto test_parallel_scheduler_schedule() -> void { } } +#ifdef __cpp_exceptions auto test_parallel_scheduler_exception_propagation() -> void { auto sch = test_std::get_parallel_scheduler(); try { @@ -293,6 +294,7 @@ auto test_parallel_scheduler_exception_propagation() -> void { ASSERT(n == 114514); } } +#endif // __cpp_exceptions } // namespace @@ -310,5 +312,7 @@ TEST(exec_parallel_scheduler) { test_parallel_scheduler_synopsis(); test_replaceability_synopsis(); test_parallel_scheduler_schedule(); +#ifdef __cpp_exceptions test_parallel_scheduler_exception_propagation(); +#endif } diff --git a/tests/beman/execution/exec-spawn-future.test.cpp b/tests/beman/execution/exec-spawn-future.test.cpp index 6d682b031..6fe019b15 100644 --- a/tests/beman/execution/exec-spawn-future.test.cpp +++ b/tests/beman/execution/exec-spawn-future.test.cpp @@ -133,7 +133,7 @@ struct exception { }; struct throws { throws() = default; - throws(throws&&) noexcept(false) { throw exception{42}; } + throws(throws&&) noexcept(false) { BEMAN_EXECUTION_THROW(exception{42}); } }; static_assert(!std::is_nothrow_constructible_v, throws>); @@ -251,6 +251,7 @@ auto test_receiver() { #endif } +#ifdef __cpp_exceptions { using c_t = test_std::completion_signatures; state_base state{}; @@ -275,6 +276,7 @@ auto test_receiver() { } #endif } +#endif // __cpp_exceptions } struct allocator { diff --git a/tests/beman/execution/exec-stopped-err.test.cpp b/tests/beman/execution/exec-stopped-err.test.cpp index 3691514ef..b453a91db 100644 --- a/tests/beman/execution/exec-stopped-err.test.cpp +++ b/tests/beman/execution/exec-stopped-err.test.cpp @@ -38,13 +38,16 @@ auto test_stopped_as_error_signatures() -> void { static_assert( std::same_as>); +#ifdef __cpp_exceptions try { test_std::sync_wait(std::move(sndr2)); } catch (int e) { ASSERT(e == 114514); } +#endif } +#ifdef __cpp_exceptions auto test_stopped_as_std_error_code() -> void { try { auto snd = test_std::stopped_as_error(test::optional_sender{}, @@ -54,6 +57,7 @@ auto test_stopped_as_std_error_code() -> void { ASSERT(e.code() == std::errc::operation_canceled); } } +#endif // __cpp_exceptions auto test_stopped_as_error_attributes() { test::sender_env s{42}; @@ -67,6 +71,8 @@ auto test_stopped_as_error_attributes() { TEST(exec_stopped_as_error) { test_stopped_as_error_signatures(); +#ifdef __cpp_exceptions test_stopped_as_std_error_code(); +#endif test_stopped_as_error_attributes(); } diff --git a/tests/beman/execution/exec-sync-wait.test.cpp b/tests/beman/execution/exec-sync-wait.test.cpp index 9a32d663a..08226f260 100644 --- a/tests/beman/execution/exec-sync-wait.test.cpp +++ b/tests/beman/execution/exec-sync-wait.test.cpp @@ -157,6 +157,7 @@ auto test_sync_wait_receiver() -> void { ASSERT(not state.error); ASSERT(*state.result == (std::tuple{arg<0>{2}, arg<1>{3}, arg<2>{5}})); } +#ifdef __cpp_exceptions { using local_sender = decltype(test_std::just(arg<0>{}, arg<1>{}, arg<2>{})); test_detail::sync_wait_state state{}; @@ -191,6 +192,7 @@ auto test_sync_wait_receiver() -> void { ASSERT(nullptr == "unexpected exception type"); } } +#endif // __cpp_exceptions { using local_sender = decltype(test_std::just(arg<0>{}, arg<1>{}, arg<2>{})); test_detail::sync_wait_state state{}; @@ -203,16 +205,18 @@ auto test_sync_wait_receiver() -> void { } auto test_sync_wait() -> void { - try { + BEMAN_EXECUTION_TRY { auto value{test_std::sync_wait(test_std::just(arg<0>{7}, arg<1>{11}))}; ASSERT(value); ASSERT(*value == (std::tuple{arg<0>{7}, arg<1>{11}})); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTBEGIN(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "no exception expected from sync_wait(just(...)"); // NOLINTEND(cert-dcl03-c,hicpp-static-assert,misc-static-assert) } +#ifdef __cpp_exceptions try { auto value{test_std::sync_wait(send_error{17})}; use(value); @@ -225,10 +229,13 @@ auto test_sync_wait() -> void { ASSERT(nullptr == "no exception expected from sync_wait(just(...)"); } - try { +#endif // __cpp_exceptions + + BEMAN_EXECUTION_TRY { auto value{test_std::sync_wait(send_stopped())}; ASSERT(not value); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTBEGIN(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "no exception expected from sync_wait(just(...)"); // NOLINTEND(cert-dcl03-c,hicpp-static-assert,misc-static-assert) diff --git a/tests/beman/execution/exec-task.test.cpp b/tests/beman/execution/exec-task.test.cpp index b6fec466a..262944bed 100644 --- a/tests/beman/execution/exec-task.test.cpp +++ b/tests/beman/execution/exec-task.test.cpp @@ -55,10 +55,12 @@ auto nested_stopped_task(bool& reached_after_stop) -> test_std::task<> { reached_after_stop = true; } +#ifdef __cpp_exceptions auto throwing_task() -> test_std::task { throw std::runtime_error("task failure"); co_return 0; } +#endif struct integer_error_environment { using error_types = test_std::completion_signatures; @@ -183,6 +185,7 @@ auto test_task_stopped_completion() -> void { ASSERT(!reached_after_stop); } +#ifdef __cpp_exceptions auto test_task_errors() -> void { bool caught{}; try { @@ -204,6 +207,7 @@ auto test_task_errors() -> void { } ASSERT(caught); } +#endif // __cpp_exceptions auto test_task_uses_pmr_allocator_arg() -> void { counting_memory_resource resource; @@ -250,7 +254,9 @@ TEST(exec_task) { test_task_is_lazy_and_completes_with_values(); test_task_awaits_senders_and_tasks(); test_task_stopped_completion(); +#ifdef __cpp_exceptions test_task_errors(); +#endif test_task_uses_pmr_allocator_arg(); test_task_allocator_arg_unrelated_to_allocator_type(); test_task_propagates_stop_token(); diff --git a/tests/beman/execution/exec-when-all.test.cpp b/tests/beman/execution/exec-when-all.test.cpp index facf6ddbf..e4fe2c997 100644 --- a/tests/beman/execution/exec-when-all.test.cpp +++ b/tests/beman/execution/exec-when-all.test.cpp @@ -306,11 +306,12 @@ TEST(exec_when_all) { static_assert(std::same_as); static_assert(std::same_as); - try { + BEMAN_EXECUTION_TRY { test_when_all(); test_when_all_attrs(); test_when_all_with_variant(); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTNEXTLINE(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "the when_all tests shouldn't throw"); } diff --git a/tests/beman/execution/exec-with-awaitable-senders.test.cpp b/tests/beman/execution/exec-with-awaitable-senders.test.cpp index 0130bb32a..4c19e432c 100644 --- a/tests/beman/execution/exec-with-awaitable-senders.test.cpp +++ b/tests/beman/execution/exec-with-awaitable-senders.test.cpp @@ -55,20 +55,16 @@ coroutine test_await_tuple() { coroutine test_await_void() { co_await exec::just(); } void test_sync_wait_awaitable() { - try { + BEMAN_EXECUTION_TRY { auto [v] = exec::sync_wait(awaitable{}).value_or(std::tuple(0)); ASSERT(v == 1); - } catch (...) { - ASSERT_UNREACHABLE(); } + BEMAN_EXECUTION_CATCH(...) { ASSERT_UNREACHABLE(); } } void test_sync_wait_void_awaitable() { - try { - ASSERT(exec::sync_wait(void_awaitable{})); - } catch (...) { - ASSERT_UNREACHABLE(); - } + BEMAN_EXECUTION_TRY { ASSERT(exec::sync_wait(void_awaitable{})); } + BEMAN_EXECUTION_CATCH(...) { ASSERT_UNREACHABLE(); } } coroutine test_mix_awaitable_and_sender() { diff --git a/tests/beman/execution/exec-within.test.cpp b/tests/beman/execution/exec-within.test.cpp index 76fb521b3..93a295ab2 100644 --- a/tests/beman/execution/exec-within.test.cpp +++ b/tests/beman/execution/exec-within.test.cpp @@ -192,6 +192,7 @@ auto test_work_completes_with_stopped() -> void { } /// Checks that if the enter scope completes with error, the work isn't executed and the error is propagated. +#ifdef __cpp_exceptions auto test_scope_enter_completes_with_error() -> void { // Arrange @@ -222,6 +223,7 @@ auto test_scope_enter_completes_with_error() -> void { ASSERT(!work_executed); ASSERT(!exit_executed); } +#endif // __cpp_exceptions /// Sender that can complete with value or stopped, but always completes with stopped. struct sender_completing_with_stopped { @@ -272,6 +274,8 @@ TEST(exec_within) { test_work_with_multiple_values_alternatives(); test_work_completes_with_error(); test_work_completes_with_stopped(); +#ifdef __cpp_exceptions test_scope_enter_completes_with_error(); +#endif test_scope_enter_completes_with_stopped(); } diff --git a/tests/beman/execution/stopsource-cons.test.cpp b/tests/beman/execution/stopsource-cons.test.cpp index 9649948a7..a01d09f51 100644 --- a/tests/beman/execution/stopsource-cons.test.cpp +++ b/tests/beman/execution/stopsource-cons.test.cpp @@ -24,21 +24,29 @@ bool fail{}; } // NOLINTNEXTLINE(hicpp-no-malloc) -auto operator new(::std::size_t size) -> void* { return fail ? throw ::std::bad_alloc() : ::std::malloc(size); } +auto operator new(::std::size_t size) -> void* { +#ifdef __cpp_exceptions + return fail ? throw ::std::bad_alloc() : ::std::malloc(size); +#else + return fail ? nullptr : ::std::malloc(size); +#endif +} auto operator delete(void* ptr) noexcept -> void { ::std::free(ptr); } // NOLINT(hicpp-no-malloc) auto operator delete(void* ptr, ::std::size_t) noexcept -> void { ::std::free(ptr); } // NOLINT(hicpp-no-malloc) TEST(stopsource_cons) { // Reference: [stopsource.cons] p1 - try { + BEMAN_EXECUTION_TRY { ::test_std::stop_source source; ASSERT(source.stop_possible()); ASSERT((not source.stop_requested())); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { ASSERT(nullptr == "can't be reached"); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) } +#ifdef __cpp_exceptions // Reference: [stopsource.cons] p3 fail = true; try { @@ -50,4 +58,5 @@ TEST(stopsource_cons) { } catch (...) { ASSERT(nullptr == "can't be reached"); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) } +#endif } diff --git a/tests/beman/execution/stopsource-general.test.cpp b/tests/beman/execution/stopsource-general.test.cpp index 165a910be..885c53b2a 100644 --- a/tests/beman/execution/stopsource-general.test.cpp +++ b/tests/beman/execution/stopsource-general.test.cpp @@ -9,17 +9,18 @@ import beman.execution; #endif TEST(stopsource_general) { - try { + BEMAN_EXECUTION_TRY { // Reference: [stopsource.general] p1. test::stop_source([] { return ::test_std::stop_source(); }); ::test_std::stop_source source1; ::test_std::stop_source source2{::test_std::nostopstate}; static_assert(noexcept(::test_std::stop_source(::test_std::nostopstate_t()))); - try { + BEMAN_EXECUTION_TRY { ASSERT(not source2.stop_possible()); ASSERT(not source2.stop_requested()); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTNEXTLINE(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "the stop source should not throw"); } @@ -28,7 +29,8 @@ TEST(stopsource_general) { static_assert(noexcept(source1.stop_requested())); static_assert(noexcept(source1.stop_possible())); static_assert(noexcept(source1.request_stop())); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTNEXTLINE(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "stop source tests shouldn't throw"); } diff --git a/tests/beman/execution/stopsource-mem.test.cpp b/tests/beman/execution/stopsource-mem.test.cpp index ce622c4be..55bb784ca 100644 --- a/tests/beman/execution/stopsource-mem.test.cpp +++ b/tests/beman/execution/stopsource-mem.test.cpp @@ -132,14 +132,14 @@ auto test_stopsource_request_stop() -> void { } // namespace TEST(stopsource_mem) { - try { + BEMAN_EXECUTION_TRY { test_stopsource_swap(); test_stopsource_get_token(); test_stopsource_stop_possible(); test_stopsource_stop_requested(); test_stopsource_request_stop(); - - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTNEXTLINE(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "the stop source tests shouldn't throw"); } diff --git a/tests/beman/execution/stoptoken-mem.test.cpp b/tests/beman/execution/stoptoken-mem.test.cpp index 566cc4ee3..40fe19166 100644 --- a/tests/beman/execution/stoptoken-mem.test.cpp +++ b/tests/beman/execution/stoptoken-mem.test.cpp @@ -100,11 +100,12 @@ auto test_stop_token_stop_possible() -> void { TEST(stoptoken_mem) { static_assert(::test_std::stoppable_token<::test_std::stop_token>); - try { + BEMAN_EXECUTION_TRY { test_stop_token_swap(); test_stop_token_stop_requested(); test_stop_token_stop_possible(); - } catch (...) { + } + BEMAN_EXECUTION_CATCH(...) { // NOLINTBEGIN(cert-dcl03-c,hicpp-static-assert,misc-static-assert) ASSERT(nullptr == "the stop token tests are not expected to throw"); // NOLINTEND(cert-dcl03-c,hicpp-static-assert,misc-static-assert) From 359a522414cedf666be9f2b31e87895a8296aadf Mon Sep 17 00:00:00 2001 From: Cra3z Date: Tue, 17 Nov 2026 15:35:04 +0800 Subject: [PATCH 2/2] Support -fno-rtti --- examples/inspect.cpp | 4 + examples/meta.hpp | 124 ------------------ include/beman/execution/detail/task.hpp | 6 +- .../beman/execution/detail/task_scheduler.hpp | 27 +++- include/beman/execution/detail/type_id.hpp | 75 +++++++++++ src/beman/execution/CMakeLists.txt | 2 + src/beman/execution/type_id.cppm | 12 ++ 7 files changed, 121 insertions(+), 129 deletions(-) delete mode 100644 examples/meta.hpp create mode 100644 include/beman/execution/detail/type_id.hpp create mode 100644 src/beman/execution/type_id.cppm diff --git a/examples/inspect.cpp b/examples/inspect.cpp index fafbe4060..8cec64486 100644 --- a/examples/inspect.cpp +++ b/examples/inspect.cpp @@ -23,6 +23,7 @@ namespace ex = beman::execution; // ---------------------------------------------------------------------------- +#ifdef __cpp_rtti namespace meta { // The code in this namespace is a fairly basic way to print types. It can // almost certainly be done better, in particular using reflection. However, @@ -186,10 +187,12 @@ struct logger_t { inline constexpr logger_t logger{}; } // namespace +#endif // ---------------------------------------------------------------------------- int main() { +#ifdef __cpp_rtti auto log = [](std::string_view name) { return [name](std::string_view msg) { std::cout << name << " message='" << msg << "'\n"; }; }; @@ -201,4 +204,5 @@ int main() { ex::sync_wait(logger(ex::just(0, 1, 2), log("just(0, 1, 2)"))); ex::sync_wait(logger(ex::just_error(0), log("just_error(0)")) | ex::upon_error([](auto) {})); ex::sync_wait(logger(ex::just_stopped(), log("just_stopped()")) | ex::upon_stopped([]() {})); +#endif } diff --git a/examples/meta.hpp b/examples/meta.hpp deleted file mode 100644 index 3f6a04ca1..000000000 --- a/examples/meta.hpp +++ /dev/null @@ -1,124 +0,0 @@ -// examples/meta.hpp -*-C++-*- -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#ifndef INCLUDED_EXAMPLES_META -#define INCLUDED_EXAMPLES_META - -// ---------------------------------------------------------------------------- - -namespace meta { -// The code in this namespace is a fairly basic way to print types. It can -// almost certainly be done better, in particular using reflection. However, -// that's beside the point of this example. The important part for the -// example is that meta::type::name() yields a string representing the -// type T in some reasonable way -template