From 60579ac25ea731c23a52cd530b50e04dfc3e00e6 Mon Sep 17 00:00:00 2001 From: Robert Leahy Date: Mon, 30 Nov 2026 10:25:04 -0500 Subject: [PATCH 1/3] Don't Connect Real Operations in Facade Walk The facade's walk constructs an algorithm's state with an archetype receiver, and expects the operations in it to be placeholders (__facade::__state_op_t). starts_on and continues_on (their schedule operations), the counting scopes' join (its schedule operation), and test_facade.cpp's t_schedule connected real operations instead. The above-mentioned now connect their operation through a member that returns the placeholder in a walk. --- include/exec/detail/repeat.hpp | 14 +--- include/stdexec/__detail/__associate.hpp | 12 ++- include/stdexec/__detail/__continues_on.hpp | 13 +-- .../stdexec/__detail/__counting_scopes.hpp | 10 ++- include/stdexec/__detail/__finally.hpp | 33 +++----- include/stdexec/__detail/__let.hpp | 11 ++- include/stdexec/__detail/__sender_facade.hpp | 80 +++++++++++++++++++ include/stdexec/__detail/__sequence.hpp | 20 +++-- include/stdexec/__detail/__starts_on.hpp | 34 ++++---- test/stdexec/detail/test_facade.cpp | 13 ++- 10 files changed, 157 insertions(+), 83 deletions(-) diff --git a/include/exec/detail/repeat.hpp b/include/exec/detail/repeat.hpp index 9107de010..4d04d2545 100644 --- a/include/exec/detail/repeat.hpp +++ b/include/exec/detail/repeat.hpp @@ -126,16 +126,10 @@ namespace experimental::execution::__repeat // themselves afterward.) If the state is for a walk, __body_op_ is a placeholder. constexpr auto __connect_body() noexcept(__nothrow_connect) -> __body_op_t & { - 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}); - } + return __facade::__emplace_state_op<_Receiver>(__body_op_, + STDEXEC::connect, + __child_t(__child_), + __body_rcvr_t{this}); } // Destroys the operations, before the operation completes diff --git a/include/stdexec/__detail/__associate.hpp b/include/stdexec/__detail/__associate.hpp index 48cd712e2..2ff887149 100644 --- a/include/stdexec/__detail/__associate.hpp +++ b/include/stdexec/__detail/__associate.hpp @@ -205,14 +205,12 @@ namespace STDEXEC : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __assoc_(STDEXEC::__allocator_aware_forward(std::move(__parts.first), __rcvr_)) { - if constexpr (!__facade::__walking_receiver<_Receiver>) + if (__assoc_) { - if (__assoc_) - { - __op_.__emplace_from(STDEXEC::connect, - std::move(*__parts.second), - __wrapped_rcvr_t{this}); - } + __facade::__emplace_state_op<_Receiver>(__op_, + STDEXEC::connect, + std::move(*__parts.second), + __wrapped_rcvr_t{this}); } } diff --git a/include/stdexec/__detail/__continues_on.hpp b/include/stdexec/__detail/__continues_on.hpp index b278efa78..e9e7db087 100644 --- a/include/stdexec/__detail/__continues_on.hpp +++ b/include/stdexec/__detail/__continues_on.hpp @@ -159,18 +159,19 @@ namespace STDEXEC STDEXEC_IMMOVABLE(__state); - _Receiver __rcvr_; - __env2_t const __env2_; - __storage_t __storage_{}; - connect_result_t<__sched_sndr_t, __sched_rcvr_t> __op_; + _Receiver __rcvr_; + __env2_t const __env2_; + __storage_t __storage_{}; + __facade::__state_op_t<_Receiver, connect_result_t, __sched_sndr_t, __sched_rcvr_t> __op_; private: template constexpr explicit __state(__scheduler_t __sch, _Child const & __child, _Receiver&& __rcvr) : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __env2_(__trnsfr::__mk_env2(__child, STDEXEC::get_env(__rcvr_))) - , __op_(STDEXEC::connect(STDEXEC::schedule(static_cast<__scheduler_t const &>(__sch)), - __sched_rcvr_t{this})) + , __op_(__facade::__make_state_op<_Receiver>(STDEXEC::connect, + STDEXEC::schedule(__sch), + __sched_rcvr_t{this})) {} }; diff --git a/include/stdexec/__detail/__counting_scopes.hpp b/include/stdexec/__detail/__counting_scopes.hpp index 8b690a8cb..48e74ba26 100644 --- a/include/stdexec/__detail/__counting_scopes.hpp +++ b/include/stdexec/__detail/__counting_scopes.hpp @@ -129,14 +129,18 @@ namespace STDEXEC using __sched_sender_t = __call_result_t>>; using __sched_rcvr_t = __facade::__receiver_t<__join_algo, __sched_op, __join_state, _Sender, _Rcvr>; - using __op_t = connect_result_t<__sched_sender_t, __sched_rcvr_t>; + // (A walk's state holds a placeholder for the operation: see __facade::__state_op_t.) + using __op_t = + __facade::__state_op_t<_Rcvr, connect_result_t, __sched_sender_t, __sched_rcvr_t>; explicit __join_state(__base_scope* __scope, _Rcvr __rcvr) noexcept(__nothrow_connectable<__sched_sender_t, __sched_rcvr_t>) : __join_state_base(__scope, &__join_state::__notify) , __rcvr_(std::move(__rcvr)) - , __op_(STDEXEC::connect(schedule(get_start_scheduler(STDEXEC::get_env(__rcvr_))), - __sched_rcvr_t{this})) + , __op_(__facade::__make_state_op<_Rcvr>(STDEXEC::connect, + schedule( + get_start_scheduler(STDEXEC::get_env(__rcvr_))), + __sched_rcvr_t{this})) {} __join_state(__join_state&&) = delete; diff --git a/include/stdexec/__detail/__finally.hpp b/include/stdexec/__detail/__finally.hpp index fe4a3616b..f194c4360 100644 --- a/include/stdexec/__detail/__finally.hpp +++ b/include/stdexec/__detail/__finally.hpp @@ -134,14 +134,16 @@ 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_(__state::__connect_final(static_cast<_Sender&&>(__sndr), this)) + , __final_op_(__facade::__make_state_op<_Receiver>(STDEXEC::connect, + __facade::__get_child<1>( + static_cast<_Sender&&>(__sndr)), + __final_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}); - } + __facade::__emplace_state_op<_Receiver>(__initial_op_, + STDEXEC::connect, + __facade::__get_child<0>( + static_cast<_Sender&&>(__sndr)), + __initial_rcvr_t{this}); } STDEXEC_IMMOVABLE(__state); @@ -151,23 +153,6 @@ 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, diff --git a/include/stdexec/__detail/__let.hpp b/include/stdexec/__detail/__let.hpp index fb7972cad..af73d23bb 100644 --- a/include/stdexec/__detail/__let.hpp +++ b/include/stdexec/__detail/__let.hpp @@ -230,12 +230,11 @@ namespace STDEXEC , __env2_(__mk_secondary_env_t<__set_tag_t>()(__facade::__get_child<0>(__sndr), STDEXEC::get_env(__rcvr_))) { - if constexpr (!__walking()) - { - __ops_.__emplace_from(STDEXEC::connect, - __facade::__get_child<0>(static_cast<_Sender&&>(__sndr)), - __child_rcvr_t{this}); - } + __facade::__emplace_state_op<_Receiver>(__ops_, + STDEXEC::connect, + __facade::__get_child<0>( + static_cast<_Sender&&>(__sndr)), + __child_rcvr_t{this}); } STDEXEC_IMMOVABLE(__state); diff --git a/include/stdexec/__detail/__sender_facade.hpp b/include/stdexec/__detail/__sender_facade.hpp index e803c2971..9d3216593 100644 --- a/include/stdexec/__detail/__sender_facade.hpp +++ b/include/stdexec/__detail/__sender_facade.hpp @@ -794,6 +794,86 @@ namespace STDEXEC template class _Fn, class... _Args> using __state_op_t = __op_or_walk_op<__walking_receiver<_Rcvr>>::template __f<_Fn, _Args...>; + template + struct __state_op_maker + { + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __make(_Fn &&__fn, _Args &&...__args) + noexcept(__nothrow_callable<_Fn, _Args...>) -> __call_result_t<_Fn, _Args...> + { + return static_cast<_Fn &&>(__fn)(static_cast<_Args &&>(__args)...); + } + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __emplace(_Storage &__storage, _Fn &&__fn, _Args &&...__args) + noexcept(__nothrow_callable<_Fn, _Args...>) -> decltype(auto) + { + return __storage.template __emplace_from<_Idx...>(static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...); + } + }; + + template <> + struct __state_op_maker + { + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __make(_Fn &&, _Args &&...) noexcept -> __walk_op + { + return {}; + } + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __emplace(_Storage &__storage, _Fn &&, _Args &&...) noexcept + -> decltype(auto) + { + return __storage.template __emplace_from<_Idx...>([]() noexcept { return __walk_op(); }); + } + }; + + // Makes an operation for a member of an algorithm's state whose type is + // __state_op_t<_Rcvr, ...>, as __fn(__args...) does (__fn is usually STDEXEC::connect), + // unless the state is for a walk: then it returns the placeholder without calling __fn, + // so the operation's type is never computed. (The arguments are still written, but a + // walk's state is never constructed, so they are never evaluated.) + // + // , __op_(__facade::__make_state_op<_Receiver>(STDEXEC::connect, + // STDEXEC::schedule(__sch), + // __sched_rcvr_t{this})) + // + // __emplace_state_op does the same for a member that holds the operation in an + // __optional or a __variant (with __emplace_from), and returns a reference to it. To + // emplace a __variant's alternative by its index, give the index after _Rcvr. + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr auto __make_state_op(_Fn &&__fn, _Args &&...__args) noexcept(noexcept( + __state_op_maker<__walking_receiver<_Rcvr>>::__make(static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...))) + -> decltype(auto) + { + return __state_op_maker<__walking_receiver<_Rcvr>>::__make(static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...); + } + + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr auto __emplace_state_op(_Storage &__storage, _Fn &&__fn, _Args &&...__args) + noexcept(noexcept(__state_op_maker<__walking_receiver<_Rcvr>>::template __emplace<_Idx...>( + __storage, + static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...))) -> decltype(auto) + { + return __state_op_maker<__walking_receiver<_Rcvr>>::template __emplace<_Idx...>( + __storage, + static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__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. template diff --git a/include/stdexec/__detail/__sequence.hpp b/include/stdexec/__detail/__sequence.hpp index cc3111aef..508af26df 100644 --- a/include/stdexec/__detail/__sequence.hpp +++ b/include/stdexec/__detail/__sequence.hpp @@ -256,20 +256,24 @@ 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) { if constexpr (__attaches<0>()) { - __ops_.__emplace_from(__attach_t<0>(), - *this, - __facade::__get_child<0>(static_cast<_Sender &&>(__sndr)), - __receiver_t<0>{this}); + __facade::__emplace_state_op<_Receiver>(__ops_, + __attach_t<0>(), + *this, + __facade::__get_child<0>( + static_cast<_Sender &&>(__sndr)), + __receiver_t<0>{this}); } else { - __ops_.__emplace_from(STDEXEC::connect, - __facade::__get_child<0>(static_cast<_Sender &&>(__sndr)), - __receiver_t<0>{this}); + __facade::__emplace_state_op<_Receiver>(__ops_, + STDEXEC::connect, + __facade::__get_child<0>( + static_cast<_Sender &&>(__sndr)), + __receiver_t<0>{this}); } } } diff --git a/include/stdexec/__detail/__starts_on.hpp b/include/stdexec/__detail/__starts_on.hpp index 7e61b4374..80d2d8a1a 100644 --- a/include/stdexec/__detail/__starts_on.hpp +++ b/include/stdexec/__detail/__starts_on.hpp @@ -102,38 +102,40 @@ namespace STDEXEC constexpr explicit __state(_Sender&& __sndr, _Receiver&& __rcvr) noexcept(__nothrow_v) : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __env2_(STDEXEC::__mk_sch_env(__facade::__get_data(__sndr), STDEXEC::get_env(__rcvr_))) - , __sched_op_(STDEXEC::connect(STDEXEC::schedule(static_cast<__scheduler_t const &>( - __facade::__get_data(__sndr))), - __sched_rcvr_t{this})) + , __sched_op_(__facade::__make_state_op<_Receiver>( + STDEXEC::connect, + STDEXEC::schedule(static_cast<__scheduler_t const &>(__facade::__get_data(__sndr))), + __sched_rcvr_t{this})) , __child_op_(__attach_child(__facade::__get_child<0>(static_cast<_Sender&&>(__sndr)))) {} STDEXEC_IMMOVABLE(__state); - _Receiver __rcvr_; - __env2_t const __env2_; - connect_result_t<__sched_sndr_t, __sched_rcvr_t> __sched_op_; - __child_op_t __child_op_; + _Receiver __rcvr_; + __env2_t const __env2_; + __facade::__state_op_t<_Receiver, connect_result_t, __sched_sndr_t, __sched_rcvr_t> + __sched_op_; + __child_op_t __child_op_; private: - // Connects the child, or attaches it (see __facade::__attaches_child), unless the state - // is for a walk: then __child_op_ is a placeholder. + // Connects the child, or attaches it (see __facade::__attaches_child) STDEXEC_ATTRIBUTE(always_inline) constexpr auto __attach_child(__child_t&& __child) noexcept(__nothrow_v) -> __child_op_t { - if constexpr (__facade::__walking_receiver<_Receiver>) - { - return {}; - } - else if constexpr (__facade::__attaches_child<__algo, _Sender, 0>) + if constexpr (__facade::__attaches_child<__algo, _Sender, 0>) { using __attach_t = __facade::__attach_child_t<__algo, _Sender, 0, __child_rcvr_t, _Subscribing...>; - return __attach_t()(*this, static_cast<__child_t&&>(__child), __child_rcvr_t{this}); + return __facade::__make_state_op<_Receiver>(__attach_t(), + *this, + static_cast<__child_t&&>(__child), + __child_rcvr_t{this}); } else { - return STDEXEC::connect(static_cast<__child_t&&>(__child), __child_rcvr_t{this}); + return __facade::__make_state_op<_Receiver>(STDEXEC::connect, + static_cast<__child_t&&>(__child), + __child_rcvr_t{this}); } } }; diff --git a/test/stdexec/detail/test_facade.cpp b/test/stdexec/detail/test_facade.cpp index 571d048ef..520def602 100644 --- a/test/stdexec/detail/test_facade.cpp +++ b/test/stdexec/detail/test_facade.cpp @@ -341,13 +341,20 @@ namespace explicit t_schedule_state(Sender &&sndr, Receiver &&rcvr) : __rcvr_(static_cast(rcvr)) - , op_(ex::connect(ex::schedule(ex::__facade::__get_data(sndr)), sched_rcvr_t{this})) + , op_(ex::__facade::__make_state_op(ex::connect, + ex::schedule(static_cast( + ex::__facade::__get_data(sndr))), + sched_rcvr_t{this})) {} t_schedule_state(t_schedule_state &&) = delete; - Receiver __rcvr_; - ex::connect_result_t op_; + // A walk's state holds a placeholder for the operation (see __facade::__state_op_t). + using op_t = + ex::__facade::__state_op_t; + + Receiver __rcvr_; + op_t op_; }; // The completions of the schedule operation are passed on, by the default __complete. From ff60d16fc622eba73af1b5e054f227b238d60090 Mon Sep 17 00:00:00 2001 From: Robert Leahy Date: Mon, 30 Nov 2026 10:25:31 -0500 Subject: [PATCH 2/3] __facade::__send_tuple Algorithms that send stored values applied a lambda that calls __send to the tuple that holds them. __send_tuple(rcvr, tag, tuple) does that, and returns __send's token. --- include/stdexec/__detail/__just.hpp | 7 ++--- include/stdexec/__detail/__sender_facade.hpp | 31 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/stdexec/__detail/__just.hpp b/include/stdexec/__detail/__just.hpp index f3baa2bbc..316b9a46f 100644 --- a/include/stdexec/__detail/__just.hpp +++ b/include/stdexec/__detail/__just.hpp @@ -60,10 +60,9 @@ namespace STDEXEC // Not static_cast(__state.__data_): nvcc's cudafe++ // drops the && when it hands the code to the host compiler, which then copies the // data instead of moving it. - return STDEXEC::__apply( - [&](_Ts&&... __vs) noexcept - { return __facade::__send(__state.__rcvr_, _SetTag(), static_cast<_Ts&&>(__vs)...); }, - static_cast<_State&&>(__state).__data_); + return __facade::__send_tuple(__state.__rcvr_, + _SetTag(), + static_cast<_State&&>(__state).__data_); } }; } // namespace __just diff --git a/include/stdexec/__detail/__sender_facade.hpp b/include/stdexec/__detail/__sender_facade.hpp index 9d3216593..3f8c1e379 100644 --- a/include/stdexec/__detail/__sender_facade.hpp +++ b/include/stdexec/__detail/__sender_facade.hpp @@ -196,9 +196,9 @@ namespace STDEXEC //////////////////////////////////////////////////////////////////////////////////////// // 2. Completing. __send is the only way an algorithm completes: it sends a completion to // the algorithm's receiver, and returns the token that records the completion. - // __start_nested is the only way it starts a child or nested operation. (Call these - // with qualified names, so that argument-dependent lookup does not look in the - // receiver's namespaces.) + // (__send_tuple sends the elements of a tuple.) __start_nested is the only way it starts + // a child or nested operation. (Call these with qualified names, so that + // argument-dependent lookup does not look in the receiver's namespaces.) STDEXEC_MODULE_EXPORT_AUTHORING template STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) @@ -218,6 +218,31 @@ namespace STDEXEC return {}; } + // Sends its arguments as the arguments of a completion (for __send_tuple) + template + struct __send_fn + { + _Receiver &__rcvr_; + + template + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto operator()(_As &&...__as) const noexcept -> __effects<_Tag(_As...)> + { + return __facade::__send(__rcvr_, _Tag(), static_cast<_As &&>(__as)...); + } + }; + + // Sends the elements of a tuple (an STDEXEC::__tuple) as the arguments of a completion, + // as __send(__rcvr, _Tag(), __elements...) would + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto __send_tuple(_Receiver &__rcvr, _Tag, _Tuple &&__tupl) noexcept + -> __apply_result_t<__send_fn<_Receiver, _Tag>, _Tuple> + { + return STDEXEC::__apply(__send_fn<_Receiver, _Tag>{__rcvr}, static_cast<_Tuple &&>(__tupl)); + } + //////////////////////////////////////////////////////////////////////////////////////// // 3. Combinators for control flow whose paths complete differently STDEXEC_MODULE_EXPORT_AUTHORING From 729937e488d2798cb54d819e5aac936b61c88ba1 Mon Sep 17 00:00:00 2001 From: Robert Leahy Date: Wed, 2 Dec 2026 19:00:49 -0500 Subject: [PATCH 3/3] Facade: constexpr __nested_receiver The member functions of __facade::__nested_receiver (the receiver of a child or nested operation that an algorithm keeps in its state) were not constexpr, so an operation that completes through one could not be evaluated in a constant expression. They weren't constexpr because they access the algorithm's state, which is incomplete while the type of the operation it holds is computed, and Clang instantiates a constexpr function as soon as a body it is instantiating calls it. This is the problem that 8ee3a918b ("stdexec::__apply: Do Not Deduce Return Type") describes, and as there the problem is not this receiver, whose return types are declared, but whatever builds a body while the state is incomplete. Making the receiver constexpr found these, each fixed here: - STDEXEC::__subscribe deduced its return type (decltype(auto)), so computing the type of a subscription built its body, and so the body of the sender's __static_subscribe, and so on down to the receiver's get_env. It now declares it, as STDEXEC::connect does. - The bulk sender of task_scheduler deduced the return type of connect. It now declares it. - exec::create's state computes the type of what the function returns when it is called with the context, which holds a receiver that points back at the state. Calling a function whose return type is deduced builds its body, and so (in Clang) the body of the receiver's set_value. The receiver now points at a base of the state holding only the downstream receiver, which is all it uses, and which is complete then. - The receiver proxies of the parallel scheduler and task_scheduler, and iterate's operation, had constexpr virtual functions that complete the receiver they hold. Clang instantiates those with the class. They are no longer constexpr. (They are reached through virtual calls into a scheduler backend, or a type-erased sequence, which can't be constant evaluated anyway. The interfaces still declare them constexpr.) - Two test senders (in test_let_value.cpp and test_starts_on.cpp) deduced the return type of connect. They now declare it. The comment on __nested_receiver now describes these rules. Even so, no operation which completes through __nested_receiver could be evaluated in a constant expression, because algorithms keep their nested operations (and the values they forward) in __variant and __optional (i.e. __manual_lifetime), and both kept their objects in byte arrays, which can't be accessed as objects of another type in constant evaluation (not even in C++26, which allows placement new, as the types aren't similar). Also STDEXEC::schedule wasn't constexpr. So this also: - Replaces the byte array of __manual_lifetime with a union of one member, which it constructs in constant evaluation with std::construct_at from an emplacer (STDEXEC::__emplace_from), as __variant::__emplace_from already did, since placement new isn't constexpr before C++26 and std::construct_at neither brace-initializes nor initializes an immovable type from a prvalue. Outside constant evaluation it still uses placement new. - Replaces the byte array of __variant with a recursive union of its alternatives. Constructing an alternative first makes the nested unions enclosing it active (a no-op outside constant evaluation), and alternatives are reached through the union rather than by casting pointers. - Makes __optional::__emplace_from and STDEXEC::schedule constexpr. - Tests that let_value, let_stopped, finally, starts_on, and continues_on connect, start, and complete in constant expressions. (Without the changes to __nested_receiver these fail, because its get_env isn't constexpr.) The tests are disabled for GCC before 15, which, in constant evaluation, copies an object which a member initializer initializes from a function's prvalue, rather than initializing it in place, leaving the pointers an operation state keeps into itself dangling (__opstate initializes its children that way, so this can't be avoided here), and for compilers which haven't been tried (Clang before 18, Apple Clang, clang-cl, MSVC, NVCC, and other EDG front ends). --- include/exec/create.hpp | 23 +++-- include/exec/sequence/iterate.hpp | 2 +- .../stdexec/__detail/__manual_lifetime.hpp | 65 +++++++++---- include/stdexec/__detail/__optional.hpp | 4 +- .../__detail/__parallel_scheduler_backend.hpp | 6 +- include/stdexec/__detail/__schedulers.hpp | 6 +- include/stdexec/__detail/__sender_facade.hpp | 31 ++++-- include/stdexec/__detail/__subscribe.hpp | 23 ++++- include/stdexec/__detail/__task_scheduler.hpp | 23 ++++- include/stdexec/__detail/__variant.hpp | 94 +++++++++++++++---- .../algos/adaptors/test_continues_on.cpp | 8 ++ test/stdexec/algos/adaptors/test_finally.cpp | 6 ++ .../algos/adaptors/test_let_stopped.cpp | 8 ++ .../stdexec/algos/adaptors/test_let_value.cpp | 9 ++ .../stdexec/algos/adaptors/test_starts_on.cpp | 9 +- test/test_common/constexpr.hpp | 85 +++++++++++++++++ 16 files changed, 335 insertions(+), 67 deletions(-) create mode 100644 test/test_common/constexpr.hpp diff --git a/include/exec/create.hpp b/include/exec/create.hpp index 6a86039c5..a238e5639 100644 --- a/include/exec/create.hpp +++ b/include/exec/create.hpp @@ -71,13 +71,23 @@ namespace experimental::execution struct __algo; + // The part of the state that the context's receiver uses. (It is complete while + // __state's members are computed, which call the function with the context: so the + // receiver's member functions can be instantiated then.) + template + struct __state_base + { + _Receiver __rcvr_; + }; + template - struct __state + struct __state : __state_base<_Receiver> { - using __data_t = __decay_t<__facade::__data_of_t<_Sender>>; - using __fun_t = decltype(__data_t::__fun_); - using __args_t = decltype(__data_t::__args_); - using __rcvr_t = __facade::__receiver_t<__algo, __callback, __state, _Sender, _Receiver>; + using __data_t = __decay_t<__facade::__data_of_t<_Sender>>; + using __fun_t = decltype(__data_t::__fun_); + using __args_t = decltype(__data_t::__args_); + using __rcvr_t = + __facade::__receiver_t<__algo, __callback, __state_base<_Receiver>, _Sender, _Receiver>; using __context_t = __context<__rcvr_t, __args_t>; static consteval auto __walking() noexcept -> bool @@ -115,7 +125,6 @@ namespace experimental::execution } } - _Receiver __rcvr_; STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS __context_t __ctx_; STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS @@ -126,7 +135,7 @@ namespace experimental::execution private: template constexpr explicit __state(_Data&& __data, _Receiver&& __rcvr) - : __rcvr_(static_cast<_Receiver&&>(__rcvr)) + : __state_base<_Receiver>{static_cast<_Receiver&&>(__rcvr)} , __ctx_{__rcvr_t{this}, static_cast<_Data&&>(__data).__args_} , __fun_(static_cast<_Data&&>(__data).__fun_) {} diff --git a/include/exec/sequence/iterate.hpp b/include/exec/sequence/iterate.hpp index cb451f7f8..e8702038a 100644 --- a/include/exec/sequence/iterate.hpp +++ b/include/exec/sequence/iterate.hpp @@ -229,7 +229,7 @@ namespace experimental::execution } } - constexpr void __start_next() noexcept final + void __start_next() noexcept final { STDEXEC_TRY { diff --git a/include/stdexec/__detail/__manual_lifetime.hpp b/include/stdexec/__detail/__manual_lifetime.hpp index 83287db1a..973bc0454 100644 --- a/include/stdexec/__detail/__manual_lifetime.hpp +++ b/include/stdexec/__detail/__manual_lifetime.hpp @@ -25,6 +25,7 @@ import stdexec; #else # include "__concepts.hpp" +# include "__utility.hpp" # if !STDEXEC_USE_MODULES() # include @@ -61,43 +62,70 @@ namespace STDEXEC constexpr auto __construct(_Args&&... __args) noexcept(STDEXEC::__nothrow_constructible_from<_Ty, _Args...>) -> _Ty& { - // Use placement new instead of std::construct_at to support aggregate - // initialization with brace elision. - return *std::launder(::new (static_cast(__buffer_)) - _Ty{static_cast<_Args&&>(__args)...}); + STDEXEC_IF_CONSTEVAL + { + // Placement new is not usable in constant evaluation before C++26 (P2747), and + // std::construct_at uses parentheses, so brace-initialize via an emplacer. + return *std::construct_at(std::addressof(__value_), + STDEXEC::__emplace_from{ + [&]() noexcept( + STDEXEC::__nothrow_constructible_from<_Ty, _Args...>) + { return _Ty{static_cast<_Args&&>(__args)...}; }}); + } + else + { + // Use placement new instead of std::construct_at to support aggregate + // initialization with brace elision. + return *::new (static_cast(std::addressof(__value_))) + _Ty{static_cast<_Args&&>(__args)...}; + } } //! Construct the `_Ty` in place from the result of calling `func`. There //! are no safeties guarding against the case that there's already one //! there. template - constexpr auto __construct_from(_Func&& func, _Args&&... __args) -> _Ty& + constexpr auto __construct_from(_Func&& func, _Args&&... __args) + noexcept(STDEXEC::__nothrow_callable<_Func, _Args...>) -> _Ty& { - // Use placement new instead of std::construct_at in case the function - // returns an immovable type. - return *std::launder(::new (static_cast(__buffer_)) - _Ty{static_cast<_Func&&>(func)(static_cast<_Args&&>(__args)...)}); + STDEXEC_IF_CONSTEVAL + { + // std::construct_at would need _Ty to be movable, so convert from an emplacer, + // whose conversion function's result initializes the object directly. + return *std::construct_at( + std::addressof(__value_), + STDEXEC::__emplace_from{ + [&]() noexcept(STDEXEC::__nothrow_callable<_Func, _Args...>) -> _Ty + { return static_cast<_Func&&>(func)(static_cast<_Args&&>(__args)...); }}); + } + else + { + // Use placement new instead of std::construct_at in case the function + // returns an immovable type. + return *::new (static_cast(std::addressof(__value_))) + _Ty{static_cast<_Func&&>(func)(static_cast<_Args&&>(__args)...)}; + } } //! End the lifetime of the contained `_Ty`. //! @pre The lifetime has started. constexpr void __destroy() noexcept { - std::destroy_at(&__get()); + std::destroy_at(std::addressof(__value_)); } //! Get access to the `_Ty`. //! @pre The lifetime has started. constexpr auto __get() & noexcept -> _Ty& { - return *reinterpret_cast<_Ty*>(__buffer_); + return __value_; } //! Get access to the `_Ty`. //! @pre The lifetime has started. constexpr auto __get() && noexcept -> _Ty&& { - return static_cast<_Ty&&>(*reinterpret_cast<_Ty*>(__buffer_)); + return static_cast<_Ty&&>(__value_); } //! Get access to the `_Ty`. @@ -105,7 +133,7 @@ namespace STDEXEC [[nodiscard]] constexpr auto __get() const & noexcept -> _Ty const & { - return *reinterpret_cast<_Ty const *>(__buffer_); + return __value_; } [[nodiscard]] @@ -113,16 +141,21 @@ namespace STDEXEC constexpr auto operator->() noexcept -> _Ty* { - return reinterpret_cast<_Ty*>(__buffer_); + return std::addressof(__value_); } constexpr auto operator->() const noexcept -> _Ty const * { - return reinterpret_cast<_Ty const *>(__buffer_); + return std::addressof(__value_); } private: - alignas(_Ty) unsigned char __buffer_[sizeof(_Ty)]{}; + // A union, rather than a byte buffer, so that the _Ty's lifetime can be managed + // in constant evaluation. + union + { + _Ty __value_; + }; }; template diff --git a/include/stdexec/__detail/__optional.hpp b/include/stdexec/__detail/__optional.hpp index 65b78a773..0624bc4e0 100644 --- a/include/stdexec/__detail/__optional.hpp +++ b/include/stdexec/__detail/__optional.hpp @@ -128,8 +128,8 @@ namespace STDEXEC template requires __std::same_as<_Tp, __call_result_t<_Fn, _Args...>> - auto __emplace_from(_Fn&& __f, _Args&&... __args) noexcept(__nothrow_callable<_Fn, _Args...>) - -> _Tp& + constexpr auto __emplace_from(_Fn&& __f, _Args&&... __args) + noexcept(__nothrow_callable<_Fn, _Args...>) -> _Tp& { reset(); auto __sg = __mk_has_value_guard(__has_value_); diff --git a/include/stdexec/__detail/__parallel_scheduler_backend.hpp b/include/stdexec/__detail/__parallel_scheduler_backend.hpp index e9769997e..2f3593f4f 100644 --- a/include/stdexec/__detail/__parallel_scheduler_backend.hpp +++ b/include/stdexec/__detail/__parallel_scheduler_backend.hpp @@ -246,7 +246,7 @@ namespace STDEXEC } } - constexpr void set_stopped() noexcept final + void set_stopped() noexcept final { if constexpr (_Infallible && unstoppable_token<__stop_token_t>) { @@ -261,7 +261,7 @@ namespace STDEXEC } protected: - constexpr void + void __query_env(__type_index __query_id, __type_index __value, void* __dest) const noexcept final { if (__query_id == __mtypeid) @@ -347,7 +347,7 @@ namespace STDEXEC { using __receiver_proxy::__receiver_proxy_base::__receiver_proxy_base; - constexpr void set_value() noexcept final + void set_value() noexcept final { this->__unregister_stop_callback(); STDEXEC::set_value(std::move(this->__rcvr_)); diff --git a/include/stdexec/__detail/__schedulers.hpp b/include/stdexec/__detail/__schedulers.hpp index 40af25704..b2350b1bf 100644 --- a/include/stdexec/__detail/__schedulers.hpp +++ b/include/stdexec/__detail/__schedulers.hpp @@ -138,7 +138,7 @@ namespace STDEXEC private: template STDEXEC_ATTRIBUTE(host, device, always_inline) - static auto __schedule(_Scheduler &&__sched) + static constexpr auto __schedule(_Scheduler &&__sched) noexcept(noexcept(static_cast<_Scheduler &&>(__sched).schedule())) -> decltype(static_cast<_Scheduler &&>(__sched).schedule()) { @@ -167,13 +167,13 @@ namespace STDEXEC template requires __has_schedule_member<_Scheduler> && __has_scheduler_concept<__decay_t<_Scheduler>> STDEXEC_ATTRIBUTE(host, device, always_inline) - auto operator()(_Scheduler &&__sched) const + constexpr auto operator()(_Scheduler &&__sched) const STDEXEC_AUTO_RETURN(__schedule(static_cast<_Scheduler &&>(__sched))) template requires __has_schedule_member<_Scheduler> [[deprecated("The scheduler must define the schedule_concept tag.")]] - STDEXEC_ATTRIBUTE(host, device, always_inline) auto operator()(_Scheduler &&__sched) const + STDEXEC_ATTRIBUTE(host, device, always_inline) constexpr auto operator()(_Scheduler &&__sched) const STDEXEC_AUTO_RETURN(__schedule(static_cast<_Scheduler &&>(__sched))) // clang-format on }; diff --git a/include/stdexec/__detail/__sender_facade.hpp b/include/stdexec/__detail/__sender_facade.hpp index 3f8c1e379..c2ceb20e2 100644 --- a/include/stdexec/__detail/__sender_facade.hpp +++ b/include/stdexec/__detail/__sender_facade.hpp @@ -581,11 +581,22 @@ namespace STDEXEC // The receiver of an operation that an algorithm stores in its state: a // nested operation, or a child that the algorithm connects itself. Its - // member functions are not constexpr, so that their bodies are instantiated - // only once the state is complete. (The type of an operation can depend on - // its receiver's member functions, for example when the operation - // type-erases its receiver, and the state is incomplete while that type is - // computed. Clang instantiates constexpr functions eagerly.) + // member functions are constexpr, and they access the state, which is + // incomplete while the operation's type is computed (the state holds the + // operation). Naming them then is fine: their return types are declared. + // But Clang instantiates a constexpr function as soon as an instantiated + // body calls it, so nothing may build such a body while the state is + // incomplete. Building a body to deduce its return type is how that + // usually happens: a function on the way from computing an operation's + // type to this receiver's member functions (a connect, a subscribe, a + // function called with a context holding this receiver, ...) must not + // deduce its return type. Nor may a class that holds this receiver and is + // instantiated then have constexpr virtual functions that call it (Clang + // instantiates them with the class). An algorithm whose state must ask + // such a function for a type (as exec::create asks for the result of its + // function) can point this receiver at a part of the state that is + // complete by then. (See 8ee3a918b, "stdexec::__apply: Do Not Deduce + // Return Type".) template struct __nested_receiver : _Algo::__receiver_base { @@ -599,20 +610,20 @@ namespace STDEXEC template STDEXEC_ATTRIBUTE(always_inline, host, device) - void set_value(_As &&...__as) noexcept + constexpr void set_value(_As &&...__as) noexcept { (void) _Algo::__complete(_Key(), *__state_, set_value_t(), static_cast<_As &&>(__as)...); } template STDEXEC_ATTRIBUTE(always_inline, host, device) - void set_error(_Error &&__err) noexcept + constexpr void set_error(_Error &&__err) noexcept { (void) _Algo::__complete(_Key(), *__state_, set_error_t(), static_cast<_Error &&>(__err)); } STDEXEC_ATTRIBUTE(always_inline, host, device) - void set_stopped() noexcept + constexpr void set_stopped() noexcept { (void) _Algo::__complete(_Key(), *__state_, set_stopped_t()); } @@ -620,7 +631,7 @@ namespace STDEXEC // Passes the items of the item source of a subscribed sender to the algorithm's __next template ::__t> STDEXEC_ATTRIBUTE(always_inline) - auto set_next(_Item &&__item) & noexcept(noexcept( + constexpr auto set_next(_Item &&__item) & noexcept(noexcept( _Algo::__next(_Key(), __declval<_State &>(), __declval<_Rcvr &>(), __declval<_Item>()))) -> decltype(_Algo::__next(_Key(), __declval<_State &>(), @@ -631,7 +642,7 @@ namespace STDEXEC } STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) - auto get_env() const noexcept -> _Env + constexpr auto get_env() const noexcept -> _Env { static_assert(__same_as, "__get_env and __env_for must produce environments of the same type"); diff --git a/include/stdexec/__detail/__subscribe.hpp b/include/stdexec/__detail/__subscribe.hpp index 7b8a5bf4d..caf7ff8fd 100644 --- a/include/stdexec/__detail/__subscribe.hpp +++ b/include/stdexec/__detail/__subscribe.hpp @@ -72,17 +72,36 @@ namespace STDEXEC } } + template + using __static_memfn_result_t = decltype(STDEXEC_REMOVE_REFERENCE( + _Sender)::__static_subscribe(__declval<_Sender>(), __declval<_Receiver>())); + + template + using __memfn_result_t = decltype(__declval<_Sender>().subscribe(__declval<_Receiver>())); + + // The result of subscribing, without building the body of __subscribe_t::operator() + // (see below) + template + using __result_t = __minvoke_if_c<__has_static_memfn<_Sender, _Receiver>, + __q<__static_memfn_result_t>, + __q<__memfn_result_t>, + _Sender, + _Receiver>; + // The customization point behind exec::subscribe. A sender hooks it with a // static __static_subscribe(sndr, rcvr) member or a subscribe(rcvr) member // function, the static one taking precedence. Senders defined in stdexec can - // use this to behave as sequence senders without depending on exec. + // use this to behave as sequence senders without depending on exec. (Its return type + // is not deduced: deducing it would build its body, and so, in Clang, the bodies of the + // constexpr functions that it calls, which can use types that are incomplete then. See + // the comment on STDEXEC::__facade::__nested_receiver.) struct __subscribe_t { template requires __has_static_memfn<_Sender, _Receiver> || __has_memfn<_Sender, _Receiver> STDEXEC_ATTRIBUTE(always_inline) constexpr auto operator()(_Sender&& __sndr, _Receiver __rcvr) const - noexcept(__nothrow_subscribe<_Sender, _Receiver>()) -> decltype(auto) + noexcept(__nothrow_subscribe<_Sender, _Receiver>()) -> __result_t<_Sender, _Receiver> { if constexpr (__has_static_memfn<_Sender, _Receiver>) { diff --git a/include/stdexec/__detail/__task_scheduler.hpp b/include/stdexec/__detail/__task_scheduler.hpp index 63c2b838b..2bb652d17 100644 --- a/include/stdexec/__detail/__task_scheduler.hpp +++ b/include/stdexec/__detail/__task_scheduler.hpp @@ -537,7 +537,7 @@ namespace STDEXEC , __backend_(std::move(__backend)) {} - constexpr void set_value() noexcept final + void set_value() noexcept final { // Send the stored values to the downstream receiver. __visit([](auto& __rcvr, auto& __tupl) @@ -547,7 +547,7 @@ namespace STDEXEC } //! Actually runs the bulk operation over the specified range. - constexpr void execute(size_t __begin, size_t __end) noexcept final + void execute(size_t __begin, size_t __end) noexcept final { STDEXEC_TRY { @@ -623,8 +623,25 @@ namespace STDEXEC , __attrs_{std::move(__sch)} {} + // The type of the operation state that connect returns. (Its return type is not + // deduced: deducing it would build the body, and so, in Clang, the bodies of the + // constexpr functions it calls, such as the receiver's get_env, which can use types + // that are incomplete then. See the comment on STDEXEC::__facade::__nested_receiver.) template - constexpr auto connect(_Rcvr __rcvr) && + static constexpr auto __opstate_type(_Sndr& __sndr) noexcept + { + auto& [__tag, __data, __child] = __sndr; + auto& [__pol, __shape, __fn] = __data; + return __mtype<__bulk_opstate>{}; + } + + template + constexpr auto connect( + _Rcvr __rcvr) && -> __t(__declval<_Sndr&>()))> { auto& [__tag, __data, __child] = __sndr_; auto& [__pol, __shape, __fn] = __data; diff --git a/include/stdexec/__detail/__variant.hpp b/include/stdexec/__detail/__variant.hpp index 5d51b6350..e2f48fd2a 100644 --- a/include/stdexec/__detail/__variant.hpp +++ b/include/stdexec/__detail/__variant.hpp @@ -115,6 +115,63 @@ namespace STDEXEC { return static_cast<_Fn &&>(__fn)(static_cast<_As &&>(__as)...); }); } + // Storage for the alternatives of a __variant. A union, rather than a byte buffer, so + // that the alternatives' lifetimes can be managed in constant evaluation. + template + union __union; + + template <> + union __union<> + {}; + + template + union __union<_Head, _Tail...> + { + STDEXEC_ATTRIBUTE(host, device) + constexpr __union() noexcept {} // NOLINT(modernize-use-equals-default) + + STDEXEC_ATTRIBUTE(host, device) + constexpr ~__union() {} // NOLINT(modernize-use-equals-default) + + _Head __head_; + __union<_Tail...> __tail_; + }; + + // Returns the _Ny-th alternative of a __union. + // @pre It is the active alternative. + template + STDEXEC_ATTRIBUTE(host, device, always_inline) + constexpr auto __union_get(_Union &__u) noexcept -> auto & + { + if constexpr (_Ny == 0) + { + return __u.__head_; + } + else + { + return __var::__union_get<_Ny - 1>(__u.__tail_); + } + } + + // Returns a pointer to the storage of the _Ny-th alternative of a __union with no + // active alternative, at which it may be constructed. The nested unions enclosing it + // are made active along the way, which constant evaluation requires and which is + // otherwise a no-op. + template + STDEXEC_ATTRIBUTE(host, device, always_inline) + constexpr auto __union_activate(_Union &__u) noexcept + { + if constexpr (_Ny == 0) + { + return std::addressof(__u.__head_); + } + else + { + std::construct_at(std::addressof(__u.__tail_)); + return __var::__union_activate<_Ny - 1>(__u.__tail_); + } + } + template class __variant; @@ -148,8 +205,6 @@ namespace STDEXEC template _Idx, class... _Ts> class __variant<_Idx, _Ts...> { - static constexpr std::size_t __max_size = STDEXEC::__umax({sizeof(_Ts)...}); - struct __move_visitor { template @@ -175,16 +230,17 @@ namespace STDEXEC STDEXEC_ATTRIBUTE(host, device) constexpr void __destroy() noexcept { - auto __index = std::exchange(__index_, __variant_npos); - void *const __ptr = __data(); + auto __index = std::exchange(__index_, __variant_npos); if (__variant_npos != __index) { - ((_Is == __index ? std::destroy_at(static_cast<_Ts *>(__ptr)) : void(0)), ...); + ((_Is == __index ? std::destroy_at(std::addressof(__var::__union_get<_Is>(__storage_))) + : void(0)), + ...); } } - std::size_t __index_{__variant_npos}; - alignas(_Ts...) std::byte __storage_[__max_size]; + std::size_t __index_{__variant_npos}; + __union<_Ts...> __storage_; public: template @@ -255,14 +311,14 @@ namespace STDEXEC STDEXEC_ATTRIBUTE(host, device, always_inline) // constexpr auto __data() noexcept -> void * { - return __storage_; + return std::addressof(__storage_); } [[nodiscard]] STDEXEC_ATTRIBUTE(host, device, always_inline) // constexpr auto __data() const noexcept -> void const * { - return __storage_; + return std::addressof(__storage_); } [[nodiscard]] @@ -305,9 +361,10 @@ namespace STDEXEC __destroy(); auto __sg = __mk_index_guard(__index_, __new_index); - auto *__ptr = std::construct_at(static_cast<_Ty *>(__data()), static_cast<_As &&>(__as)...); + auto *__ptr = std::construct_at(__var::__union_activate<__new_index>(__storage_), + static_cast<_As &&>(__as)...); __sg.__dismiss(); - return *std::launder(__ptr); + return *__ptr; } template @@ -319,10 +376,10 @@ namespace STDEXEC __destroy(); auto __sg = __mk_index_guard(__index_, _Ny); - auto *__ptr = std::construct_at(static_cast<__at_t<_Ny> *>(__data()), + auto *__ptr = std::construct_at(__var::__union_activate<_Ny>(__storage_), static_cast<_As &&>(__as)...); __sg.__dismiss(); - return *std::launder(__ptr); + return *__ptr; } template @@ -339,17 +396,17 @@ namespace STDEXEC STDEXEC_IF_CONSTEVAL { auto *__ptr = std::construct_at( - static_cast<__value_t *>(__data()), + __var::__union_activate<_Ny>(__storage_), __var::__mk_emplace_from_fn(static_cast<_Fn &&>(__fn), static_cast<_As &&>(__as)...)); __sg.__dismiss(); - return *std::launder(__ptr); + return *__ptr; } else { - auto *__ptr = ::new (__data()) + auto *__ptr = ::new (static_cast(__var::__union_activate<_Ny>(__storage_))) __value_t(static_cast<_Fn &&>(__fn)(static_cast<_As &&>(__as)...)); __sg.__dismiss(); - return *std::launder(__ptr); + return *__ptr; } } @@ -390,10 +447,9 @@ namespace STDEXEC STDEXEC_ATTRIBUTE(nodiscard, host, device, always_inline) static constexpr auto __get(_Self &&__self) noexcept -> __copy_cvref_t<_Self, __at_t<_Ny>> && { - using __value_t = __at_t<_Ny>; STDEXEC_ASSERT(_Ny == __self.__index_); return static_cast<__copy_cvref_t<_Self, __at_t<_Ny>> &&>( - *const_cast<__value_t *>(static_cast<__value_t const *>(__self.__data()))); + __var::__union_get<_Ny>(__self.__storage_)); } }; diff --git a/test/stdexec/algos/adaptors/test_continues_on.cpp b/test/stdexec/algos/adaptors/test_continues_on.cpp index 93d1b4d3e..67e419ae5 100644 --- a/test/stdexec/algos/adaptors/test_continues_on.cpp +++ b/test/stdexec/algos/adaptors/test_continues_on.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,13 @@ using namespace std::chrono_literals; namespace { +#if STDEXEC_TEST_CONSTEXPR_NESTED_OPERATIONS() + // continues_on's schedule operation completes through a receiver that points into the + // continues_on operation, and the values it forwards are stored in a __variant + static_assert(constexpr_connect_and_start(ex::just(4) | ex::continues_on(ex::inline_scheduler{})) + == 4); +#endif + template using value_type_of_t = ex::value_types_of_t, std::type_identity_t, std::type_identity_t>; diff --git a/test/stdexec/algos/adaptors/test_finally.cpp b/test/stdexec/algos/adaptors/test_finally.cpp index da46c8b9d..0eecf58a1 100644 --- a/test/stdexec/algos/adaptors/test_finally.cpp +++ b/test/stdexec/algos/adaptors/test_finally.cpp @@ -16,6 +16,7 @@ #include #include "exec/finally.hpp" +#include "test_common/constexpr.hpp" #include "test_common/type_helpers.hpp" #include @@ -24,6 +25,11 @@ using namespace STDEXEC; namespace { +#if STDEXEC_TEST_CONSTEXPR_NESTED_OPERATIONS() + // finally's operations complete through receivers that point into the finally + // operation, and are stored in an __optional + static_assert(constexpr_connect_and_start(exec::finally(just(5), just())) == 5); +#endif TEST_CASE("finally is a sender", "[adaptors][finally]") { diff --git a/test/stdexec/algos/adaptors/test_let_stopped.cpp b/test/stdexec/algos/adaptors/test_let_stopped.cpp index 7c317458c..82a697caa 100644 --- a/test/stdexec/algos/adaptors/test_let_stopped.cpp +++ b/test/stdexec/algos/adaptors/test_let_stopped.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -36,6 +37,13 @@ using namespace std::chrono_literals; namespace { +#if STDEXEC_TEST_CONSTEXPR_NESTED_OPERATIONS() + // let_stopped's second operation completes through a receiver that points into the + // let_stopped operation, and is stored in a __variant + static_assert(constexpr_connect_and_start( + ex::just_stopped() | ex::let_stopped([]() noexcept { return ex::just(6); })) + == 6); +#endif TEST_CASE("let_stopped returns a sender", "[adaptors][let_stopped]") { diff --git a/test/stdexec/algos/adaptors/test_let_value.cpp b/test/stdexec/algos/adaptors/test_let_value.cpp index 5621f33fd..4f6eb796f 100644 --- a/test/stdexec/algos/adaptors/test_let_value.cpp +++ b/test/stdexec/algos/adaptors/test_let_value.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,13 @@ using namespace std::chrono_literals; namespace { +#if STDEXEC_TEST_CONSTEXPR_NESTED_OPERATIONS() + // let_value's second operation completes through a receiver that points into the + // let_value operation, and is stored in a __variant + static_assert(constexpr_connect_and_start( + ex::just(1) | ex::let_value([](int i) noexcept { return ex::just(i + 1); })) + == 2); +#endif TEST_CASE("let_value returns a sender", "[adaptors][let_value]") { @@ -521,6 +529,7 @@ namespace template [[nodiscard]] auto connect(Receiver r) const & noexcept + -> ::STDEXEC::connect_result_t { return ::STDEXEC::connect(::STDEXEC::just(), std::move(r)); } diff --git a/test/stdexec/algos/adaptors/test_starts_on.cpp b/test/stdexec/algos/adaptors/test_starts_on.cpp index 30acd1259..a1eae73b3 100644 --- a/test/stdexec/algos/adaptors/test_starts_on.cpp +++ b/test/stdexec/algos/adaptors/test_starts_on.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,12 @@ STDEXEC_PRAGMA_IGNORE_GNU("-Wunneeded-internal-declaration") namespace { +#if STDEXEC_TEST_CONSTEXPR_NESTED_OPERATIONS() + // starts_on's schedule operation completes through a receiver that points into the + // starts_on operation + static_assert(constexpr_connect_and_start(ex::starts_on(ex::inline_scheduler{}, ex::just(3))) + == 3); +#endif TEST_CASE("starts_on returns a sender", "[adaptors][starts_on]") { @@ -349,7 +356,7 @@ namespace }; template - static auto connect(Receiver rcvr) + static auto connect(Receiver rcvr) -> ex::connect_result_t { return ex::connect(ex::just(), static_cast(rcvr)); } diff --git a/test/test_common/constexpr.hpp b/test/test_common/constexpr.hpp new file mode 100644 index 000000000..e5c123cde --- /dev/null +++ b/test/test_common/constexpr.hpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2026 Robert Leahy. All rights reserved. + * + * Licensed under the Apache License Version 2.0 with LLVM Exceptions + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://llvm.org/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#if STDEXEC_USE_MODULES() +import std; +import stdexec; +#else +# include + +# include +#endif + +// Whether operation states that point into themselves, as those of algorithms that +// connect nested operations do, can be connected and started in constant expressions. +// GCC before 15, in constant evaluation, copies an object that a member initializer +// initializes from a function's prvalue, which leaves such pointers dangling. Other +// compilers are untested. +#if !STDEXEC_EDG() \ + && ((STDEXEC_GCC() && STDEXEC_GCC_VERSION >= 1500) \ + || (STDEXEC_CLANG() && !STDEXEC_CLANG_CL() && !STDEXEC_APPLE_CLANG() \ + && STDEXEC_CLANG_VERSION >= 1800)) +# define STDEXEC_TEST_CONSTEXPR_NESTED_OPERATIONS() 1 +#else +# define STDEXEC_TEST_CONSTEXPR_NESTED_OPERATIONS() 0 +#endif + +namespace +{ + // Records the int its sender completes with, or a negative number which identifies + // its other completion: for use in constant expressions + struct constexpr_receiver + { + using receiver_concept = STDEXEC::receiver_tag; + + constexpr void set_value(int i) && noexcept + { + result = i; + } + + constexpr void set_value() && noexcept + { + result = -1; + } + + template + constexpr void set_error(Error&&) && noexcept + { + result = -2; + } + + constexpr void set_stopped() && noexcept + { + result = -3; + } + + int& result; + }; + + // Connects sndr to a constexpr_receiver, starts the operation, and returns what the + // receiver recorded + template + constexpr auto constexpr_connect_and_start(Sender&& sndr) -> int + { + int result = 0; + auto op = STDEXEC::connect(std::forward(sndr), constexpr_receiver{result}); + STDEXEC::start(op); + return result; + } +} // namespace