diff --git a/include/beman/execution/detail/bulk.hpp b/include/beman/execution/detail/bulk.hpp index 3d66c243..752c38d3 100644 --- a/include/beman/execution/detail/bulk.hpp +++ b/include/beman/execution/detail/bulk.hpp @@ -29,6 +29,7 @@ import beman.execution.detail.meta.combine; import beman.execution.detail.meta.unique; import beman.execution.detail.nested_sender_has_affine; import beman.execution.detail.product_type; +import beman.execution.detail.remap_attrs; import beman.execution.detail.sender; import beman.execution.detail.sender_adaptor_closure; import beman.execution.detail.sender_for; @@ -50,6 +51,7 @@ import beman.execution.detail.set_value; #include #include #include +#include #include #include #include @@ -103,12 +105,36 @@ struct bulk_transform_signatures::is_nothrow_invocable> {}; + static constexpr bool may_throw = !(... && is_nothrow::value); + using type = ::beman::execution::detail::meta::unique<::beman::execution::detail::meta::combine< ::beman::execution::completion_signatures, ::std::conditional_t< - (... && is_nothrow::value), - ::beman::execution::completion_signatures<>, - ::beman::execution::completion_signatures<::beman::execution::set_error_t(::std::exception_ptr)>>>>; + may_throw, + ::beman::execution::completion_signatures<::beman::execution::set_error_t(::std::exception_ptr)>, + ::beman::execution::completion_signatures<>>>>; +}; + +// A value completion becomes set_error if the invocation of the function throws. +template +struct bulk_invocation_exception { + using source = ::beman::execution::set_value_t; + using output = ::beman::execution::set_error_t; + + template + static consteval auto applies() noexcept -> bool { + return ::beman::execution::detail::bulk_transform_signatures::may_throw; + } +}; + +template +struct bulk_get_attrs { + template + auto operator()(const ::beman::execution::detail::product_type&, + const auto& child) const noexcept { + return ::beman::execution::detail::remap_attrs< + ::beman::execution::detail::bulk_invocation_exception>(child); + } }; template @@ -176,6 +202,8 @@ struct bulk_algo_t { } struct impls_for : ::beman::execution::detail::default_impls { + static constexpr ::beman::execution::detail::bulk_get_attrs get_attrs{}; + struct complete_impl { template (f)); } + // bulk is lowered to bulk_chunked calling f for each index: it throws when bulk_unchunked would. + struct impls_for : ::beman::execution::detail::default_impls { + static constexpr ::beman::execution::detail::bulk_get_attrs get_attrs{}; + }; + template <::beman::execution::detail::sender_for Sender, typename Env> static auto transform_sender(::beman::execution::set_value_t, Sender&& sndr, const Env&) { auto data = ::beman::execution::detail::forward_like(sndr.template get<1>()); diff --git a/include/beman/execution/detail/completion_signatures.hpp b/include/beman/execution/detail/completion_signatures.hpp index d27f6380..f1c71f65 100644 --- a/include/beman/execution/detail/completion_signatures.hpp +++ b/include/beman/execution/detail/completion_signatures.hpp @@ -9,6 +9,7 @@ import std; #else #include +#include #include #endif #ifdef BEMAN_HAS_MODULES @@ -38,11 +39,11 @@ struct completion_signatures { using type = Tag; }; template - static constexpr bool count_of(Tag) { - return (::std::same_as::type, Tag> + ... + 0); + static constexpr auto count_of(Tag) noexcept -> ::std::size_t { + return (::std::same_as::type, Tag> + ... + 0uz); } template - static constexpr void for_each(Fun&& fun) { + static constexpr auto for_each(Fun&& fun) noexcept -> void { (fun(static_cast(nullptr)), ...); } }; diff --git a/include/beman/execution/detail/hide_completion_queries.hpp b/include/beman/execution/detail/hide_completion_queries.hpp index b185e563..7086b155 100644 --- a/include/beman/execution/detail/hide_completion_queries.hpp +++ b/include/beman/execution/detail/hide_completion_queries.hpp @@ -44,7 +44,7 @@ struct hide_completion_queries { return ::beman::execution::detail::fwd_env(child_attrs).query(q, ::std::forward(args)...); } - ::std::remove_cvref_t child_attrs; + ChildAttrs child_attrs; }; } // namespace beman::execution::detail diff --git a/include/beman/execution/detail/indeterminate_domain.hpp b/include/beman/execution/detail/indeterminate_domain.hpp index bc35ae59..6afdce11 100644 --- a/include/beman/execution/detail/indeterminate_domain.hpp +++ b/include/beman/execution/detail/indeterminate_domain.hpp @@ -63,6 +63,7 @@ struct indeterminate_domain { } // namespace beman::execution +#ifndef BEMAN_EXECUTION_DETAIL_INDETERMINATE_DOMAIN_CPPM template struct std::common_type<::beman::execution::indeterminate_domain, ::beman::execution::indeterminate_domain> { @@ -72,12 +73,21 @@ struct std::common_type<::beman::execution::indeterminate_domain, template struct std::common_type<::beman::execution::indeterminate_domain, D> { - using type = ::beman::execution::detail::meta::unique<::beman::execution::indeterminate_domain>; + // ReSharper disable once CppFunctionIsNotImplemented + static auto get_common_domain() noexcept -> D + requires(sizeof...(Domains) == 0uz); + + // ReSharper disable once CppFunctionIsNotImplemented + static auto get_common_domain() noexcept + -> ::beman::execution::detail::meta::unique<::beman::execution::indeterminate_domain>; + + using type = decltype(get_common_domain()); }; template struct std::common_type> : ::std::common_type<::beman::execution::indeterminate_domain, D> {}; +#endif // ---------------------------------------------------------------------------- diff --git a/include/beman/execution/detail/into_variant.hpp b/include/beman/execution/detail/into_variant.hpp index d0a66527..08a45e78 100644 --- a/include/beman/execution/detail/into_variant.hpp +++ b/include/beman/execution/detail/into_variant.hpp @@ -18,6 +18,7 @@ import beman.execution.detail.basic_sender; import beman.execution.detail.child_type; import beman.execution.detail.completion_signatures; import beman.execution.detail.completion_signatures_for; +import beman.execution.detail.completion_signatures_of_t; import beman.execution.detail.decayed_tuple; import beman.execution.detail.default_impls; import beman.execution.detail.dependent_sender; @@ -27,10 +28,13 @@ import beman.execution.detail.env_of_t; import beman.execution.detail.error_types_of_t; import beman.execution.detail.forward_like; import beman.execution.detail.fwd_env; +import beman.execution.detail.gather_signatures; import beman.execution.detail.impls_for; import beman.execution.detail.make_sender; import beman.execution.detail.meta.combine; +import beman.execution.detail.meta.unique; import beman.execution.detail.nested_sender_has_affine; +import beman.execution.detail.remap_attrs; import beman.execution.detail.sender; import beman.execution.detail.sender_adaptor_closure; import beman.execution.detail.sender_in; @@ -43,6 +47,7 @@ import beman.execution.detail.variant_or_empty; #else #include #include +#include #include #include #include @@ -52,10 +57,13 @@ import beman.execution.detail.variant_or_empty; #include #include #include +#include #include #include #include +#include #include +#include #include #include #include @@ -92,7 +100,33 @@ struct into_variant_t : ::beman::execution::sender_adaptor_closure using make_error_types = ::beman::execution::completion_signatures<::beman::execution::set_error_t(E)...>; - private: + template + using variant_for = ::beman::execution::detail::gather_signatures<::beman::execution::set_value_t, + Signatures, + ::beman::execution::detail::decayed_tuple, + ::beman::execution::detail::variant_or_empty>; + + struct variant_construction_exception { + using source = ::beman::execution::set_value_t; + using output = ::beman::execution::set_error_t; + + template + static consteval auto applies() noexcept -> bool { + using variant_type = variant_for; + bool throwing = false; + Signatures::for_each([&](Tag (*)(Args...)) noexcept { + if constexpr (::std::same_as) { + using tuple_type = ::beman::execution::detail::decayed_tuple; + constexpr bool nothrow = requires { + { variant_type(tuple_type{::std::declval()...}) } noexcept; + }; + throwing |= !nothrow; + } + }); + return throwing; + } + }; + template struct get_signatures; template @@ -105,13 +139,20 @@ struct into_variant_t : ::beman::execution::sender_adaptor_closure; - using variant_type = ::beman::execution::value_types_of_t; + using signatures = ::beman::execution::completion_signatures_of_t; + using variant_type = variant_for; using value_types = ::std::conditional_t< ::std::same_as, ::beman::execution::completion_signatures<>, ::beman::execution::completion_signatures<::beman::execution::set_value_t(variant_type)>>; - using error_types = ::beman::execution::error_types_of_t; + using construction_error = + ::std::conditional_t(), + make_error_types<::std::exception_ptr>, + make_error_types<>>; + using error_types = ::beman::execution::detail::meta::unique<::beman::execution::detail::meta::combine< + ::beman::execution::error_types_of_t, + construction_error>>; using stopped_types = ::std::conditional_t<::beman::execution::sends_stopped, ::beman::execution::completion_signatures<::beman::execution::set_stopped_t()>, @@ -129,6 +170,10 @@ struct into_variant_t : ::beman::execution::sender_adaptor_closure, Env...>::get(); } struct impls_for : ::beman::execution::detail::default_impls { + static constexpr auto get_attrs = [](const auto&, const auto& child) noexcept { + return ::beman::execution::detail::remap_attrs(child); + }; + struct get_state_impl { template auto operator()(Sender&&, Receiver&&) const noexcept -> ::std::type_identity< diff --git a/include/beman/execution/detail/psched_bulk_sender.hpp b/include/beman/execution/detail/psched_bulk_sender.hpp index f2cb14ca..25a0f7de 100644 --- a/include/beman/execution/detail/psched_bulk_sender.hpp +++ b/include/beman/execution/detail/psched_bulk_sender.hpp @@ -37,6 +37,7 @@ import beman.execution.detail.meta.unique; import beman.execution.detail.operation_state; import beman.execution.detail.parallel_scheduler_replacement; import beman.execution.detail.receiver; +import beman.execution.detail.remap_attrs; import beman.execution.detail.sender; import beman.execution.detail.start; import beman.execution.detail.set_error; @@ -63,6 +64,7 @@ import beman.execution.detail.value_types_of_t; #include #include #include +#include #include #include #include @@ -240,7 +242,8 @@ struct psched_bulk_sender { constexpr auto compl_sigs = ::beman::execution::get_completion_signatures(); return ::beman::execution::detail::meta::unique<::beman::execution::detail::meta::combine< ::std::remove_cvref_t, - ::beman::execution::completion_signatures<::beman::execution::set_error_t(::std::exception_ptr)>>>{}; + ::beman::execution::completion_signatures<::beman::execution::set_error_t(::std::exception_ptr), + ::beman::execution::set_stopped_t()>>>{}; } template @@ -253,7 +256,15 @@ struct psched_bulk_sender { ::std::move(fn)}; } - auto get_env() const noexcept { return ::beman::execution::get_env(child); } + // After a value completion of the child the work is handed to the backend, which can also + // complete with an error or, when stop was requested, with stopped. + auto get_env() const noexcept { + return ::beman::execution::detail::remap_attrs< + ::beman::execution::detail:: + redirect_completion<::beman::execution::set_value_t, ::beman::execution::set_error_t, true>, + ::beman::execution::detail:: + redirect_completion<::beman::execution::set_value_t, ::beman::execution::set_stopped_t, true>>(child); + } [[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] ::std::bool_constant _; ::std::shared_ptr backend; diff --git a/include/beman/execution/detail/remap_attrs.hpp b/include/beman/execution/detail/remap_attrs.hpp new file mode 100644 index 00000000..4544d204 --- /dev/null +++ b/include/beman/execution/detail/remap_attrs.hpp @@ -0,0 +1,371 @@ +// include/beman/execution/detail/remap_attrs.hpp -*-C++-*- +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_REMAP_ATTRS +#define INCLUDED_BEMAN_EXECUTION_DETAIL_REMAP_ATTRS + +#include +#ifdef BEMAN_HAS_IMPORT_STD +import std; +#else +#include +#include +#endif +#ifdef BEMAN_HAS_MODULES +import beman.execution.detail.common_domain; +import beman.execution.detail.completion_signatures_of_t; +import beman.execution.detail.env_of_t; +import beman.execution.detail.get_completion_domain; +import beman.execution.detail.get_completion_scheduler; +import beman.execution.detail.get_env; +import beman.execution.detail.hide_completion_queries; +import beman.execution.detail.indeterminate_domain; +import beman.execution.detail.meta.filter; +import beman.execution.detail.sender_in; +import beman.execution.detail.set_error; +import beman.execution.detail.set_stopped; +import beman.execution.detail.set_value; +import beman.execution.detail.try_query; +import beman.execution.detail.type_list; +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +// ---------------------------------------------------------------------------- + +namespace beman::execution::detail { +/*! + * \brief A rule making Source a source of Output for every child. + * \headerfile beman/execution.hpp + * \internal + * + * \details + * A rule tells where the completions of an adaptor come from. It states that a child completion + * with tag source may make the adaptor complete with tag output, i.e., that + * source is one of the sources of output. A rule provides: + * - source: the tag of the child completion. + * - output: the tag the adaptor may complete with in response. + * - applies(): whether this is actually possible for a given child. + * + * redirect_completion is the rule which applies whatever the child's signatures are. It does + * not state that the adaptor completes with Output whenever the child completes with Source. + * + * \tparam Source The tag of the child completion. + * \tparam Output The tag the adaptor may complete with in response. + * \tparam KeepsSource Whether Source also remains a source of itself, i.e., whether the adaptor + * may still complete with Source in response to a child completion with tag Source. + */ +template +struct redirect_completion { + using source = Source; + using output = Output; + + template + static consteval auto applies() noexcept -> bool { + return true; + } +}; +} // namespace beman::execution::detail + +namespace beman::execution::detail::remapped { +/*! + * \brief Whether Source remains a source of itself in the presence of Rule. + * \headerfile beman/execution.hpp + * \internal + */ +template +inline constexpr bool rule_keeps_source = true; +template +inline constexpr bool + rule_keeps_source<::beman::execution::detail::redirect_completion, Source> = false; + +/*! + * \brief Whether Rule is about Source being a source of Output. + * \headerfile beman/execution.hpp + * \internal + */ +template +concept rule_matches = ::std::same_as && ::std::same_as; + +/*! + * \brief Whether Rule makes Source a source of Output for Child. + * \headerfile beman/execution.hpp + * \internal + * + * \details + * Rule::applies is only instantiated for a matching rule. + * + * \tparam Signatures The completion signatures of Child in the environments Env.... + */ +template +consteval auto rule_applies() noexcept -> bool { + if constexpr (::beman::execution::detail::remapped::rule_matches) { + return Rule::template applies(); + } else { + return false; + } +} +} // namespace beman::execution::detail::remapped + +// ---------------------------------------------------------------------------- + +namespace beman::execution::detail::remapped { +template +struct attrs; + +/*! + * \brief The child completion tag whose scheduler Attrs reports for Tag. + * \headerfile beman/execution.hpp + * \internal + * + * \details + * This is Tag itself unless Attrs is a remapped::attrs: that answers the query + * by asking its child, possibly using a different tag, or not at all, in which case the origin is void. + */ +template +struct scheduler_origin : ::std::type_identity {}; + +template +struct scheduler_origin<::beman::execution::detail::remapped::attrs, Tag, Env...> + : ::std::type_identity:: + template scheduler_origin> {}; + +template +using scheduler_origin_t = typename ::beman::execution::detail::remapped::scheduler_origin::type; + +/*! + * \brief The source to ask Attrs for the completion scheduler. + * \headerfile beman/execution.hpp + * \internal + * + * \details + * The type is void unless all sources have the same, known scheduler_origin. + * + * \tparam Sources A type_list of the completion tags which are the sources of the output. + */ +template +struct scheduler_source : ::std::type_identity {}; + +template +struct scheduler_source, Env...> + : ::std::conditional< + !::std::is_void_v<::beman::execution::detail::remapped::scheduler_origin_t> && + (::std::same_as<::beman::execution::detail::remapped::scheduler_origin_t, + ::beman::execution::detail::remapped::scheduler_origin_t> && + ...), + Source, + void> {}; + +/*! + * \brief The completion domain attrs reports for the completion tag Source. + * \headerfile beman/execution.hpp + * \internal + * + * \details + * Without an environment a missing domain is reported as no_completion_domain. With an + * environment the domain of the source's completion scheduler is used instead or else + * indeterminate_domain<>. + * + * \return A default constructed object of the domain's type. + */ +template +auto source_domain(const Attrs& attrs, const Env&... env) noexcept { + if constexpr (requires { ::beman::execution::get_completion_domain(attrs, env...); }) { + return ::beman::execution::get_completion_domain(attrs, env...); + } else if constexpr (sizeof...(Env) == 0) { + return ::beman::execution::detail::no_completion_domain{}; + } else if constexpr (requires { + ::beman::execution::get_completion_domain<::beman::execution::set_value_t>( + ::beman::execution::get_completion_scheduler(attrs, env...), env...); + }) { + return ::beman::execution::get_completion_domain<::beman::execution::set_value_t>( + ::beman::execution::get_completion_scheduler(attrs, env...), env...); + } else { + return ::beman::execution::indeterminate_domain<>{}; + } +} + +/*! + * \brief The common completion domain attrs reports for all Source.... + * \headerfile beman/execution.hpp + * \internal + * + * \return An object of the common_domain of the sources' domains or no_completion_domain + * if there is no source or a source has no domain. + */ +template +auto sources_domain(const Attrs& attrs, ::beman::execution::detail::type_list, const Env&... env) noexcept { + constexpr bool missing = + (::std::same_as(attrs, env...)), + ::beman::execution::detail::no_completion_domain> || + ...); + if constexpr (sizeof...(Source) == 0 || missing) { + return ::beman::execution::detail::no_completion_domain{}; + } else { + return ::beman::execution::detail::common_domain( + ::beman::execution::detail::remapped::source_domain(attrs, env...)...); + } +} + +/*! + * \brief Derives a single-child adaptor's completion attributes from the child's attributes. + * \headerfile beman/execution.hpp + * \internal + * + * \details + * Rules describe where the adaptor's completions come from, see + * redirect_completion. A child completion keeps its tag unless it is the + * Source of a redirect_completion. + * + * A query for an output tag is answered from its sources, i.e., the child completion tags which + * can produce that output: + * - The sources are determined from the child's completion signatures in the supplied + * environment. If the signatures are not available, a query without an environment assumes + * that every child completion may occur and a query with an environment has no sources. + * - The completion scheduler is the child's scheduler for a source, provided all sources have + * the same scheduler_origin: equal scheduler types do not imply equal schedulers. + * - The completion domain is the common_domain of the sources' domains. + * - The child's untagged completion domain is forwarded unless a rule turns a completion with + * another tag into set_value. + * - Other forwarding queries are passed to the child. + * + * \tparam Child The type of the adaptor's child sender. + * \tparam Rules The rules describing where the adaptor's completions come from. + */ +template +struct attrs : ::beman::execution::detail::hide_completion_queries<::beman::execution::env_of_t> { + using base = ::beman::execution::detail::hide_completion_queries<::beman::execution::env_of_t>; + using base::query; + + private: + using attrs_t = ::std::remove_cvref_t<::beman::execution::env_of_t>; + + /*! + * \brief Whether Source is a source of Output for the adaptor. + */ + template + static consteval auto is_source() noexcept -> bool { + constexpr bool kept = ::std::same_as && + (::beman::execution::detail::remapped::rule_keeps_source && ...); + constexpr bool ruled = (::beman::execution::detail::remapped::rule_matches || ...); + + if constexpr (!kept && !ruled) { + return false; + } else if constexpr (!::beman::execution::sender_in) { + // Before the receiver environment is known, assume every channel may occur. + return sizeof...(Env) == 0; + } else { + using signatures = ::beman::execution::completion_signatures_of_t; + if constexpr (signatures::count_of(Source{}) == 0u) { + return false; + } else if constexpr (kept) { + return true; + } else { + return (::beman::execution::detail::remapped:: + rule_applies() || + ...); + } + } + } + + template + struct sources { + template + using contributes = ::std::bool_constant()>; + + using type = ::beman::execution::detail::meta::filter< + contributes, + ::beman::execution::detail::type_list<::beman::execution::set_value_t, + ::beman::execution::set_error_t, + ::beman::execution::set_stopped_t>>; + }; + + template + using sources_t = typename sources::type; + + template + using scheduler_source_t = typename ::beman::execution::detail::remapped:: + scheduler_source, Env...>::type; + + public: + /*! + * \brief The child completion tag whose scheduler is reported for Output. + * \see remapped::scheduler_origin + */ + template + using scheduler_origin = + ::beman::execution::detail::remapped::scheduler_origin_t, Env...>; + + /*! + * \brief The completion scheduler for Output: the child's scheduler for the sources' common origin. + * \return The scheduler or void if the sources don't have the same, known origin. + */ + template + auto query(::beman::execution::get_completion_scheduler_t, const Env&... env) const noexcept { + using source = scheduler_source_t; + // Without a scheduler the result is void instead of the query being ill-formed: that + // prevents try_query from retrying without the environment. + if constexpr (!::std::is_void_v && + requires { ::beman::execution::get_completion_scheduler(this->child_attrs, env...); }) { + return ::beman::execution::get_completion_scheduler(this->child_attrs, env...); + } + } + + /*! + * \brief The completion domain for Output: the common domain of its sources. + */ + template + requires(!::std::same_as) + auto query(::beman::execution::get_completion_domain_t, const Env&... env) const noexcept { + return ::beman::execution::detail::remapped::sources_domain( + this->child_attrs, sources_t{}, env...); + } + + /*! + * \brief The child's untagged completion domain, unless a rule turns another tag into set_value. + */ + template + requires((::std::same_as || + !::std::same_as) && + ...) && + requires(const attrs_t& attrs, const Env&... env) { + ::beman::execution::detail::try_query(attrs, ::beman::execution::get_completion_domain<>, env...); + } + auto query(::beman::execution::get_completion_domain_t<>, const Env&... env) const noexcept { + return ::beman::execution::detail::try_query( + this->child_attrs, ::beman::execution::get_completion_domain<>, env...); + } +}; +} // namespace beman::execution::detail::remapped + +namespace beman::execution::detail { +/*! + * \brief Create the completion attributes of an adaptor from its child sender. + * \headerfile beman/execution.hpp + * \internal + * + * \tparam Rules The rules describing where the adaptor's completions come from, specified explicitly. + * \param child The adaptor's child sender. + * \return A remapped::attrs holding the child's attributes. + */ +template +auto remap_attrs(const Child& child) noexcept { + return ::beman::execution::detail::remapped::attrs{::beman::execution::get_env(child)}; +} +} // namespace beman::execution::detail + +#endif // INCLUDED_BEMAN_EXECUTION_DETAIL_REMAP_ATTRS diff --git a/include/beman/execution/detail/stop_when.hpp b/include/beman/execution/detail/stop_when.hpp index 797f6b0d..64510ed0 100644 --- a/include/beman/execution/detail/stop_when.hpp +++ b/include/beman/execution/detail/stop_when.hpp @@ -8,7 +8,6 @@ #ifdef BEMAN_HAS_IMPORT_STD import std; #else -#include #include #include #include @@ -88,10 +87,7 @@ struct beman::execution::detail::stop_when_t::sender { struct cb_t { ::beman::execution::inplace_stop_source& source; - auto operator()() const noexcept { - ::std::cout << "stop_when: stop requested\n" << ::std::flush; - this->source.request_stop(); - } + auto operator()() const noexcept { this->source.request_stop(); } }; struct env { base_state* st; diff --git a/include/beman/execution/detail/stopped_as_error.hpp b/include/beman/execution/detail/stopped_as_error.hpp index e6619171..6c014fcc 100644 --- a/include/beman/execution/detail/stopped_as_error.hpp +++ b/include/beman/execution/detail/stopped_as_error.hpp @@ -15,33 +15,50 @@ import std; #ifdef BEMAN_HAS_MODULES import beman.execution.detail.child_type; import beman.execution.detail.data_type; +import beman.execution.detail.default_impls; import beman.execution.detail.forward_like; import beman.execution.detail.just; import beman.execution.detail.let; import beman.execution.detail.make_sender; import beman.execution.detail.movable_value; import beman.execution.detail.nested_sender_has_affine; +import beman.execution.detail.remap_attrs; import beman.execution.detail.sender; import beman.execution.detail.sender_adaptor_closure; import beman.execution.detail.sender_for; +import beman.execution.detail.set_error; +import beman.execution.detail.set_stopped; import beman.execution.detail.set_value; #else #include #include +#include #include #include #include #include #include #include +#include #include #include #include +#include +#include #include #endif namespace beman::execution::detail { struct stopped_as_error_t { + struct impls_for : ::beman::execution::detail::default_impls { + static constexpr auto get_attrs = [](const auto&, const auto& child) noexcept { + return ::beman::execution::detail::remap_attrs< + ::beman::execution::detail:: + redirect_completion<::beman::execution::set_stopped_t, ::beman::execution::set_error_t, false>>( + child); + }; + }; + template <::beman::execution::detail::sender_for Sndr, typename Env> auto transform_sender(::beman::execution::set_value_t, Sndr&& sndr, Env&&) const { auto&& data = sndr.template get<1>(); diff --git a/include/beman/execution/detail/stopped_as_optional.hpp b/include/beman/execution/detail/stopped_as_optional.hpp index 55615d71..362040b5 100644 --- a/include/beman/execution/detail/stopped_as_optional.hpp +++ b/include/beman/execution/detail/stopped_as_optional.hpp @@ -10,36 +10,47 @@ #ifdef BEMAN_HAS_IMPORT_STD import std; #else +#include #include #include #include #endif #ifdef BEMAN_HAS_MODULES import beman.execution.detail.child_type; +import beman.execution.detail.default_impls; +import beman.execution.detail.env; import beman.execution.detail.forward_like; import beman.execution.detail.fwd_env; import beman.execution.detail.just; import beman.execution.detail.let; import beman.execution.detail.make_sender; import beman.execution.detail.nested_sender_has_affine; +import beman.execution.detail.remap_attrs; import beman.execution.detail.sender; import beman.execution.detail.sender_adaptor_closure; import beman.execution.detail.sender_for; +import beman.execution.detail.set_error; +import beman.execution.detail.set_stopped; import beman.execution.detail.set_value; import beman.execution.detail.single_sender; import beman.execution.detail.single_sender_value_type; import beman.execution.detail.then; #else #include +#include +#include #include #include #include #include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -48,6 +59,36 @@ import beman.execution.detail.then; namespace beman::execution::detail { struct stopped_as_optional_t : ::beman::execution::sender_adaptor_closure { + private: + struct value_construction_exception { + using source = ::beman::execution::set_value_t; + using output = ::beman::execution::set_error_t; + + template > + static consteval auto applies() noexcept -> bool { + bool throwing = false; + if constexpr (requires { typename ::beman::execution::detail::single_sender_value_type; }) { + using value_type = ::beman::execution::detail::single_sender_value_type; + Signatures::for_each([&](Tag (*)(Args...)) noexcept { + if constexpr (::std::same_as) { + throwing |= !::std::is_nothrow_constructible_v; + } + }); + } + return throwing; + } + }; + + public: + struct impls_for : ::beman::execution::detail::default_impls { + static constexpr auto get_attrs = [](const auto&, const auto& child) noexcept { + return ::beman::execution::detail::remap_attrs< + ::beman::execution::detail:: + redirect_completion<::beman::execution::set_stopped_t, ::beman::execution::set_value_t, false>, + value_construction_exception>(child); + }; + }; + template <::beman::execution::detail::sender_for Sndr, typename Env> auto transform_sender(::beman::execution::set_value_t, Sndr&& sndr, Env&&) const { static_assert( diff --git a/include/beman/execution/detail/then.hpp b/include/beman/execution/detail/then.hpp index de995371..198ae062 100644 --- a/include/beman/execution/detail/then.hpp +++ b/include/beman/execution/detail/then.hpp @@ -37,6 +37,7 @@ import beman.execution.detail.meta.transform; import beman.execution.detail.meta.unique; import beman.execution.detail.movable_value; import beman.execution.detail.nested_sender_has_affine; +import beman.execution.detail.remap_attrs; import beman.execution.detail.sender; import beman.execution.detail.sender_adaptor_closure; import beman.execution.detail.sender_in; @@ -66,6 +67,7 @@ import beman.execution.detail.value_signature; #include #include #include +#include #include #include #include @@ -99,17 +101,13 @@ struct then_transform_t { template struct then_exception_fun : ::std::false_type {}; template -struct then_exception_fun - : ::std::bool_constant()(::std::declval()...))> {}; +struct then_exception_fun : ::std::bool_constant> {}; template struct then_exception : ::std::false_type {}; -template -struct then_exception> { - static constexpr bool value{ - then_exception_fun::value || - then_exception>::value}; -}; +template +struct then_exception> + : ::std::disjunction...> {}; template struct then_t { @@ -135,6 +133,17 @@ struct then_t { } private: + template + struct invocation_exception { + using source = Completion; + using output = ::beman::execution::set_error_t; + + template + static consteval auto applies() noexcept -> bool { + return ::beman::execution::detail::then_exception::value; + } + }; + template struct get_signatures; @@ -167,26 +176,28 @@ struct then_t { } struct impls_for : ::beman::execution::detail::default_impls { + static constexpr auto get_attrs = [](const Fun&, const auto& child) noexcept { + return ::beman::execution::detail::remap_attrs< + ::beman::execution::detail::redirect_completion, + invocation_exception>(child); + }; + // NOLINTBEGIN(bugprone-exception-escape) struct complete_impl { - template - auto operator()(auto, auto& fun, auto& receiver, Tag, Args&&... args) const noexcept -> void { + template + auto operator()(auto, Fun& fun, auto& receiver, Tag, Args&&... args) const noexcept -> void { if constexpr (::std::same_as) { BEMAN_EXECUTION_TRY { - auto invoke = [&] { return ::std::invoke(::std::move(fun), ::std::forward(args)...); }; - if constexpr (::std::same_as) { - invoke(); + if constexpr (::std::is_void_v<::std::invoke_result_t>) { + ::std::invoke(::std::move(fun), ::std::forward(args)...); ::beman::execution::set_value(::std::move(receiver)); } else { - ::beman::execution::set_value(::std::move(receiver), invoke()); + ::beman::execution::set_value( + ::std::move(receiver), ::std::invoke(::std::move(fun), ::std::forward(args)...)); } } BEMAN_EXECUTION_CATCH(...) { - if constexpr (not noexcept(::std::invoke(::std::move(fun), ::std::forward(args)...) - - )) { - static_assert(noexcept( - ::beman::execution::set_error(::std::move(receiver), ::std::current_exception()))); + if constexpr (!::std::is_nothrow_invocable_v) { ::beman::execution::set_error(::std::move(receiver), ::std::current_exception()); } } diff --git a/include/beman/execution/execution.hpp b/include/beman/execution/execution.hpp index c818a760..cd0c0994 100644 --- a/include/beman/execution/execution.hpp +++ b/include/beman/execution/execution.hpp @@ -40,6 +40,7 @@ import beman.execution.detail.get_scheduler_allocator; import beman.execution.detail.get_start_scheduler; import beman.execution.detail.get_stop_token; import beman.execution.detail.indeterminate_domain; +import beman.execution.detail.inlinable_receiver; import beman.execution.detail.inline_scheduler; import beman.execution.detail.into_variant; import beman.execution.detail.just; @@ -68,7 +69,6 @@ import beman.execution.detail.set_value; import beman.execution.detail.simple_counting_scope; import beman.execution.detail.spawn_future; import beman.execution.detail.spawn; -//-dk:TODO? import beman.execution.detail.split; import beman.execution.detail.start; import beman.execution.detail.starts_on; import beman.execution.detail.stopped_as_error; @@ -119,6 +119,7 @@ import beman.execution.detail.write_env; #include #include #include +#include #include #include #include @@ -147,7 +148,6 @@ import beman.execution.detail.write_env; #include #include #include -//-dk:TODO? #include #include #include #include diff --git a/src/beman/execution/CMakeLists.txt b/src/beman/execution/CMakeLists.txt index f0571070..92a1ddec 100644 --- a/src/beman/execution/CMakeLists.txt +++ b/src/beman/execution/CMakeLists.txt @@ -169,6 +169,7 @@ target_sources( ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/read_env.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/receiver.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/receiver_of.hpp + ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/remap_attrs.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/run_loop.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/sched_env.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/schedule.hpp @@ -407,6 +408,7 @@ if(BEMAN_USE_MODULES) read_env.cppm receiver_of.cppm receiver.cppm + remap_attrs.cppm run_loop.cppm sched_env.cppm schedule_from.cppm diff --git a/src/beman/execution/get_completion_domain.cppm b/src/beman/execution/get_completion_domain.cppm index 98d1fc8b..56a7b66f 100644 --- a/src/beman/execution/get_completion_domain.cppm +++ b/src/beman/execution/get_completion_domain.cppm @@ -6,6 +6,10 @@ module; export module beman.execution.detail.get_completion_domain; +namespace beman::execution::detail { +export using beman::execution::detail::no_completion_domain; +} // namespace beman::execution::detail + namespace beman::execution { export using beman::execution::get_completion_domain_t; export using beman::execution::get_completion_domain; diff --git a/src/beman/execution/indeterminate_domain.cppm b/src/beman/execution/indeterminate_domain.cppm index cb66d9b1..5a6f8429 100644 --- a/src/beman/execution/indeterminate_domain.cppm +++ b/src/beman/execution/indeterminate_domain.cppm @@ -2,6 +2,7 @@ module; // src/beman/execution/indeterminate_domain.cppm -*-C++-*- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#define BEMAN_EXECUTION_DETAIL_INDETERMINATE_DOMAIN_CPPM #include export module beman.execution.detail.indeterminate_domain; @@ -9,3 +10,27 @@ export module beman.execution.detail.indeterminate_domain; namespace beman::execution { export using beman::execution::indeterminate_domain; } // namespace beman::execution + +template +struct std::common_type<::beman::execution::indeterminate_domain, + ::beman::execution::indeterminate_domain> { + using type = + ::beman::execution::detail::meta::unique<::beman::execution::indeterminate_domain>; +}; + +template +struct std::common_type<::beman::execution::indeterminate_domain, D> { + // ReSharper disable once CppFunctionIsNotImplemented + static auto get_common_domain() noexcept -> D + requires(sizeof...(Domains) == 0uz); + + // ReSharper disable once CppFunctionIsNotImplemented + static auto get_common_domain() noexcept + -> ::beman::execution::detail::meta::unique<::beman::execution::indeterminate_domain>; + + using type = decltype(get_common_domain()); +}; + +template +struct std::common_type> + : ::std::common_type<::beman::execution::indeterminate_domain, D> {}; diff --git a/src/beman/execution/remap_attrs.cppm b/src/beman/execution/remap_attrs.cppm new file mode 100644 index 00000000..6b7618ba --- /dev/null +++ b/src/beman/execution/remap_attrs.cppm @@ -0,0 +1,12 @@ +module; +// src/beman/execution/remap_attrs.cppm -*-C++-*- +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include + +export module beman.execution.detail.remap_attrs; + +namespace beman::execution::detail { +export using beman::execution::detail::remap_attrs; +export using beman::execution::detail::redirect_completion; +} // namespace beman::execution::detail diff --git a/tests/beman/execution/exec-bulk.test.cpp b/tests/beman/execution/exec-bulk.test.cpp index 53a3a1c4..77d916c7 100644 --- a/tests/beman/execution/exec-bulk.test.cpp +++ b/tests/beman/execution/exec-bulk.test.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include +#include #include #include #ifdef BEMAN_HAS_IMPORT_STD @@ -9,12 +10,14 @@ import std; #else #include #include +#include #include #include #include #include #include #include +#include #include #ifdef __cpp_lib_parallel_algorithm #include @@ -593,6 +596,54 @@ auto test_bulk_attributes() { 84, test::test_non_forwardable_attr{}, test_std::bulk_unchunked(s, test_std::seq, 1, [](int) {})); } +using test::attribute_sender; +using test::completion_domain_t; +using test::error_domain; +using test::has_completion_scheduler; +using test::stopped_domain; +using test::value_domain; + +// A callback which may throw turns a value completion into an error completion: the error +// completion then no longer comes from the error completion of the child only. +auto test_bulk_completion_attributes() -> void { + test_std::run_loop value_loop, error_loop, stopped_loop; + const auto value_scheduler = value_loop.get_scheduler(); + const auto error_scheduler = error_loop.get_scheduler(); + const auto stopped_scheduler = stopped_loop.get_scheduler(); + const auto env = test_std::env<>{}; + const auto child = attribute_sender<>{value_scheduler, error_scheduler, stopped_scheduler}; + + const auto check_unchanged = [&](const auto& sndr) { + using sender_t = std::remove_cvref_t; + const auto attrs = test_std::get_env(sndr); + ASSERT(test_std::get_completion_scheduler(attrs, env) == value_scheduler); + ASSERT(test_std::get_completion_scheduler(attrs, env) == stopped_scheduler); + static_assert(std::same_as, value_domain>); + static_assert(std::same_as, stopped_domain>); + }; + const auto check = [&](const auto& nothrow, const auto& throwing) { + using nothrow_t = std::remove_cvref_t; + using throwing_t = std::remove_cvref_t; + check_unchanged(nothrow); + check_unchanged(throwing); + + ASSERT(test_std::get_completion_scheduler(test_std::get_env(nothrow), env) == + error_scheduler); + static_assert(std::same_as, error_domain>); + + static_assert(!has_completion_scheduler>); + static_assert(std::same_as, + test_std::indeterminate_domain>); + }; + + check(child | test_std::bulk(test_std::seq, 1, [](int, int&) noexcept {}), + child | test_std::bulk(test_std::seq, 1, [](int, int&) {})); + check(child | test_std::bulk_chunked(test_std::seq, 1, [](int, int, int&) noexcept {}), + child | test_std::bulk_chunked(test_std::seq, 1, [](int, int, int&) {})); + check(child | test_std::bulk_unchunked(test_std::seq, 1, [](int, int&) noexcept {}), + child | test_std::bulk_unchunked(test_std::seq, 1, [](int, int&) {})); +} + } // namespace TEST(exec_bulk) { @@ -628,5 +679,6 @@ TEST(exec_bulk) { BEMAN_EXECUTION_CATCH(...) { ASSERT(nullptr == +"the bulk tests shouldn't throw"); } test_bulk_attributes(); + test_bulk_completion_attributes(); return 0; } diff --git a/tests/beman/execution/exec-cmplsigs.test.cpp b/tests/beman/execution/exec-cmplsigs.test.cpp index c008862d..055fe7e4 100644 --- a/tests/beman/execution/exec-cmplsigs.test.cpp +++ b/tests/beman/execution/exec-cmplsigs.test.cpp @@ -7,6 +7,7 @@ import std; #else #include +#include #include #include #endif @@ -90,6 +91,12 @@ auto test_completion_signature() { } auto test_completion_signatures() -> void { + static_assert(std::same_as); + static_assert(sender::env_signatures::count_of(test_std::set_value) == 4); + static_assert(sender::env_signatures::count_of(test_std::set_error) == 2); + static_assert(sender::env_signatures::count_of(test_std::set_stopped) == 1); + static_assert(sender::none_signatures::count_of(test_std::set_value) == 0); + test_std::completion_signatures<>(); test_std::completion_signatures(); //-dk:TODO create negative compilation test_std::completion_signatures(); diff --git a/tests/beman/execution/exec-parallel-scheduler.test.cpp b/tests/beman/execution/exec-parallel-scheduler.test.cpp index f530e9a0..b1a0e1ae 100644 --- a/tests/beman/execution/exec-parallel-scheduler.test.cpp +++ b/tests/beman/execution/exec-parallel-scheduler.test.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include +#include #include #ifdef BEMAN_HAS_IMPORT_STD import std; @@ -296,6 +297,42 @@ auto test_parallel_scheduler_exception_propagation() -> void { } #endif // __cpp_exceptions +using test::attribute_sender; +using test::completion_domain_t; +using test::error_domain; +using test::has_completion_scheduler; +using test::stopped_domain; +using test::value_domain; + +// The bulk of the parallel scheduler hands a value completion of its child to the backend, +// which may complete with an error or with stopped instead. +auto test_parallel_bulk_completion_attributes() -> void { + test_std::run_loop value_loop, error_loop, stopped_loop; + const auto value_scheduler = value_loop.get_scheduler(); + const auto env = test_std::env<>{}; + const auto child = attribute_sender<>{value_scheduler, error_loop.get_scheduler(), stopped_loop.get_scheduler()}; + const auto domain = test_std::get_completion_domain(test_std::get_parallel_scheduler()); + + const auto check = [&](const auto& sndr) { + using sender_t = std::remove_cvref_t; + ASSERT(test_std::get_completion_scheduler(test_std::get_env(sndr), env) == + value_scheduler); + static_assert(std::same_as, value_domain>); + + static_assert(!has_completion_scheduler>); + static_assert(std::same_as, + test_std::indeterminate_domain>); + static_assert(!has_completion_scheduler>); + static_assert(std::same_as, + test_std::indeterminate_domain>); + }; + + check(domain.transform_sender( + test_std::set_value, child | test_std::bulk_chunked(test_std::par, 4, [](int, int, int&) noexcept {}), env)); + check(domain.transform_sender( + test_std::set_value, child | test_std::bulk_unchunked(test_std::par, 4, [](int, int&) noexcept {}), env)); +} + } // namespace #if !BEMAN_EXECUTION_WITH_DEFAULT_PARALLEL_SCHEDULER_BACKEND @@ -312,6 +349,7 @@ TEST(exec_parallel_scheduler) { test_parallel_scheduler_synopsis(); test_replaceability_synopsis(); test_parallel_scheduler_schedule(); + test_parallel_bulk_completion_attributes(); #ifdef __cpp_exceptions test_parallel_scheduler_exception_propagation(); #endif diff --git a/tests/beman/execution/exec-snd-transform.test.cpp b/tests/beman/execution/exec-snd-transform.test.cpp index 56863d73..565f539b 100644 --- a/tests/beman/execution/exec-snd-transform.test.cpp +++ b/tests/beman/execution/exec-snd-transform.test.cpp @@ -208,6 +208,20 @@ TEST(exec_snd_transform) { ASSERT(r.value == 42); } + // [exec.domain.indeterminate]/4: only an empty pack paired with a bare domain unwraps. + using empty = test_std::indeterminate_domain<>; + using single = test_std::indeterminate_domain; + static_assert(std::same_as, special_domain>); + static_assert(std::same_as, special_domain>); + static_assert(std::same_as, single>); + static_assert(std::same_as, single>); + static_assert(std::same_as, test_std::default_domain>); + static_assert(std::same_as, test_std::default_domain>); + static_assert(std::same_as, + test_std::indeterminate_domain>); + static_assert(std::same_as, + test_std::indeterminate_domain>); + // indeterminate_domain test_indeterminate_domain_transform(); } diff --git a/tests/beman/execution/exec-stopped-opt.test.cpp b/tests/beman/execution/exec-stopped-opt.test.cpp index 5c00e668..83a0b18e 100644 --- a/tests/beman/execution/exec-stopped-opt.test.cpp +++ b/tests/beman/execution/exec-stopped-opt.test.cpp @@ -10,17 +10,28 @@ import std; #else #include #include +#include #include #endif #ifdef BEMAN_HAS_MODULES import beman.execution; import beman.execution.detail; #else +#include +#include +#include +#include +#include +#include #include #include +#include +#include #include +#include #include #include +#include #endif // ---------------------------------------------------------------------------- @@ -49,10 +60,63 @@ auto test_stopped_as_optional_attributes() { test::test_sender_env(84, test::test_non_forwardable_attr{}, test_std::stopped_as_optional(s)); } +struct stoppable_env { + test_std::inplace_stop_token token; + auto query(test_std::get_stop_token_t) const noexcept { return token; } +}; + +struct optional_receiver { + using receiver_concept = test_std::receiver_tag; + stoppable_env env; + std::optional>* result; + + auto get_env() const noexcept { return env; } + auto set_value(std::optional value) && noexcept -> void { result->emplace(std::move(value)); } + auto set_error(auto&&) && noexcept -> void { ASSERT_UNREACHABLE(); } + auto set_stopped() && noexcept -> void { ASSERT_UNREACHABLE(); } +}; + +template +concept has_value_scheduler = requires(const Sender& sender, const Env&... env) { + test_std::get_completion_scheduler(test_std::get_env(sender), env...); +}; + +auto test_stopped_as_optional_on_run_loop() -> void { + test_std::run_loop loop; + test_std::inplace_stop_source active, stopped; + const auto scheduler = loop.get_scheduler(); + auto scheduled = test_std::schedule(scheduler); + const auto attrs = test_std::get_env(scheduled); + const auto get_value_scheduler = test_std::get_completion_scheduler; + ASSERT(get_value_scheduler(attrs) == scheduler); + ASSERT(test_std::get_completion_scheduler(attrs) == scheduler); + + auto sndr = scheduled | test_std::then([]() noexcept { return 42; }) | test_std::stopped_as_optional; + static_assert(has_value_scheduler>); + ASSERT(get_value_scheduler(test_std::get_env(sndr), test_std::env<>{}) == scheduler); + // Without an unstoppable environment, both upstream tags can produce value. The mapper + // cannot infer a shared origin from the two separate scheduler queries on the run loop. + static_assert(!has_value_scheduler); + static_assert(!has_value_scheduler); + + std::optional> active_result, stopped_result; + auto active_op = test_std::connect(sndr, optional_receiver{{active.get_token()}, &active_result}); + auto stopped_op = test_std::connect(std::move(sndr), optional_receiver{{stopped.get_token()}, &stopped_result}); + test_std::start(active_op); + test_std::start(stopped_op); + ASSERT(!active_result && !stopped_result); + stopped.request_stop(); + loop.finish(); + loop.run(); + ASSERT(active_result && *active_result == 42); + ASSERT(stopped_result && !stopped_result->has_value()); +} + } // namespace // ---------------------------------------------------------------------------- TEST(exec_stopped_as_optional) { test_stopped_as_optional(); test_stopped_as_optional_attributes(); + test_stopped_as_optional_on_run_loop(); } diff --git a/tests/beman/execution/exec-then.test.cpp b/tests/beman/execution/exec-then.test.cpp index 637a2a29..b07aa419 100644 --- a/tests/beman/execution/exec-then.test.cpp +++ b/tests/beman/execution/exec-then.test.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include +#include #include #include #include @@ -9,6 +10,7 @@ import std; #else #include +#include #include #include #include @@ -22,6 +24,13 @@ import beman.execution; #include #include #include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -183,6 +192,17 @@ auto test_then_value() -> void { test_std::sync_wait(test_std::just_stopped() | test_std::upon_stopped([]() { return 17; }))); } +auto test_then_reference_result() -> void { + int value = 42; + bool received_original = false; + auto child = test_std::just() | test_std::then([&]() noexcept -> int& { return value; }); + test_then_type(child); + auto state = test_std::connect( + child | test_std::then([&](int& result) noexcept { received_original = (&result == &value); }), receiver{}); + test_std::start(state); + ASSERT(received_original); +} + struct memory_env { std::pmr::polymorphic_allocator<> allocator; auto query(const test_std::get_allocator_t&) const noexcept { return allocator; } @@ -278,6 +298,149 @@ auto test_then_attributes() { test::test_sender_env(42, test::test_forwardable_attr{}, s | test_std::upon_stopped([]() {})); test::test_sender_env(84, test::test_non_forwardable_attr{}, s | test_std::upon_stopped([]() {})); } + +// Completion attributes must follow the source completion after an adaptor changes its tag. +struct attribute_domain : test_std::default_domain {}; +using test::attribute_sender; +using test::error_domain; +using test::has_completion_scheduler; +using test::loop_scheduler; +using test::stopped_domain; +using test::value_domain; + +auto test_remapped_completion_attributes() -> void { + test_std::run_loop value_loop, error_loop, stopped_loop; + struct receiver_env { + loop_scheduler scheduler; + auto query(test_std::get_start_scheduler_t) const noexcept { return scheduler; } + static auto query(test_std::get_domain_t) noexcept { return attribute_domain{}; } + }; + receiver_env env{value_loop.get_scheduler()}; + auto recovered = test_std::just_error(-1) | test_std::upon_error([](int i) noexcept { return i; }); + auto stopped = test_std::just_stopped() | test_std::stopped_as_error(-1); + ASSERT(test_std::get_completion_scheduler(test_std::get_env(recovered), env) == + value_loop.get_scheduler()); + ASSERT(test_std::get_completion_scheduler(test_std::get_env(stopped), env) == + value_loop.get_scheduler()); + static_assert(std::same_as( + test_std::get_env(recovered), env)), + attribute_domain>); + static_assert( + std::same_as(test_std::get_env(stopped), env)), + attribute_domain>); + + auto child = + attribute_sender<>{value_loop.get_scheduler(), error_loop.get_scheduler(), stopped_loop.get_scheduler()}; + auto mixed_value = child | test_std::upon_error([](int i) noexcept { return i; }); + auto mixed_error = child | test_std::stopped_as_error(-1); + auto unchanged = child | test_std::then([](int i) noexcept { return i; }); + auto throwing = child | test_std::then([](int i) { return i; }); + auto optional = child | test_std::stopped_as_optional; + auto empty_env = test_std::env{}; + static_assert(!has_completion_scheduler); + static_assert(!has_completion_scheduler); + static_assert(std::same_as( + test_std::get_env(mixed_value), empty_env)), + test_std::indeterminate_domain>); + static_assert(std::same_as( + test_std::get_env(mixed_error), empty_env)), + test_std::indeterminate_domain>); + static_assert(!has_completion_scheduler); + static_assert(std::same_as( + test_std::get_env(throwing), empty_env)), + test_std::indeterminate_domain>); + static_assert(!has_completion_scheduler); + static_assert(std::same_as( + test_std::get_env(optional), empty_env)), + test_std::indeterminate_domain>); + ASSERT(test_std::get_completion_scheduler(test_std::get_env(unchanged), empty_env) == + error_loop.get_scheduler()); + auto same_origin = test_std::just(1) | test_std::then([](int i) { return i; }) | + test_std::upon_error([](const std::exception_ptr&) noexcept { return -1; }); + ASSERT(test_std::get_completion_scheduler(test_std::get_env(same_origin), env) == + value_loop.get_scheduler()); +} + +auto test_equal_schedulers_from_independent_sources() -> void { + test_std::run_loop loop; + const auto scheduler = loop.get_scheduler(); + auto child = attribute_sender<>{scheduler, scheduler, scheduler}; + auto attrs = test_std::get_env(child); + ASSERT(test_std::get_completion_scheduler(attrs) == + test_std::get_completion_scheduler(attrs)); + + auto sndr = child | test_std::upon_error([](int i) noexcept { return i; }); + // Equal scheduler values do not make two independent completion queries the same source. + static_assert(!has_completion_scheduler); + static_assert(!has_completion_scheduler>); + ASSERT(test_std::get_completion_scheduler(test_std::get_env(sndr)) == scheduler); +} + +struct potentially_throwing_copy { + potentially_throwing_copy(const potentially_throwing_copy&) noexcept(false); + potentially_throwing_copy(potentially_throwing_copy&&) noexcept = default; +}; + +auto test_optional_construction_error_origin() -> void { + test_std::run_loop value_loop, error_loop; + const auto value_scheduler = value_loop.get_scheduler(); + const auto error_scheduler = error_loop.get_scheduler(); + const auto env = test_std::env<>{}; + auto copy_child = + attribute_sender{value_scheduler, error_scheduler, value_scheduler}; + auto move_child = attribute_sender{value_scheduler, error_scheduler, value_scheduler}; + auto copy = copy_child | test_std::stopped_as_optional; + auto move = move_child | test_std::stopped_as_optional; + + static_assert(!has_completion_scheduler); + static_assert( + std::same_as(test_std::get_env(copy), env)), + test_std::indeterminate_domain>); + ASSERT(test_std::get_completion_scheduler(test_std::get_env(move), env) == error_scheduler); +} + +auto test_variant_construction_error_origin() -> void { + test_std::run_loop value_loop, error_loop; + const auto value_scheduler = value_loop.get_scheduler(); + const auto error_scheduler = error_loop.get_scheduler(); + const auto env = test_std::env<>{}; + auto copy_child = + attribute_sender{value_scheduler, error_scheduler, value_scheduler}; + auto move_child = attribute_sender{value_scheduler, error_scheduler, value_scheduler}; + auto copy = copy_child | test_std::into_variant; + auto move = move_child | test_std::into_variant; + + using copy_signatures = test_std::completion_signatures_of_t>; + using move_signatures = test_std::completion_signatures_of_t>; + static_assert(copy_signatures::count_of(test_std::set_error_t{}) == 2); + static_assert(move_signatures::count_of(test_std::set_error_t{}) == 1); + static_assert(!has_completion_scheduler); + static_assert( + std::same_as(test_std::get_env(copy), env)), + test_std::indeterminate_domain>); + ASSERT(test_std::get_completion_scheduler(test_std::get_env(move), env) == error_scheduler); + ASSERT(test_std::get_completion_scheduler(test_std::get_env(copy), env) == value_scheduler); +} + +auto test_scheduler_origin_through_nested_adaptors() -> void { + test_std::run_loop loop; + test_std::inplace_stop_source stop_source; + struct stoppable_env { + test_std::inplace_stop_token token; + auto query(test_std::get_stop_token_t) const noexcept { return token; } + }; + const auto env = stoppable_env{stop_source.get_token()}; + const auto scheduler = loop.get_scheduler(); + auto sndr = test_std::schedule(scheduler) | test_std::then([] { return 42; }) | + test_std::upon_error([](const std::exception_ptr&) { return -1; }) | + test_std::upon_error([](const std::exception_ptr&) noexcept { return -2; }); + auto attrs = test_std::get_env(sndr); + // Each recovery can turn a callback failure back into a value. All value paths still + // trace to schedule's value completion, independently of the possible stopped path. + ASSERT(test_std::get_completion_scheduler(attrs, env) == scheduler); + ASSERT(test_std::get_completion_scheduler(attrs, env) == scheduler); + static_assert(!has_completion_scheduler); +} } // namespace TEST(exec_then) { @@ -293,9 +456,15 @@ TEST(exec_then) { test_then_type(); test_then_multi_type(); test_then_value(); + test_then_reference_result(); test_then_allocator(); test_then_env(); test_then_completions(); test_then_attributes(); + test_remapped_completion_attributes(); + test_equal_schedulers_from_independent_sources(); + test_optional_construction_error_origin(); + test_variant_construction_error_origin(); + test_scheduler_origin_through_nested_adaptors(); } diff --git a/tests/beman/execution/include/test/attribute_sender.hpp b/tests/beman/execution/include/test/attribute_sender.hpp new file mode 100644 index 00000000..a527b086 --- /dev/null +++ b/tests/beman/execution/include/test/attribute_sender.hpp @@ -0,0 +1,85 @@ +// tests/beman/execution/include/test/attribute_sender.hpp -*-C++-*- +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef INCLUDED_TESTS_BEMAN_EXECUTION_INCLUDE_TEST_ATTRIBUTE_SENDER +#define INCLUDED_TESTS_BEMAN_EXECUTION_INCLUDE_TEST_ATTRIBUTE_SENDER + +#include +#include +#ifdef BEMAN_HAS_IMPORT_STD +import std; +#else +#include +#include +#endif +#ifdef BEMAN_HAS_MODULES +import beman.execution; +#else +#include +#endif + +// ---------------------------------------------------------------------------- + +namespace test { +struct value_domain : test_std::default_domain {}; +struct error_domain : test_std::default_domain {}; +struct stopped_domain : test_std::default_domain {}; +using loop_scheduler = decltype(std::declval().get_scheduler()); + +// A sender reporting a different completion scheduler and domain for each completion tag. +template +struct attribute_sender { + using sender_concept = test_std::sender_tag; + using completion_signatures = test_std:: + completion_signatures; + template + static consteval auto get_completion_signatures() -> completion_signatures { + return {}; + } + + loop_scheduler value_scheduler; + loop_scheduler error_scheduler; + loop_scheduler stopped_scheduler; + + struct attributes { + loop_scheduler value_scheduler; + loop_scheduler error_scheduler; + loop_scheduler stopped_scheduler; + + template + auto query(test_std::get_completion_scheduler_t, const Env&...) const noexcept { + if constexpr (std::same_as) + return value_scheduler; + else if constexpr (std::same_as) + return error_scheduler; + else + return stopped_scheduler; + } + template + requires(!std::same_as) + static auto query(test_std::get_completion_domain_t, const Env&...) noexcept { + if constexpr (std::same_as) + return value_domain{}; + else if constexpr (std::same_as) + return error_domain{}; + else + return stopped_domain{}; + } + }; + auto get_env() const noexcept { return attributes{value_scheduler, error_scheduler, stopped_scheduler}; } +}; + +template +concept has_completion_scheduler = requires(const Sender& sender, const Env&... env) { + test_std::get_completion_scheduler(test_std::get_env(sender), env...); +}; + +// The completion domain a sender reports for Tag in an empty environment. +template +using completion_domain_t = decltype(test_std::get_completion_domain( + test_std::get_env(std::declval()), test_std::env<>{})); +} // namespace test + +// ---------------------------------------------------------------------------- + +#endif