diff --git a/libs/pika/async_cuda/include/pika/async_cuda/then_on_host.hpp b/libs/pika/async_cuda/include/pika/async_cuda/then_on_host.hpp index ba96269d9..fa7813b9b 100644 --- a/libs/pika/async_cuda/include/pika/async_cuda/then_on_host.hpp +++ b/libs/pika/async_cuda/include/pika/async_cuda/then_on_host.hpp @@ -92,12 +92,12 @@ namespace pika::cuda::experimental { // Certain versions of GCC with optimizations fail on // the move with an internal compiler error. #if defined(PIKA_GCC_VERSION) && (PIKA_GCC_VERSION < 100000) - auto&& result = PIKA_INVOKE(std::move(r.f), PIKA_FORWARD(Ts, ts)...); + pika::execution::experimental::set_value(PIKA_MOVE(r.receiver), + PIKA_INVOKE(std::move(r.f), PIKA_FORWARD(Ts, ts)...)); #else - auto&& result = PIKA_INVOKE(PIKA_MOVE(r.f), PIKA_FORWARD(Ts, ts)...); + pika::execution::experimental::set_value(PIKA_MOVE(r.receiver), + PIKA_INVOKE(PIKA_MOVE(r.f), PIKA_FORWARD(Ts, ts)...)); #endif - pika::execution::experimental::set_value( - PIKA_MOVE(r.receiver), PIKA_MOVE(result)); } }, [&](std::exception_ptr ep) { diff --git a/libs/pika/execution/include/pika/execution/algorithms/then.hpp b/libs/pika/execution/include/pika/execution/algorithms/then.hpp index e85b14086..fb73bf84f 100644 --- a/libs/pika/execution/include/pika/execution/algorithms/then.hpp +++ b/libs/pika/execution/include/pika/execution/algorithms/then.hpp @@ -77,12 +77,12 @@ namespace pika::then_detail { // Certain versions of GCC with optimizations fail on // the move with an internal compiler error. # if defined(PIKA_GCC_VERSION) && (PIKA_GCC_VERSION < 100000) - auto&& result = PIKA_INVOKE(std::move(f), PIKA_FORWARD(Ts, ts)...); + pika::execution::experimental::set_value(PIKA_MOVE(receiver), + PIKA_INVOKE(std::move(f), PIKA_FORWARD(Ts, ts)...)); # else - auto&& result = PIKA_INVOKE(PIKA_MOVE(f), PIKA_FORWARD(Ts, ts)...); + pika::execution::experimental::set_value(PIKA_MOVE(receiver), + PIKA_INVOKE(PIKA_MOVE(f), PIKA_FORWARD(Ts, ts)...)); # endif - pika::execution::experimental::set_value( - PIKA_MOVE(receiver), PIKA_MOVE(result)); } }, [&](std::exception_ptr ep) { diff --git a/libs/pika/execution/tests/unit/algorithm_then.cpp b/libs/pika/execution/tests/unit/algorithm_then.cpp index 3367b56b3..cee716fc5 100644 --- a/libs/pika/execution/tests/unit/algorithm_then.cpp +++ b/libs/pika/execution/tests/unit/algorithm_then.cpp @@ -136,6 +136,24 @@ int main() PIKA_TEST(set_value_called); } + // This is not recommended, but allowed + { + std::atomic set_value_called{false}; + auto s1 = ex::then(ex::just(), []() -> int& { + static int x = 3; + return x; + }); + auto s2 = ex::then(std::move(s1), [](int& x) -> int& { + x *= 4; + return x; + }); + auto f = [](int& x) { PIKA_TEST_EQ(x, 12); }; + auto r = callback_receiver{f, set_value_called}; + auto os = ex::connect(std::move(s2), std::move(r)); + ex::start(os); + PIKA_TEST(set_value_called); + } + // operator| overload { std::atomic set_value_called{false};