diff --git a/MAINTAINERS.md b/MAINTAINERS.md index 4e6dd390e..da4a64f4e 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -449,7 +449,7 @@ algorithms need only `__complete`, and perhaps `__start`. | `__start(state, children)` | starts every child with `children.__start_all()` | the algorithm completes from `start`, starts only some of its children, or starts a nested operation | `just`, `let_value`, `repeat_n` | | `__get_state(sndr, rcvr)` | a `__basic_state` that holds the receiver and the sender's data | the state holds more than the data (nested operations, saved results, a stop source), or the algorithm rejects its inputs by returning an `__mexception` | `when_all`, `continues_on`, `let_value` | | `__get_state(sndr, rcvr, __subscribing_t)` | the same as the above | the state of a subscribed (sequence) sender differs from that of a connected one | | -| `__state_type` (opt-in) | the type that `__get_state` returns | the state holds nested operations. The walk then gets the state's type without asking whether making the state can throw, which would instantiate the operations below it. It must name the type that `__get_state` returns, or its `__mexception` | `let_value`, `__sequence`, `exec::ignore_all_values` | +| `__state_type` (opt-in) | the type that `__get_state` returns | the state holds nested operations. The walk then gets the state's type without asking whether making the state can throw, which would instantiate the operations below it. It must name the type that `__get_state` returns, or its `__mexception`. A state whose receiver is an archetype (see `__facade::__walking_receiver`) is for the walk, or for asking whether connecting can throw: it holds placeholders for its operations (see `__facade::__state_op_t`) and does not connect them, but whether connecting them can throw is still asked of the real operations | `let_value`, `__sequence`, `exec::ignore_all_values` | | `__connects_children` | `true` | the algorithm connects a child itself, in its state: to connect it later, to start it from a nested operation, or to destroy its operation early | `__sequence`, `starts_on`, `let_value` | | `__connect(key, state, sndr, rcvr)` | `STDEXEC::connect(sndr, rcvr)` | a child or nested operation must be connected to a different kind of receiver, as when a domain adapts a child whose operation completes from a kernel | nvexec | | `__receiver_base` | an empty struct | a domain's algorithm needs the facade's receivers to have a base class, so that its senders complete them in the way they expect | nvexec | diff --git a/include/exec/detail/repeat.hpp b/include/exec/detail/repeat.hpp index 1a4a8afa7..36459ddcf 100644 --- a/include/exec/detail/repeat.hpp +++ b/include/exec/detail/repeat.hpp @@ -92,8 +92,11 @@ namespace experimental::execution::__repeat using __child_t = __repeat::__child_t<_Sender>; using __body_rcvr_t = __body_receiver_t<_Algo, _Sender, _Receiver>; using __bounce_rcvr_t = __facade::__receiver_t<_Algo, __bounce, __state, _Sender, _Receiver>; - using __body_op_t = connect_result_t<__child_t, __body_rcvr_t>; - using __bounce_op_t = connect_result_t<__bounce_sender_t, __bounce_rcvr_t>; + // (A walk's state holds a placeholder for the body's operation: see + // __facade::__state_op_t. The trampoline's operation is the same in every walk.) + using __body_op_t = + __facade::__state_op_t<_Receiver, connect_result_t, __child_t, __body_rcvr_t>; + using __bounce_op_t = connect_result_t<__bounce_sender_t, __bounce_rcvr_t>; static constexpr bool __nothrow_connect = __nothrow_constructible_from<__child_t, __child_t &> && __nothrow_connectable<__child_t, __body_rcvr_t>; @@ -118,10 +121,19 @@ namespace experimental::execution::__repeat // Connects the body of the next iteration, destroying the operation of the last one. // (This happens in the last one's completion. Operations that complete must not touch - // themselves afterward.) + // themselves afterward.) If the state is for a walk, __body_op_ is a placeholder. constexpr auto __connect_body() noexcept(__nothrow_connect) -> __body_op_t & { - return __body_op_.__emplace_from(STDEXEC::connect, __child_t(__child_), __body_rcvr_t{this}); + if constexpr (__facade::__walking_receiver<_Receiver>) + { + return __body_op_.emplace(); + } + else + { + return __body_op_.__emplace_from(STDEXEC::connect, + __child_t(__child_), + __body_rcvr_t{this}); + } } // Destroys the operations, before the operation completes @@ -147,6 +159,41 @@ namespace experimental::execution::__repeat return true; } + // The type that __get_state returns: the state, or the type error that it reports. + // (Classes keyed by bools only, so that the debug information, which names a class when + // it names the state's type, does not repeat the sender's and the receiver's types.) + template + struct __state_or_error + { + template + using __f = __state<_Algo, _Sender, _Receiver>; + }; + + template + struct __state_or_error + { + template + using __f = __mexception<_WHAT_(_INVALID_ARGUMENT_), + _WHERE_(_IN_ALGORITHM_, typename __decay_t<_Sender>::__tag_t), + _WHY_(_THE_INPUT_SENDER_MUST_BE_COPYABLE_), + _WITH_SENDER_<__child_t<_Sender>>>; + }; + + template <> + struct __state_or_error + { + template + using __f = __mexception<_WHAT_(_INVALID_ARGUMENT_), + _WHERE_(_IN_ALGORITHM_, typename __decay_t<_Sender>::__tag_t), + _WHY_(_THE_SENDER_CANNOT_BE_CONNECTED_), + _WITH_SENDER_<__child_t<_Sender>>>; + }; + + template + using __state_type_t = __state_or_error< + __std::copy_constructible<__child_t<_Sender>>, + __connectable<_Algo, _Sender, _Receiver>>::template __f<_Algo, _Sender, _Receiver>; + // The base of the repeating algorithms. _Derived is the algorithm, which says what the // body's value completions mean (with a __complete(__body, ...) handler), and how the // operation starts (with __start, which calls __start_iteration). Its static member @@ -202,29 +249,24 @@ namespace experimental::execution::__repeat return STDEXEC::schedule(trampoline_scheduler{}); } + // (See __facade::__state_type_t.) + template + using __state_type = __repeat::__state_type_t<_Derived, _Sender, _Receiver>; + + // The state, or the type error that it reports (see __state_or_error) template static constexpr auto __get_state(_Sender &&__sndr, _Receiver __rcvr) noexcept(__repeat::__nothrow_state<_Derived, _Sender, _Receiver>()) + -> __state_type<_Sender, _Receiver> { - using __tag_t = __decay_t<_Sender>::__tag_t; - if constexpr (!__std::copy_constructible<__child_t<_Sender>>) + if constexpr (__connectable<_Derived, _Sender, _Receiver>) { - return __mexception<_WHAT_(_INVALID_ARGUMENT_), - _WHERE_(_IN_ALGORITHM_, __tag_t), - _WHY_(_THE_INPUT_SENDER_MUST_BE_COPYABLE_), - _WITH_SENDER_<__child_t<_Sender>>>(); - } - else if constexpr (!__connectable<_Derived, _Sender, _Receiver>) - { - return __mexception<_WHAT_(_INVALID_ARGUMENT_), - _WHERE_(_IN_ALGORITHM_, __tag_t), - _WHY_(_THE_SENDER_CANNOT_BE_CONNECTED_), - _WITH_SENDER_<__child_t<_Sender>>>(); + return __state<_Derived, _Sender, _Receiver>(static_cast<_Sender &&>(__sndr), + static_cast<_Receiver &&>(__rcvr)); } else { - return __state<_Derived, _Sender, _Receiver>(static_cast<_Sender &&>(__sndr), - static_cast<_Receiver &&>(__rcvr)); + return __state_type<_Sender, _Receiver>(); } } diff --git a/include/stdexec/__detail/__associate.hpp b/include/stdexec/__detail/__associate.hpp index a053e5a21..fef951811 100644 --- a/include/stdexec/__detail/__associate.hpp +++ b/include/stdexec/__detail/__associate.hpp @@ -196,18 +196,24 @@ namespace STDEXEC using __wrap_sender_t = __associate_data_t::__wrap_sender_t; using __wrapped_rcvr_t = __facade::__receiver_t<__algo, __wrapped_op, __state, _Sender, _Receiver>; - using __op_t = connect_result_t<__wrap_sender_t, __wrapped_rcvr_t>; + // (A walk's state holds a placeholder for the operation: see __facade::__state_op_t.) + using __op_t = + __facade::__state_op_t<_Receiver, connect_result_t, __wrap_sender_t, __wrapped_rcvr_t>; - // The wrapped sender's operation is connected only if the association succeeded. + // The wrapped sender's operation is connected only if the association succeeded, and + // the state is not for a walk. explicit __state(std::pair<__assoc_t, __sender_ref_t> __parts, _Receiver&& __rcvr) : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __assoc_(STDEXEC::__allocator_aware_forward(std::move(__parts.first), __rcvr_)) { - if (__assoc_) + if constexpr (!__facade::__walking_receiver<_Receiver>) { - __op_.__emplace_from(STDEXEC::connect, - std::move(*__parts.second), - __wrapped_rcvr_t{this}); + if (__assoc_) + { + __op_.__emplace_from(STDEXEC::connect, + std::move(*__parts.second), + __wrapped_rcvr_t{this}); + } } } @@ -244,6 +250,10 @@ namespace STDEXEC return {}; } + // (See __facade::__state_type_t.) + template + using __state_type = __state<_Sender, _Receiver>; + template static constexpr auto __get_state(_Sender&& __sndr, _Receiver __rcvr) noexcept((!std::is_lvalue_reference_v<_Sender> diff --git a/include/stdexec/__detail/__finally.hpp b/include/stdexec/__detail/__finally.hpp index 97da27d07..75f158b08 100644 --- a/include/stdexec/__detail/__finally.hpp +++ b/include/stdexec/__detail/__finally.hpp @@ -113,8 +113,11 @@ namespace STDEXEC __facade::__receiver_t<__algo, __facade::__child<0>, __state, _Sender, _Receiver>; using __final_rcvr_t = __facade::__receiver_t<__algo, __facade::__child<1>, __state, _Sender, _Receiver>; - using __initial_op_t = connect_result_t<__initial_t, __initial_rcvr_t>; - using __final_op_t = connect_result_t<__final_t, __final_rcvr_t>; + // (A walk's state holds placeholders for the operations: see __facade::__state_op_t.) + using __initial_op_t = + __facade::__state_op_t<_Receiver, connect_result_t, __initial_t, __initial_rcvr_t>; + using __final_op_t = + __facade::__state_op_t<_Receiver, connect_result_t, __final_t, __final_rcvr_t>; static constexpr bool __saves_results = __facade::__completions_of_t<__algo, __facade::__child<1>, _Sender, _Receiver>::__count( @@ -131,12 +134,14 @@ namespace STDEXEC && __nothrow_connectable<__initial_t, __initial_rcvr_t>) : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __env2_(__trnsfr::__mk_env2(__facade::__get_child<0>(__sndr), STDEXEC::get_env(__rcvr_))) - , __final_op_(STDEXEC::connect(__facade::__get_child<1>(static_cast<_Sender&&>(__sndr)), - __final_rcvr_t{this})) + , __final_op_(__state::__connect_final(static_cast<_Sender&&>(__sndr), this)) { - __initial_op_.__emplace_from(STDEXEC::connect, - __facade::__get_child<0>(static_cast<_Sender&&>(__sndr)), - __initial_rcvr_t{this}); + if constexpr (!__facade::__walking_receiver<_Receiver>) + { + __initial_op_.__emplace_from(STDEXEC::connect, + __facade::__get_child<0>(static_cast<_Sender&&>(__sndr)), + __initial_rcvr_t{this}); + } } STDEXEC_IMMOVABLE(__state); @@ -146,6 +151,23 @@ namespace STDEXEC __storage_t __storage_{}; __final_op_t __final_op_; __optional<__initial_op_t> __initial_op_{}; + + private: + // Connects the final sender, unless the state is for a walk: then __final_op_ is a + // placeholder. + static constexpr auto __connect_final(_Sender&& __sndr, __state* __self) + noexcept(__nothrow_connectable<__final_t, __final_rcvr_t>) -> __final_op_t + { + if constexpr (__facade::__walking_receiver<_Receiver>) + { + return {}; + } + else + { + return STDEXEC::connect(__facade::__get_child<1>(static_cast<_Sender&&>(__sndr)), + __final_rcvr_t{__self}); + } + } }; // __finally_(initial, final): when the initial sender completes, its results are saved, @@ -187,6 +209,10 @@ namespace STDEXEC __hide_scheduler{STDEXEC::get_env(__state.__rcvr_)}); } + // (See __facade::__state_type_t.) + template + using __state_type = __state<_Sender, _Receiver>; + template STDEXEC_ATTRIBUTE(host, device) static constexpr auto __get_state(_Sender&& __sndr, _Receiver __rcvr) diff --git a/include/stdexec/__detail/__starts_on.hpp b/include/stdexec/__detail/__starts_on.hpp index 0f4a8ec83..e1b8410db 100644 --- a/include/stdexec/__detail/__starts_on.hpp +++ b/include/stdexec/__detail/__starts_on.hpp @@ -47,6 +47,27 @@ namespace STDEXEC struct __algo; + // The type of the child's operation: the one that the state attaches, or, if the state is + // for a walk (its receiver is an archetype), a placeholder, since the walk needs only the + // state's type. Naming the operation there would instantiate the operations below it, + // connected to archetypes. (A class keyed by a bool only, so that the debug information, + // which names the class when it names the operation's type, does not repeat the sender's + // and the receiver's types.) + template + struct __child_op_of + { + template + using __f = + __facade::__child_op_t<__algo, _Sender, 0, _State, _Child, _Rcvr, _Subscribing...>; + }; + + template <> + struct __child_op_of + { + template + using __f = __facade::__walk_op; + }; + // The state of a starts_on. If the starts_on is subscribed (_Subscribing is // __facade::__subscribing_t), so is its child, when that is a sequence sender (see // __facade::__attaches_child). @@ -63,8 +84,9 @@ namespace STDEXEC __facade::__receiver_t<__algo, __sched_op, __state, _Sender, _Receiver>; using __child_rcvr_t = __facade::__receiver_t<__algo, __facade::__child<0>, __state, _Sender, _Receiver>; - using __child_op_t = __facade:: - __child_op_t<__algo, _Sender, 0, __state, __child_t, __child_rcvr_t, _Subscribing...>; + using __child_op_of_t = __child_op_of<__facade::__walking_receiver<_Receiver>>; + using __child_op_t = + __child_op_of_t::template __f<_Sender, __state, __child_t, __child_rcvr_t, _Subscribing...>; static constexpr bool __nothrow_v = __nothrow_callable && __nothrow_connectable<__sched_sndr_t, __sched_rcvr_t> @@ -94,11 +116,16 @@ namespace STDEXEC __child_op_t __child_op_; private: - // Connects the child, or attaches it (see __facade::__attaches_child) + // Connects the child, or attaches it (see __facade::__attaches_child), unless the state + // is for a walk: then __child_op_ is a placeholder. STDEXEC_ATTRIBUTE(always_inline) constexpr auto __attach_child(__child_t&& __child) noexcept(__nothrow_v) -> __child_op_t { - if constexpr (__facade::__attaches_child<__algo, _Sender, 0>) + if constexpr (__facade::__walking_receiver<_Receiver>) + { + return {}; + } + else if constexpr (__facade::__attaches_child<__algo, _Sender, 0>) { using __attach_t = __facade::__attach_child_t<__algo, _Sender, 0, __child_rcvr_t, _Subscribing...>; @@ -181,6 +208,10 @@ namespace STDEXEC __facade::__get_data(__sndr)}; } + // (See __facade::__state_type_t.) + template + using __state_type = __state<_Sender, _Receiver, _Subscribing...>; + // (The state of a subscribed starts_on takes __facade::__subscribing_t.) template STDEXEC_ATTRIBUTE(host, device) diff --git a/test/stdexec/algos/adaptors/test_associate.cpp b/test/stdexec/algos/adaptors/test_associate.cpp index 70f0d005a..1aa4e9854 100644 --- a/test/stdexec/algos/adaptors/test_associate.cpp +++ b/test/stdexec/algos/adaptors/test_associate.cpp @@ -25,7 +25,9 @@ import std; #else # include +# include # include +# include #endif namespace ex = STDEXEC; @@ -305,6 +307,38 @@ namespace (void) snd; } +#if !STDEXEC_NO_STDCPP_EXCEPTIONS() + struct throws_on_connect + { + using sender_concept = ex::sender_tag; + using completion_signatures = ex::completion_signatures; + + template + auto connect(Receiver) const -> ex::connect_result_t + { + throw std::logic_error("connect"); + } + }; + + TEST_CASE("associate - connecting can throw if connecting the wrapped sender can", + "[adaptors][associate]") + { + // (The function is noexcept, so the exception can come only from connecting.) + auto fn = []() noexcept + { + return ex::associate(throws_on_connect{}, null_token{}); + }; + using Sender = decltype(fn()); + STATIC_REQUIRE(!ex::__nothrow_connectable>>); + + // So an algorithm that connects it when another sender completes reports the exception. + using Let = decltype(ex::let_value(ex::just(), fn)); + STATIC_REQUIRE(ex::__mapply, + ex::completion_signatures_of_t>>::value); + CHECK_THROWS_AS(ex::sync_wait(ex::let_value(ex::just(), fn)), std::logic_error); + } +#endif // !STDEXEC_NO_STDCPP_EXCEPTIONS() + // TODO: check the pass-through nature of __sync_attrs // TODO: check the pass-through stop request behaviour // TODO: confirm timing of destruction of opstate relative to release of diff --git a/test/stdexec/algos/adaptors/test_finally.cpp b/test/stdexec/algos/adaptors/test_finally.cpp index ad4109930..da46c8b9d 100644 --- a/test/stdexec/algos/adaptors/test_finally.cpp +++ b/test/stdexec/algos/adaptors/test_finally.cpp @@ -105,6 +105,41 @@ namespace CHECK_THROWS_AS(sync_wait(std::move(s)), int); CHECK(called); } + + struct throws_on_connect + { + using sender_concept = sender_tag; + using completion_signatures = STDEXEC::completion_signatures; + + template + auto connect(Receiver) const -> connect_result_t + { + throw 42; + } + }; + + TEST_CASE("finally - connecting can throw if connecting either sender can", "[adaptors][finally]") + { + // (The functions are noexcept, so the exception can come only from connecting.) + auto initial_throws = []() noexcept + { + return exec::finally(throws_on_connect{}, just()); + }; + auto final_throws = []() noexcept + { + return exec::finally(just(), throws_on_connect{}); + }; + using Rcvr = __receiver_archetype>; + STATIC_REQUIRE(!__nothrow_connectable); + STATIC_REQUIRE(!__nothrow_connectable); + + // So an algorithm that connects it when another sender completes reports the exception. + using Let = decltype(let_value(just(), final_throws)); + STATIC_REQUIRE(__mapply<__mcontains, + completion_signatures_of_t>>::value); + CHECK_THROWS_AS(sync_wait(let_value(just(), initial_throws)), int); + CHECK_THROWS_AS(sync_wait(let_value(just(), final_throws)), int); + } #endif // !STDEXEC_NO_STDCPP_EXCEPTIONS() TEST_CASE("finally completes where the final action completes", "[adaptors][finally]") diff --git a/test/stdexec/algos/adaptors/test_starts_on.cpp b/test/stdexec/algos/adaptors/test_starts_on.cpp index 4d3bce363..30acd1259 100644 --- a/test/stdexec/algos/adaptors/test_starts_on.cpp +++ b/test/stdexec/algos/adaptors/test_starts_on.cpp @@ -29,6 +29,8 @@ import std; #else # include // IWYU pragma: keep for std::chrono_literals +# include +# include #endif namespace ex = STDEXEC; @@ -524,6 +526,37 @@ namespace auto domain = ex::get_env(error).query(ex::get_completion_domain); static_assert(std::same_as); } + +#if !STDEXEC_NO_STDCPP_EXCEPTIONS() + struct throws_on_connect + { + using sender_concept = ex::sender_tag; + using completion_signatures = ex::completion_signatures; + + template + auto connect(Receiver) const -> ex::connect_result_t + { + throw std::logic_error("connect"); + } + }; + + TEST_CASE("starts_on - connecting can throw if connecting the child can", "[adaptors][starts_on]") + { + // (The function is noexcept, so the exception can come only from connecting.) + auto fn = []() noexcept + { + return ex::starts_on(inline_scheduler{}, throws_on_connect{}); + }; + using Sender = decltype(fn()); + STATIC_REQUIRE(!ex::__nothrow_connectable>>); + + // So an algorithm that connects it when another sender completes reports the exception. + using Let = decltype(ex::let_value(ex::just(), fn)); + STATIC_REQUIRE(ex::__mapply, + ex::completion_signatures_of_t>>::value); + CHECK_THROWS_AS(ex::sync_wait(ex::let_value(ex::just(), fn)), std::logic_error); + } +#endif // !STDEXEC_NO_STDCPP_EXCEPTIONS() } // namespace STDEXEC_PRAGMA_POP()