diff --git a/include/exec/sequence/merge_each.hpp b/include/exec/sequence/merge_each.hpp index 4315fd3ee..377246d10 100644 --- a/include/exec/sequence/merge_each.hpp +++ b/include/exec/sequence/merge_each.hpp @@ -774,6 +774,10 @@ namespace experimental::execution struct _MERGE_WITH_REQUIRES_A_SEQUENCE_OF_SEQUENCES_ {}; + template + struct __types + {}; + template struct __value_completions_error { @@ -1248,59 +1252,162 @@ namespace experimental::execution return make_sequence_expr(__(), static_cast<_Sequence&&>(__sequence)); } + // Concatenates and uniques lists of types (or propagates errors). + static consteval auto __mk_concat_lists() + { + return [](_Lists...) + { + return STDEXEC::__minvoke>>, + _Lists...>(); + }; + } + + // Given the arguments of a set_value completion of an item sender of the + // sequence of sequences, returns a list of the nested sequence types, or + // an error. + template + static consteval auto __mk_nested_sequence() + { + return [](set_value_t (*)(_Args...)) + { + if constexpr (sizeof...(_Args) == 1 && (has_sequence_item_types<_Args, _Env...> && ...)) + { + return __types<_Args...>(); + } + else if constexpr (sizeof...(_Env) == 0 && sizeof...(_Args) == 1 + && (dependent_sender<_Args> && ...)) + { + return STDEXEC::__throw_compile_time_error...>(); + } + else + { + return STDEXEC::__throw_compile_time_error< + _WHAT_(_INVALID_ARGUMENT_), + _WHERE_(_IN_ALGORITHM_, merge_each_t), + _WHY_(_MERGE_WITH_REQUIRES_A_SEQUENCE_OF_SEQUENCES_), + __fn_t<_WITH_ENVIRONMENT_, _Env>..., + _WITH_ARGUMENTS_(_Args...)>(); + } + }; + } + + template + static consteval auto __mk_item_nested_sequences() + { + return []() + { + auto __sigs = STDEXEC::get_completion_signatures<_ItemSender, _Env...>(); + STDEXEC_IF_OK(__sigs) + { + return __sigs.__select(set_value).__transform_reduce(__mk_nested_sequence<_Env...>(), + __mk_concat_lists()); + } + }; + } + + // Returns a list of the (unique) types of all the nested sequences, or an + // error. _Env is the augmented environment. template - static consteval auto get_item_types() + static consteval auto __nested_sequences() { - static_assert(sender_for<_Self, merge_each_t>); - using __result_t = - __compute::__nested_values_t<__child_of<_Self>, - __env_with_inplace_stop_token_result_t<_Env>...>; - if constexpr (__ok<__result_t>) + auto __items = exec::get_item_types<__child_of<_Self>, _Env...>(); + STDEXEC_IF_OK(__items) { - return __result_t(); + return __items.__transform(__mk_item_nested_sequences<_Env...>(), __mk_concat_lists()); } - else + } + + template + static consteval auto __nested_items_of() + { + auto __items = exec::get_item_types<_Sequence, _Env...>(); + STDEXEC_IF_OK(__items) { - return exec::__invalid_item_types(__result_t()); + return STDEXEC::__mapply, decltype(__items)>(); } } - template - struct __completions_fn + // Returns a list of the types of all the item senders of all the nested + // sequences, or an error. + template + static consteval auto __nested_values(__types<_Sequences...>) { - template - using __f = __minvoke_q< - __concat_completion_signatures_t, - // merge_each can always complete with set_value() (when - // all the nested sequences complete normally), with - // set_stopped(), and with an exception_ptr (errors from - // the sequence of sequences and failures to connect or - // start the error item are stored as one). - completion_signatures, - __sequence_aware_completion_signatures_of_t<__child_of<_Self>, _Env...>, - __sequence_aware_completion_signatures_of_t<_Sequences, _Env...>...>; - }; + return __mk_concat_lists()(__nested_items_of<_Sequences, _Env...>()...); + } - template - using __completions_t = __mapply<__completions_fn<_Self, _Env...>, - __compute::__nested_sequences_t<__child_of<_Self>, _Env...>>; + // The completion signatures of all the senders in the list (or an error). + template + static consteval auto __completions_of(__types<_Senders...>) + { + return exec::concat_completion_signatures( + STDEXEC::get_completion_signatures<_Senders, _Env...>()...); + } template - static consteval auto get_completion_signatures() + static consteval auto get_item_types() { static_assert(sender_for<_Self, merge_each_t>); - // TODO: update this to use constant evaluation: - using __result_t = __minvoke<__mtry_q<__completions_t>, - _Self, - __env_with_inplace_stop_token_result_t<_Env>...>; - if constexpr (__ok<__result_t>) + using __child_t = __child_of<_Self>; + + auto __seqs = __nested_sequences<_Self, __env_with_inplace_stop_token_result_t<_Env>...>(); + STDEXEC_IF_OK(__seqs) { - static_assert(STDEXEC::__well_formed_completions<__result_t>); - return __result_t(); + auto __values = __nested_values<__env_with_inplace_stop_token_result_t<_Env>...>(__seqs); + STDEXEC_IF_OK(__values) + { + // All the errors that can be emitted on the output sequence: those + // of the sequence of sequences' item senders, of the nested + // sequences, and of the nested sequences' item senders. + auto __item_sigs = + exec::get_item_types<__child_t, __env_with_inplace_stop_token_result_t<_Env>...>() + .__transform( + __mk_item_completions<__env_with_inplace_stop_token_result_t<_Env>...>(), + exec::concat_completion_signatures); + auto __all_sigs = exec::concat_completion_signatures( + __item_sigs, + __completions_of<__env_with_inplace_stop_token_result_t<_Env>...>(__seqs), + __completions_of<__env_with_inplace_stop_token_result_t<_Env>...>(__values)); + STDEXEC_IF_OK(__all_sigs) + { + using __error_variant_t = STDEXEC::__mapply_q< + STDEXEC::__uniqued_variant, + STDEXEC::__minvoke< + STDEXEC::__mconcat>, + __types, + STDEXEC::__error_types_t>>>; + return __mapply<__compute::__nested_values_fn<__error_variant_t>, + decltype(__values)>(); + } + } } - else + } + + template + static consteval auto __mk_item_completions() + { + return []() + { + return STDEXEC::get_completion_signatures<_ItemSender, _Env...>(); + }; + } + + template + static consteval auto get_completion_signatures() + { + static_assert(sender_for<_Self, merge_each_t>); + auto __seqs = __nested_sequences<_Self, __env_with_inplace_stop_token_result_t<_Env>...>(); + STDEXEC_IF_OK(__seqs) { - return STDEXEC::__throw_compile_time_error(__result_t()); + // merge_each can always complete with set_value() (when all the + // nested sequences complete normally), with set_stopped(), and with + // an exception_ptr (errors from the sequence of sequences and + // failures to connect or start the error item are stored as one). + return exec::concat_completion_signatures( + completion_signatures(), + STDEXEC::get_completion_signatures<__child_of<_Self>, + __env_with_inplace_stop_token_result_t<_Env>...>(), + __completions_of<__env_with_inplace_stop_token_result_t<_Env>...>(__seqs)); } } diff --git a/include/exec/sequence_senders.hpp b/include/exec/sequence_senders.hpp index 1a3c817cd..87a085f94 100644 --- a/include/exec/sequence_senders.hpp +++ b/include/exec/sequence_senders.hpp @@ -842,11 +842,7 @@ namespace experimental::execution consteval auto __sequence_completion_signatures_of() { auto __item_types = get_item_types<_Sequence, _Env...>(); - if constexpr (STDEXEC::__merror) - { - return exec::__invalid_item_types(__item_types); - } - else + STDEXEC_IF_OK(__item_types) { return exec::concat_completion_signatures( exec::__sequence_completion_signatures<_Sequence, _Env...>(), diff --git a/include/stdexec/__detail/__completion_signatures.hpp b/include/stdexec/__detail/__completion_signatures.hpp index fb16d9ea6..6b3f3c82d 100644 --- a/include/stdexec/__detail/__completion_signatures.hpp +++ b/include/stdexec/__detail/__completion_signatures.hpp @@ -227,7 +227,8 @@ namespace STDEXEC //! @return The result of reducing the transformed signatures. template [[nodiscard]] - static consteval auto __transform_reduce(_Transform __transform, _Reduce __reduce) + static consteval auto + __transform_reduce([[maybe_unused]] _Transform __transform, _Reduce __reduce) -> __call_result_t<_Reduce, __call_result_t<_Transform, _Sigs*>...> { return __reduce(__transform(static_cast<_Sigs*>(nullptr))...);