Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions include/stdexec/__detail/__sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ namespace STDEXEC

template <class _Self>
static constexpr auto transform_sender(set_value_t, _Self &&__self, __ignore) //
-> decltype(auto)
noexcept(__nothrow_transform<_Self>()) -> decltype(auto)
{
constexpr std::size_t __nbr_sndrs = __nbr_children_of<_Self>;
if constexpr (__nbr_sndrs == 0)
Expand All @@ -491,10 +491,32 @@ namespace STDEXEC
}

private:
static constexpr auto __mk_sndr = []<class... _Child>(__ignore, __ignore, _Child &&...__child)
static constexpr auto __mk_sndr =
[]<class... _Child>(__ignore, __ignore, _Child &&...__child) //
noexcept(__nothrow_decay_copyable<_Child...>)
{
return __seq::__sndr<__decay_t<_Child>...>{static_cast<_Child &&>(__child)...};
};

//! whether transform_sender, given a sequence of type _Self, can't throw
template <class _Self>
static consteval auto __nothrow_transform() noexcept -> bool
{
constexpr std::size_t __nbr_sndrs = __nbr_children_of<_Self>;
if constexpr (__nbr_sndrs == 0)
{
return true;
}
else if constexpr (__nbr_sndrs == 1)
{
return noexcept(
static_cast<__tuple_element_t<2, _Self>>(STDEXEC::__get<2>(__declval<_Self>())));
}
else
{
return __nothrow_applicable<decltype(__mk_sndr) const &, _Self>;
}
}
};

inline constexpr __sequence_t __sequence{};
Expand Down
2 changes: 1 addition & 1 deletion include/stdexec/__detail/__transform_sender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace STDEXEC
}
else if constexpr (__same_as<__result_t, _Sndr>)
{
return __declfn<__result_t, __is_nothrow>();
return __declfn<__result_t, (_Nothrow && __is_nothrow)>();
}
else if constexpr (__same_as<_OpTag, start_t>)
{
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ set(stdexec_test_sources
stdexec/cpos/test_cpo_receiver.cpp
stdexec/cpos/test_cpo_schedule.cpp
stdexec/cpos/test_cpo_start.cpp
stdexec/cpos/test_cpo_transform_sender.cpp
stdexec/cpos/test_cpo_upon_stopped.cpp
stdexec/detail/test_any.cpp
stdexec/detail/test_common_domain.cpp
Expand Down
26 changes: 26 additions & 0 deletions test/stdexec/algos/adaptors/test_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,32 @@ TEST_CASE("sequence with two arguments works", "[sequence]")
}
}

namespace
{
//! A value whose copy constructor may throw, but whose move constructor
//! can't
struct throwing_copy
{
throwing_copy() = default;

throwing_copy(throwing_copy const &) noexcept(false) {}

throwing_copy(throwing_copy &&) noexcept = default;
};
} // namespace

TEST_CASE("lowering a sequence is noexcept unless moving or copying its children can throw",
"[sequence]")
{
auto nothrow = exec::sequence(ex::just(), ex::just(42));
STATIC_REQUIRE(noexcept(ex::transform_sender(std::move(nothrow), ex::env<>{})));
STATIC_REQUIRE(ex::__nothrow_connectable<decltype(nothrow), ex::__receiver_archetype<ex::env<>>>);

auto throwing = exec::sequence(ex::just(), ex::just(throwing_copy{}));
STATIC_REQUIRE(noexcept(ex::transform_sender(std::move(throwing), ex::env<>{})));
STATIC_REQUIRE(!noexcept(ex::transform_sender(throwing, ex::env<>{})));
}

#if !STDEXEC_NO_STDCPP_EXCEPTIONS()
TEST_CASE("sequence with sender with throwing connect", "[sequence]")
{
Expand Down
98 changes: 98 additions & 0 deletions test/stdexec/cpos/test_cpo_transform_sender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2026 Ian Petersen
* Copyright (c) 2026 NVIDIA Corporation
*
* 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.
*/
#include <catch2/catch_all.hpp>

#include <stdexec/execution.hpp>

#include <stdexcept>
#include <type_traits>
#include <utility>

namespace ex = STDEXEC;

namespace
{
inline constexpr auto return_zero = []() noexcept
{
return 0;
};

using then_sender_t = decltype(ex::just() | ex::then(return_zero));
using just_sender_t = decltype(ex::just(42));

//! A domain that transforms a then_sender_t into a just_sender_t, which it
//! doesn't transform, so the result is transformed again, by default_domain.
//! The transformation throws unless it's noexcept.
template <bool Nothrow>
struct then_to_just_domain
{
template <class Sndr, class Env>
requires std::same_as<std::remove_cvref_t<Sndr>, then_sender_t>
auto
transform_sender(ex::start_t, Sndr &&, Env const &) const noexcept(Nothrow) -> just_sender_t
{
if constexpr (!Nothrow)
{
throw std::runtime_error("transform_sender");
}
return ex::just(42);
}
};

template <bool Nothrow>
struct receiver
{
using receiver_concept = ex::receiver_tag;

void set_value(int) noexcept {}

auto get_env() const noexcept
{
return ex::prop(ex::get_domain, then_to_just_domain<Nothrow>{});
}
};

TEST_CASE("transform_sender is noexcept only if every transformation is",
"[cpo][cpo_transform_sender]")
{
SECTION("a potentially-throwing transformation whose result is transformed again")
{
auto env = ex::prop(ex::get_domain, then_to_just_domain<false>{});
STATIC_REQUIRE(
std::same_as<decltype(ex::transform_sender(std::declval<then_sender_t>(), env)),
just_sender_t>);
STATIC_REQUIRE(!noexcept(ex::transform_sender(std::declval<then_sender_t>(), env)));
STATIC_REQUIRE(
!noexcept(ex::connect(std::declval<then_sender_t>(), std::declval<receiver<false>>())));
}

SECTION("non-throwing transformations")
{
auto env = ex::prop(ex::get_domain, then_to_just_domain<true>{});
STATIC_REQUIRE(noexcept(ex::transform_sender(std::declval<then_sender_t>(), env)));
STATIC_REQUIRE(
noexcept(ex::connect(std::declval<then_sender_t>(), std::declval<receiver<true>>())));
}
}

TEST_CASE("an exception thrown by a transformation propagates out of connect",
"[cpo][cpo_transform_sender]")
{
auto sndr = ex::just() | ex::then(return_zero);
REQUIRE_THROWS_AS(ex::connect(std::move(sndr), receiver<false>{}), std::runtime_error);
}
} // namespace