diff --git a/include/exec/async_scope.hpp b/include/exec/async_scope.hpp index 65f5c5797..10deb398b 100644 --- a/include/exec/async_scope.hpp +++ b/include/exec/async_scope.hpp @@ -674,8 +674,18 @@ namespace experimental::execution public: using sender_concept = STDEXEC::sender_tag; - __future(__future&&) = default; - auto operator=(__future&&) -> __future& = default; + __future(__future&&) = default; + auto operator=(__future&& __other) noexcept -> __future& + { + if (this != &__other) + { + // Apply the existing destructor's pending-state ownership transfer + // to the old future instead of deleting its unique_ptr directly. + __future __old{std::move(*this)}; + __state_ = std::move(__other.__state_); + } + return *this; + } ~__future() noexcept { diff --git a/test/exec/CMakeLists.txt b/test/exec/CMakeLists.txt index d83ea9c96..b9d470061 100644 --- a/test/exec/CMakeLists.txt +++ b/test/exec/CMakeLists.txt @@ -16,6 +16,7 @@ set(exec_test_sources ../test_main.cpp + test_spawn_future_move_assignment.cpp test_bwos_lifo_queue.cpp test_atomic_intrusive_queue.cpp test_any_sender.cpp diff --git a/test/exec/test_spawn_future_move_assignment.cpp b/test/exec/test_spawn_future_move_assignment.cpp new file mode 100644 index 000000000..c2da72acc --- /dev/null +++ b/test/exec/test_spawn_future_move_assignment.cpp @@ -0,0 +1,72 @@ +/* + * Licensed under the Apache License, Version 2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + */ + +#include +#include +#include +#include +#include +#include +#include + +namespace regression_spawn_future_move_assignment +{ + // Public APIs only, unmodified production headers. Both futures have already + // started their schedule operations, which are still queued in the run_loop. + + int run_case(int argc, char** argv) + { + auto mode = argc > 1 ? std::string_view(argv[1]) : std::string_view("assign"); + exec::async_scope scope; + STDEXEC::run_loop loop; + auto task = STDEXEC::schedule(loop.get_scheduler()); + using future = decltype(scope.spawn_future(task)); + std::optional first{scope.spawn_future(task)}; + auto second = scope.spawn_future(task); + if (mode == "completed") + { + loop.finish(); + loop.run(); + } + if (mode == "drop") + first.reset(); + else + *first = std::move(second); + std::fputs("future replacement/drop returned; draining queued operations\n", stderr); + if (mode != "completed") + { + loop.finish(); + loop.run(); + } + STDEXEC::sync_wait(scope.on_empty()); + std::printf("PASS mode=%.*s scope drained\n", int(mode.size()), mode.data()); + return 0; + } + +} // namespace regression_spawn_future_move_assignment + +TEST_CASE("spawn_future_move_assignment: assign", "[regression][async-scope-future]") +{ + char program[] = "regression"; + char arg0[] = "assign"; + char* argv[] = {program, arg0}; + REQUIRE(regression_spawn_future_move_assignment::run_case(2, argv) == 0); +} + +TEST_CASE("spawn_future_move_assignment: drop", "[regression][async-scope-future]") +{ + char program[] = "regression"; + char arg0[] = "drop"; + char* argv[] = {program, arg0}; + REQUIRE(regression_spawn_future_move_assignment::run_case(2, argv) == 0); +} + +TEST_CASE("spawn_future_move_assignment: completed", "[regression][async-scope-future]") +{ + char program[] = "regression"; + char arg0[] = "completed"; + char* argv[] = {program, arg0}; + REQUIRE(regression_spawn_future_move_assignment::run_case(2, argv) == 0); +}