From 7f9b12a2fbb1cbd1a3e9625c104d9a5459d005bb Mon Sep 17 00:00:00 2001 From: Robert Leahy Date: Wed, 2 Dec 2026 19:00:49 -0500 Subject: [PATCH 1/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 947fe0ec6..a585054ed 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 6097d1741..5611f65f2 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 7d17fb09b..e87463e4b 100644 --- a/include/stdexec/__detail/__sender_facade.hpp +++ b/include/stdexec/__detail/__sender_facade.hpp @@ -556,11 +556,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 { @@ -574,20 +585,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()); } @@ -595,7 +606,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 &>(), @@ -606,7 +617,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 From 3f09479d16df29b25e5a4825efeed327c9938383 Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Sat, 5 Dec 2026 10:48:43 -0800 Subject: [PATCH 2/3] fold together the facade's `__receiver` and `__nested_receiver` also, fix a clang compile error by giving \`repeat\`'s \`__start_iteration\` a declared return type. --- include/exec/detail/repeat.hpp | 9 +- include/stdexec/__detail/__sender_facade.hpp | 129 +++--------------- include/stdexec/__detail/__subscribe.hpp | 3 +- include/stdexec/__detail/__task_scheduler.hpp | 2 +- 4 files changed, 24 insertions(+), 119 deletions(-) diff --git a/include/exec/detail/repeat.hpp b/include/exec/detail/repeat.hpp index 9107de010..2b5ad34dc 100644 --- a/include/exec/detail/repeat.hpp +++ b/include/exec/detail/repeat.hpp @@ -246,9 +246,11 @@ namespace experimental::execution::__repeat decltype(__algo_base::__get_state<__mconst<__ignore>::__f>(__declval<_Sender>(), __declval<_Receiver>())); + using __iteration_effects_t = __facade::__effects<__facade::__starts<__bounce>>; + // Starts an iteration: the trampoline, which starts the body template - static constexpr auto __start_iteration(_State &__state) noexcept + static constexpr auto __start_iteration(_State &__state) noexcept -> __iteration_effects_t { using __bounce_rcvr_t = _State::__bounce_rcvr_t; auto &__op = __state.__bounce_op_.__emplace_from(STDEXEC::connect, @@ -269,9 +271,8 @@ namespace experimental::execution::__repeat } else { - using __result_t = - __facade::__union_t>; + using __eptr_effects_t = __facade::__effects; + using __result_t = __facade::__union_t<__iteration_effects_t, __eptr_effects_t>; STDEXEC_TRY { __state.__connect_body(); diff --git a/include/stdexec/__detail/__sender_facade.hpp b/include/stdexec/__detail/__sender_facade.hpp index e87463e4b..60014fc10 100644 --- a/include/stdexec/__detail/__sender_facade.hpp +++ b/include/stdexec/__detail/__sender_facade.hpp @@ -554,79 +554,6 @@ namespace STDEXEC _State *__state_; }; - // 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 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 - { - using receiver_concept = receiver_tag; - using __child_receiver_t = void; // see STDEXEC::__child_receiver - - STDEXEC_ATTRIBUTE(always_inline, host, device) - constexpr explicit __nested_receiver(_State *__state) noexcept - : __state_(__state) - {} - - template - STDEXEC_ATTRIBUTE(always_inline, host, device) - 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) - 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) - constexpr void set_stopped() noexcept - { - (void) _Algo::__complete(_Key(), *__state_, set_stopped_t()); - } - - // Passes the items of the item source of a subscribed sender to the algorithm's __next - template ::__t> - STDEXEC_ATTRIBUTE(always_inline) - constexpr auto set_next(_Item &&__item) & noexcept(noexcept( - _Algo::__next(_Key(), __declval<_State &>(), __declval<_Rcvr &>(), __declval<_Item>()))) - -> decltype(_Algo::__next(_Key(), - __declval<_State &>(), - __declval<_Rcvr &>(), - __declval<_Item>())) - { - return _Algo::__next(_Key(), *__state_, __state_->__rcvr_, static_cast<_Item &&>(__item)); - } - - STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) - constexpr auto get_env() const noexcept -> _Env - { - static_assert(__same_as, - "__get_env and __env_for must produce environments of the same type"); - return _Algo::__get_env(_Key(), *__state_); - } - - _State *__state_; - }; - // The receiver of an operation that an algorithm stores in its state, when the state's // receiver is an archetype (as in the walk). Its completions do nothing, so analyzing an // algorithm does not instantiate the code that completes it. (That would instantiate @@ -634,14 +561,12 @@ namespace STDEXEC // a CUDA stream operation completes a receiver that is not a stream receiver from a // kernel. And the algorithm's handlers need not be callable there.) template - struct __nested_receiver_archetype : __receiver_archetype<_Env> + struct __receiver_archetype : STDEXEC::__receiver_archetype<_Env> { - __nested_receiver_archetype() = default; + __receiver_archetype() = default; - template STDEXEC_ATTRIBUTE(host, device) - constexpr explicit __nested_receiver_archetype(_State *) noexcept - {} + constexpr explicit __receiver_archetype(void *) noexcept {} }; // (A class keyed only by whether the state's receiver is an archetype, so that the debug @@ -653,10 +578,10 @@ namespace STDEXEC struct __receiver_of { template - using __f = __nested_receiver<_Algo, - _Key, - _State, - __facade::__env_t<_Algo, _Key, _Sender, env_of_t<_Receiver>>>; + using __f = __receiver<_Algo, + _Key, + _State, + __facade::__env_t<_Algo, _Key, _Sender, env_of_t<_Receiver>>>; }; template <> @@ -664,7 +589,7 @@ namespace STDEXEC { template using __f = - __nested_receiver_archetype<__facade::__env_t<_Algo, _Key, _Sender, env_of_t<_Receiver>>>; + __receiver_archetype<__facade::__env_t<_Algo, _Key, _Sender, env_of_t<_Receiver>>>; }; // The receiver to which an algorithm connects an operation it stores in its state @@ -846,42 +771,22 @@ namespace STDEXEC template STDEXEC_ATTRIBUTE(always_inline, host, device) constexpr auto operator()(_State &__state, _Child &&__child_sndr, _Rcvr &&__rcvr) const - noexcept(noexcept(_Algo::__connect(__child<_Idx>(), - __state, - static_cast<_Child &&>(__child_sndr), - static_cast<_Rcvr &&>(__rcvr)))) - -> decltype(_Algo::__connect(__child<_Idx>(), - __state, - static_cast<_Child &&>(__child_sndr), - static_cast<_Rcvr &&>(__rcvr))) - { - return _Algo::__connect(__child<_Idx>(), - __state, - static_cast<_Child &&>(__child_sndr), - static_cast<_Rcvr &&>(__rcvr)); - } + STDEXEC_AUTO_RETURN(_Algo::__connect(__child<_Idx>(), + __state, + static_cast<_Child &&>(__child_sndr), + static_cast<_Rcvr &&>(__rcvr))) }; template struct __attach_child_fn<_Algo, _Idx, true> { template - STDEXEC_ATTRIBUTE(always_inline) + STDEXEC_ATTRIBUTE(always_inline, host, device) constexpr auto operator()(_State &__state, _Child &&__child_sndr, _Rcvr &&__rcvr) const - noexcept(noexcept(_Algo::__subscribe(__child<_Idx>(), - __state, - static_cast<_Child &&>(__child_sndr), - static_cast<_Rcvr &&>(__rcvr)))) - -> decltype(_Algo::__subscribe(__child<_Idx>(), - __state, - static_cast<_Child &&>(__child_sndr), - static_cast<_Rcvr &&>(__rcvr))) - { - return _Algo::__subscribe(__child<_Idx>(), - __state, - static_cast<_Child &&>(__child_sndr), - static_cast<_Rcvr &&>(__rcvr)); - } + STDEXEC_AUTO_RETURN(_Algo::__subscribe(__child<_Idx>(), + __state, + static_cast<_Child &&>(__child_sndr), + static_cast<_Rcvr &&>(__rcvr))) }; // The function object that attaches the child keyed by __child<_Idx> of a sender of type diff --git a/include/stdexec/__detail/__subscribe.hpp b/include/stdexec/__detail/__subscribe.hpp index caf7ff8fd..98cbc9ae9 100644 --- a/include/stdexec/__detail/__subscribe.hpp +++ b/include/stdexec/__detail/__subscribe.hpp @@ -93,8 +93,7 @@ namespace STDEXEC // function, the static one taking precedence. Senders defined in stdexec can // 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.) + // constexpr functions that it calls, which can use types that are incomplete then.) struct __subscribe_t { template diff --git a/include/stdexec/__detail/__task_scheduler.hpp b/include/stdexec/__detail/__task_scheduler.hpp index 2bb652d17..608ef0ed4 100644 --- a/include/stdexec/__detail/__task_scheduler.hpp +++ b/include/stdexec/__detail/__task_scheduler.hpp @@ -626,7 +626,7 @@ namespace STDEXEC // 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.) + // that are incomplete then.) template static constexpr auto __opstate_type(_Sndr& __sndr) noexcept { From 5bdfc45030499cb746628082e4a2c322b001bf0b Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Sat, 5 Dec 2026 12:03:38 -0800 Subject: [PATCH 3/3] give `use_sender`'s sender's `connect` a declared return type --- include/exec/asio/use_sender.hpp | 2 ++ include/stdexec/__detail/__starts_on.hpp | 1 - include/stdexec/__detail/__variant.hpp | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/exec/asio/use_sender.hpp b/include/exec/asio/use_sender.hpp index c16e08f43..ca9054e1c 100644 --- a/include/exec/asio/use_sender.hpp +++ b/include/exec/asio/use_sender.hpp @@ -143,6 +143,7 @@ namespace experimental::execution::asio template <::STDEXEC::receiver Receiver> constexpr auto connect(Receiver rcvr) const & noexcept( ::STDEXEC::__nothrow_connectable>) + -> ::STDEXEC::connect_result_t> { return ::STDEXEC::connect(sndr_, receiver{static_cast(rcvr)}); } @@ -150,6 +151,7 @@ namespace experimental::execution::asio template <::STDEXEC::receiver Receiver> constexpr auto connect(Receiver rcvr) && noexcept( ::STDEXEC::__nothrow_connectable>) + -> ::STDEXEC::connect_result_t> { return ::STDEXEC::connect(static_cast(sndr_), receiver{static_cast(rcvr)}); diff --git a/include/stdexec/__detail/__starts_on.hpp b/include/stdexec/__detail/__starts_on.hpp index 31a8a7b08..0d03a1e0c 100644 --- a/include/stdexec/__detail/__starts_on.hpp +++ b/include/stdexec/__detail/__starts_on.hpp @@ -32,7 +32,6 @@ import stdexec; # include "__sender_facade.hpp" # include "__sender_introspection.hpp" # include "__senders.hpp" -# include "__utility.hpp" # include "__prologue.hpp" diff --git a/include/stdexec/__detail/__variant.hpp b/include/stdexec/__detail/__variant.hpp index e2f48fd2a..4f8be625b 100644 --- a/include/stdexec/__detail/__variant.hpp +++ b/include/stdexec/__detail/__variant.hpp @@ -31,7 +31,6 @@ import stdexec; # if !STDEXEC_USE_MODULES() # include # include -# include # include # endif