diff --git a/MAINTAINERS.md b/MAINTAINERS.md index 22226c90e..4e6dd390e 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -449,6 +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` | | `__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/fork_join.hpp b/include/exec/fork_join.hpp index 21f4a1938..1144c8db4 100644 --- a/include/exec/fork_join.hpp +++ b/include/exec/fork_join.hpp @@ -155,26 +155,59 @@ namespace experimental::execution STDEXEC::__facade::__nth_child_of_t, STDEXEC::__fwd_env_t...>; - // The operation of a sender that cannot be connected to the receiver it is given (in the - // walk, where the receiver is an archetype, when the closures' senders depend on the - // environment). The walk asks the sender for its completions, and reports its error. + // The operation of a sender that cannot be connected to the receiver it is given. (The + // walk asks the sender for its completions, and reports its error.) struct _no_op {}; - template + // The type of an operation that the state holds: the operation, or _no_op, 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 bools 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 _op_of { template using __f = STDEXEC::connect_result_t; }; + template + struct _op_of + { + template + using __f = STDEXEC::__facade::__walk_op; + }; + template <> - struct _op_of + struct _op_of { template using __f = _no_op; }; + // Can the operations be connected? A walk does not ask: its state holds placeholders. + template + struct _connectable_of + { + template + static consteval auto __f() noexcept -> bool + { + return STDEXEC::sender_to && STDEXEC::sender_to; + } + }; + + template <> + struct _connectable_of + { + template + static consteval auto __f() noexcept -> bool + { + return true; + } + }; + template struct _state { @@ -196,10 +229,13 @@ namespace experimental::execution static constexpr bool _child_stays_put = STDEXEC::__trnsfr::__child_stays_put_v<_child_t, STDEXEC::env_of_t>; - static constexpr bool _connectable = STDEXEC::sender_to<_child_t, _child_rcvr_t> - && STDEXEC::sender_to<_when_all_sndr_t, _fork_rcvr_t>; - using _child_op_t = _op_of<_connectable>::template __f<_child_t, _child_rcvr_t>; - using _fork_op_t = _op_of<_connectable>::template __f<_when_all_sndr_t, _fork_rcvr_t>; + // (A walk's state holds placeholders: see _op_of.) + using _connectable_of_t = _connectable_of>; + static constexpr bool _connectable = + _connectable_of_t::template __f<_child_t, _child_rcvr_t, _when_all_sndr_t, _fork_rcvr_t>(); + using _op_of_t = _op_of, _connectable>; + using _child_op_t = _op_of_t::template __f<_child_t, _child_rcvr_t>; + using _fork_op_t = _op_of_t::template __f<_when_all_sndr_t, _fork_rcvr_t>; static constexpr bool _nothrow_v = STDEXEC::__nothrow_applicable<_mk_when_all_fn, _closures_t, _cache_sndr_t> @@ -215,7 +251,7 @@ namespace experimental::execution STDEXEC::get_env(__rcvr_))) , _fork_op_(_state::_connect_fork(static_cast(sndr), this)) { - if constexpr (_connectable) + if constexpr (_connectable && !STDEXEC::__facade::__walking_receiver) { _child_op_.__emplace_from(STDEXEC::connect, STDEXEC::__facade::__get_child<0>(static_cast(sndr)), @@ -236,7 +272,7 @@ namespace experimental::execution static constexpr auto _connect_fork(Sndr&& sndr, _state* self) noexcept(_nothrow_v) -> _fork_op_t { - if constexpr (_connectable) + if constexpr (_connectable && !STDEXEC::__facade::__walking_receiver) { return STDEXEC::connect(STDEXEC::__apply(_mk_when_all_fn{}, STDEXEC::__facade::__get_data( @@ -251,6 +287,42 @@ namespace experimental::execution } }; + // The type that __get_state returns: the state, or the type error that it reports. + // (Classes keyed by bools only, as _op_of is.) + template + struct _state_if_copyable + { + template + using __f = _state; + }; + + template <> + struct _state_if_copyable + { + template + using __f = STDEXEC::__mexception; + }; + + template + struct _state_if_valid + { + template + using __f = _state_if_copyable::_child_completions_t>::value>::template __f; + }; + + template <> + struct _state_if_valid + { + template + using __f = + STDEXEC::__mexception>, + STDEXEC::_WITH_ENVIRONMENT_(STDEXEC::env_of_t)>; + }; + // fork_join(sndr, closures...): when the child completes, its results are cached, its // operation is destroyed, and when_all(closures(cache)...) is started, where cache is a // sender of the cached results (by const reference). Its completions are the operation's. @@ -366,6 +438,13 @@ namespace experimental::execution return true; } + // (See __facade::__state_type_t.) + template + using __state_type = + _state_if_valid>>::template __f; + template STDEXEC_ATTRIBUTE(host, device) static constexpr auto __get_state(Sndr&& sndr, Rcvr rcvr) diff --git a/include/exec/sequence/ignore_all_values.hpp b/include/exec/sequence/ignore_all_values.hpp index 83125b32e..949153aa4 100644 --- a/include/exec/sequence/ignore_all_values.hpp +++ b/include/exec/sequence/ignore_all_values.hpp @@ -223,6 +223,10 @@ namespace experimental::execution return {}; } + // (See __facade::__state_type_t.) + template + using __state_type = __state<_Sender, _Receiver>; + template static constexpr auto __get_state(_Sender&& __sndr, _Receiver __rcvr) noexcept(__nothrow_constructible_from<__state<_Sender, _Receiver>, _Sender, _Receiver>) @@ -306,12 +310,6 @@ namespace experimental::execution // saved result. (The receiver's type does not name the operation's sender, as the state's // does: the types of the items' operations name it, and they would grow with the size of // the sender, as would the debug info that names them.) - template - struct __nothrow_subscribe_t - { - static constexpr bool value = __nothrow_subscribable<_Child, _Rcvr>; - }; - template struct __state_base : __result_type<_Variant> { @@ -332,8 +330,8 @@ namespace experimental::execution using __base_t = __state_base<_Receiver, __result_variant_t<__child_t, env_of_t<_Receiver>>>; using __seq_rcvr_t = __ignore_all_values::__receiver<__base_t>; - explicit __state(_Sender&& __sndr, _Receiver&& __rcvr) noexcept( - __facade::__nothrow_state_v<_Receiver, __nothrow_subscribe_t<__child_t, __seq_rcvr_t>>) + explicit __state(_Sender&& __sndr, _Receiver&& __rcvr) + noexcept(__nothrow_subscribable<__child_t, __seq_rcvr_t>) : __base_t{{}, static_cast<_Receiver&&>(__rcvr)} , __op_{__subscribe_child(static_cast<_Sender&&>(__sndr), this)} {} diff --git a/include/stdexec/__detail/__let.hpp b/include/stdexec/__detail/__let.hpp index a781c7beb..223d2ac7c 100644 --- a/include/stdexec/__detail/__let.hpp +++ b/include/stdexec/__detail/__let.hpp @@ -166,9 +166,20 @@ namespace STDEXEC struct __types<__mlist<_Ts...>> { using __tuple_t = __tuple<_Ts...>; - using __op_t = __result_op_of<__minvoke_or_q<__invoke_result_t, void, __fn_t, _Ts&...>, - __receiver2_t<_Ts...>, - _Ts...>::__t; + }; + + // The operation of the sender that the function returns when it is called with the + // results in _List. (Kept apart from __types, which the results' storage names, so that + // naming the storage does not name the operations: see __ops_lazy.) + template + struct __result_op_for; + + template + struct __result_op_for<__mlist<_Ts...>> + { + using __t = __result_op_of<__minvoke_or_q<__invoke_result_t, void, __fn_t, _Ts&...>, + __receiver2_t<_Ts...>, + _Ts...>::__t; }; // The child is connected with the state. The operation of the sender that the @@ -182,8 +193,13 @@ namespace STDEXEC struct __variants { using __args_t = __variant::__tuple_t...>; - using __ops_t = __variant, - typename __types<_Lists>::__op_t...>; + }; + + template + struct __ops_variant + { + using __t = __variant, + typename __result_op_for<_Lists>::__t...>; }; using __variants_t = __mapply_q<__variants, __args_lists_t>; @@ -192,11 +208,15 @@ namespace STDEXEC // and of the senders that the function returns: they would be connected to archetypes.) struct __ops_lazy { - using __t = __variants_t::__ops_t; + using __t = __mapply_q<__ops_variant, __args_lists_t>::__t; }; using __ops_t = __facade::__lazy_state_ops_t<_Receiver, __ops_lazy, __variant<__facade::__walk_op>>; - static constexpr bool __walking = __facade::__walking_v<_Receiver>; + + static consteval auto __walking() noexcept -> bool + { + return __facade::__walking_receiver<_Receiver>; + } static constexpr bool __nothrow_v = __nothrow_decay_copyable<__data_of<_Sender>> && __nothrow_connectable<__child_t, __child_rcvr_t>; @@ -209,7 +229,7 @@ namespace STDEXEC , __env2_(__mk_secondary_env_t<__set_tag_t>()(__facade::__get_child<0>(__sndr), STDEXEC::get_env(__rcvr_))) { - if constexpr (!__walking) + if constexpr (!__walking()) { __ops_.__emplace_from(STDEXEC::connect, __facade::__get_child<0>(static_cast<_Sender&&>(__sndr)), @@ -287,6 +307,10 @@ namespace STDEXEC using __facade::__defaults::__nested_sender; + // (See __facade::__state_type_t.) + template + using __state_type = __state<_LetTag, _Sender, _Receiver>; + template STDEXEC_ATTRIBUTE(host, device) static constexpr auto __get_state(_Sender&& __sndr, _Receiver __rcvr) @@ -361,7 +385,7 @@ namespace STDEXEC using __started_t = __facade::__effects<__facade::__starts<__key_t>>; // In a walk, only the effects matter (and the state holds no operations to make). - if constexpr (_State::__walking) + if constexpr (_State::__walking()) { if constexpr (__nothrow_v) return __started_t(); diff --git a/include/stdexec/__detail/__sender_facade.hpp b/include/stdexec/__detail/__sender_facade.hpp index 1fb6ad171..eab9090fa 100644 --- a/include/stdexec/__detail/__sender_facade.hpp +++ b/include/stdexec/__detail/__sender_facade.hpp @@ -694,9 +694,41 @@ namespace STDEXEC struct __subscribing_t {}; + // Does the algorithm name the type of its state with an alias, __state_type? + template + concept __declares_state_type = requires { + typename _Algo::template __state_type<_Sender, _Receiver, _Subscribing...>; + }; + + // (A class keyed only by whether the algorithm declares __state_type, so that the debug + // information, which names the class when it names the state's type by this alias, does + // not repeat the types of the sender and the receiver.) + template + struct __state_type_of + { + template + using __f = decltype(_Algo::__get_state(__declval<_Sender>(), + __declval<_Receiver>(), + _Subscribing()...)); + }; + + template <> + struct __state_type_of + { + template + using __f = _Algo::template __state_type<_Sender, _Receiver, _Subscribing...>; + }; + + // The type of the algorithm's state: the one it declares with __state_type, or else the + // type that __get_state returns. Naming the type of a call needs the function's exception + // specification ([except.spec]), and its body if its return type is deduced. An algorithm + // whose state holds operations declares __state_type, so that the walk, which needs only + // the type, does not ask whether making the state can throw: that would name the + // operations below the state. template using __state_type_t = - decltype(_Algo::__get_state(__declval<_Sender>(), __declval<_Receiver>(), _Subscribing()...)); + __state_type_of<__declares_state_type<_Algo, _Sender, _Receiver, _Subscribing...>>:: + template __f<_Algo, _Sender, _Receiver, _Subscribing...>; // Is the child keyed by __child<_Idx> subscribed? It is if it is the item source of the // sender, of type _Sender, and the sender is subscribed. @@ -745,15 +777,17 @@ namespace STDEXEC using __f = __walk_op; }; + // Is the state's receiver an archetype, as it is in a walk? Then the state's operations + // are never made or started (the state is for a walk, or for asking whether connecting + // can throw), so it holds placeholders for them. (Whether making the state can throw is + // still asked of the real operations.) STDEXEC_MODULE_EXPORT_AUTHORING - template class _Fn, class... _Args> - using __state_op_t = __op_or_walk_op< - __std::derived_from<_Rcvr, __receiver_archetype_base>>::template __f<_Fn, _Args...>; + template + concept __walking_receiver = __std::derived_from<_Rcvr, __receiver_archetype_base>; - // Is the state's receiver an archetype, as it is in a walk? STDEXEC_MODULE_EXPORT_AUTHORING - template - inline constexpr bool __walking_v = __std::derived_from<_Rcvr, __receiver_archetype_base>; + template class _Fn, class... _Args> + using __state_op_t = __op_or_walk_op<__walking_receiver<_Rcvr>>::template __f<_Fn, _Args...>; // The type of the operations that a state holds, as _Lazy::__t (a class that names it, so // that it is not instantiated unless used), or _Walk if the state is for a walk. @@ -773,24 +807,8 @@ namespace STDEXEC STDEXEC_MODULE_EXPORT_AUTHORING template - using __lazy_state_ops_t = __lazy_state_ops<__walking_v<_Rcvr>>::template __f<_Lazy, _Walk>; - - // Is a state's value of noexcept(...) that of the trait _Trait (a class with a static - // member `value`)? Not for a walk, whose state is never constructed: it is true, without - // instantiating the trait (which would name the operations below the state). Clang - // instantiates the exception specification of __get_state to get the state's type, and a - // concept-id in a disjunction is satisfied even when the other operand decides it, so - // the trait is what makes this lazy. - template - inline constexpr bool __nothrow_unless_walking_v = _Trait::value; - - template - inline constexpr bool __nothrow_unless_walking_v = true; - - STDEXEC_MODULE_EXPORT_AUTHORING - template - inline constexpr bool __nothrow_state_v = - __nothrow_unless_walking_v<__std::derived_from<_Rcvr, __receiver_archetype_base>, _Trait>; + using __lazy_state_ops_t = + __lazy_state_ops<__walking_receiver<_Rcvr>>::template __f<_Lazy, _Walk>; struct __walk_attach_t { diff --git a/include/stdexec/__detail/__sequence.hpp b/include/stdexec/__detail/__sequence.hpp index c68f8e784..c80194332 100644 --- a/include/stdexec/__detail/__sequence.hpp +++ b/include/stdexec/__detail/__sequence.hpp @@ -250,7 +250,7 @@ namespace STDEXEC , __env2s_(__later_t::__mk_env2s(__sndr, STDEXEC::get_env(__rcvr_))) , __sndrs_(__later_t::__take_senders(static_cast<_Sender &&>(__sndr))) { - if constexpr (__nbr != 0 && !__walking) + if constexpr (__nbr != 0 && !__walking()) { if constexpr (__attaches<0>()) { @@ -276,7 +276,11 @@ namespace STDEXEC { using __t = __variant<__op_t<_Is>...>; }; - static constexpr bool __walking = __facade::__walking_v<_Receiver>; + static consteval auto __walking() noexcept -> bool + { + return __facade::__walking_receiver<_Receiver>; + } + using __ops_t = __facade::__lazy_state_ops_t<_Receiver, __ops_lazy, __variant<__facade::__walk_op>>; @@ -349,6 +353,10 @@ namespace STDEXEC using __state_t = __seq_state<_Sender, _Receiver, __make_indices<__nbr_children_of<_Sender>>, _Subscribing...>; + // (See __facade::__state_type_t.) + template + using __state_type = __state_t<_Sender, _Receiver, _Subscribing...>; + template STDEXEC_ATTRIBUTE(host, device) static constexpr auto __get_state(_Sender &&__sndr, _Receiver __rcvr) @@ -428,7 +436,7 @@ namespace STDEXEC using __started_t = __facade::__effects<__facade::__starts<__facade::__child<_Idx>>>; // In a walk, only the effects matter (and the state holds no operations to make). - if constexpr (_State::__walking) + if constexpr (_State::__walking()) { if constexpr (_State::template __nothrow_attach<_Idx>()) return __started_t(); diff --git a/test/exec/sequence/test_ignore_all_values.cpp b/test/exec/sequence/test_ignore_all_values.cpp index 935774f30..73b5af676 100644 --- a/test/exec/sequence/test_ignore_all_values.cpp +++ b/test/exec/sequence/test_ignore_all_values.cpp @@ -15,6 +15,7 @@ * limitations under the License. */ +#include "exec/sequence.hpp" #include "exec/sequence/empty_sequence.hpp" #include "exec/sequence/ignore_all_values.hpp" @@ -113,5 +114,57 @@ namespace STDEXEC::set_error_t(std::exception_ptr)>; STATIC_REQUIRE(STDEXEC::__mset_eq); } + + // A sequence with no items and no error completions, whose subscription throws + template + struct throwing_subscribe_op + { + void start() & noexcept + { + STDEXEC::set_value(static_cast(rcvr_)); + } + + Rcvr rcvr_; + }; + + struct throwing_subscribe_sequence + { + using sender_concept = exec::sequence_sender_tag; + using completion_signatures = STDEXEC::completion_signatures; + using item_types = exec::item_types<>; + + template Rcvr> + auto subscribe(Rcvr) const noexcept(false) -> throwing_subscribe_op + { + throw std::runtime_error("subscribe"); + } + }; + + TEST_CASE("ignore_all_values - connecting can throw if subscribing can", + "[sequence_senders][ignore_all_values]") + { + using Sender = decltype(exec::ignore_all_values(throwing_subscribe_sequence{})); + STATIC_REQUIRE( + !STDEXEC::__nothrow_connectable>>); + + // So an algorithm that connects it when another sender completes reports the exception. + // (The function is noexcept, so the exception can come only from connecting.) + auto fn = []() noexcept + { + return exec::ignore_all_values(throwing_subscribe_sequence{}); + }; + using Let = decltype(STDEXEC::let_value(STDEXEC::just(), fn)); + using Seq = decltype(exec::sequence(STDEXEC::just(), fn())); + using Eptr = STDEXEC::set_error_t(std::exception_ptr); + STATIC_REQUIRE( + STDEXEC::__mapply, + STDEXEC::completion_signatures_of_t>>::value); + STATIC_REQUIRE( + STDEXEC::__mapply, + STDEXEC::completion_signatures_of_t>>::value); + CHECK_THROWS_AS(STDEXEC::sync_wait(STDEXEC::let_value(STDEXEC::just(), fn)), + std::runtime_error); + CHECK_THROWS_AS(STDEXEC::sync_wait(exec::sequence(STDEXEC::just(), fn())), std::runtime_error); + } #endif // !STDEXEC_NO_STDCPP_EXCEPTIONS() } // namespace