Skip to content

Facade: eliminate __nested_receiver for constexpr goodness - #2604

Merged
ericniebler merged 3 commits into
NVIDIA:mainfrom
ericniebler:ericniebler/facade_improvements_20261202
Dec 5, 2026
Merged

ericniebler merged 3 commits into
NVIDIA:mainfrom
ericniebler:ericniebler/facade_improvements_20261202

Conversation

@ericniebler

Copy link
Copy Markdown
Collaborator

No description provided.

RobertLeahy and others added 3 commits December 5, 2026 09:38
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 8ee3a91
("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).
also, fix a clang compile error by giving \`repeat\`'s
\`__start_iteration\` a declared return type.
@ericniebler
ericniebler merged commit 3234ff3 into NVIDIA:main Dec 5, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants