summaryrefslogtreecommitdiffstats
path: root/library/cpp/threading
diff options
context:
space:
mode:
authorthegeorg <[email protected]>2025-11-06 17:13:27 +0300
committerthegeorg <[email protected]>2025-11-06 17:43:28 +0300
commitb64824d81555d01eeb83574daaaef7bb670461f4 (patch)
treed7aa8a77a0a076599f98529072936b511ef84fe0 /library/cpp/threading
parent92f0fb706123c524bda1e50de2ac060cdeb7f6c8 (diff)
Try to prevent slicing in `co_return ex;`
commit_hash:348f2fb14a4a93cfa3c8504419d021c1f41e4b8f
Diffstat (limited to 'library/cpp/threading')
-rw-r--r--library/cpp/threading/future/core/coroutine_traits.h5
-rw-r--r--library/cpp/threading/future/ut_gtest/coroutine_traits_ut.cpp7
2 files changed, 11 insertions, 1 deletions
diff --git a/library/cpp/threading/future/core/coroutine_traits.h b/library/cpp/threading/future/core/coroutine_traits.h
index b36362db28a..9ad791e58d0 100644
--- a/library/cpp/threading/future/core/coroutine_traits.h
+++ b/library/cpp/threading/future/core/coroutine_traits.h
@@ -87,6 +87,11 @@ struct std::coroutine_traits<NThreading::TFuture<T>, Args...> {
Y_ASSERT(success && "value already set");
}
+ // Allow only rvalues to be returned to prevent slicing
+ template <typename E>
+ requires std::derived_from<std::remove_cvref_t<E>, std::exception>
+ void return_value(E& err) = delete;
+
void return_value(auto&& val) {
bool success = State_->TrySetValue(std::forward<decltype(val)>(val), /* deferCallbacks */ true);
Y_ASSERT(success && "value already set");
diff --git a/library/cpp/threading/future/ut_gtest/coroutine_traits_ut.cpp b/library/cpp/threading/future/ut_gtest/coroutine_traits_ut.cpp
index a9b0176920e..0f902b9b998 100644
--- a/library/cpp/threading/future/ut_gtest/coroutine_traits_ut.cpp
+++ b/library/cpp/threading/future/ut_gtest/coroutine_traits_ut.cpp
@@ -149,7 +149,12 @@ TEST(TestFutureTraits, ErrorViaReturnException) {
co_return std::runtime_error("exception_to_return");
static std::runtime_error another("another_exception_not_to_return");
- co_return another;
+
+ // should not compile
+ // co_return another;
+
+ // explicit slicing is allowed
+ co_return std::exception(another);
};
auto coroutineReturnValueReturnException = [&]() -> NThreading::TFuture<size_t> {