From 0981f72a517d50775bb61ae0db5df44220a18303 Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Thu, 19 Nov 2026 22:23:46 +0800 Subject: [PATCH] Secure the assigned value before destroying the contained value of proxy - Changed the copy, move, converting and pointer assignments to construct the new value before destroying the contained value of `*this`, because the source may be owned by that value, such as in `p = std::move(p->next)`, which was undefined behavior. When `*this` does not contain a value, the new value is constructed directly in `*this` without a temporary. - Moved the temporary of the copy and pointer assignments into `*this`, unless it cannot be moved, or moving it may throw while copying it cannot. A second copy is therefore made only for a facade that forbids relocation, or to keep the `noexcept` specification of the copy assignments, and the `noexcept` specifications stay unchanged. `std::move_if_noexcept` was not used, because it copies whenever the move may throw even if the copy may throw too, and it treats a `proxy` that forbids relocation as nothrow movable through its copy constructor. - Constrained the internal move of a `proxy` like its move constructors, so an rvalue falls back to the internal copy when the facade forbids relocation or requires trivial copyability, as overload resolution does for the constructors. The converting copy assignment of a facade with trivial copyability therefore copies the temporary bytewise instead of relocating it. - Documented that the source of an assignment may be owned by the contained value of `*this`, how the contained value is initialized from the temporary, and the state of `*this` when an exception is thrown. - Changed `swap` for facades that forbid relocation to copy directly instead of through the copy assignment, so it still makes three copies. - Updated the expected operations of the move assignment and of the converting copy assignment of a facade that forbids relocation when the target holds a value, and of the copy and pointer assignments to an empty `proxy`, which no longer make a temporary. Added tests that assign from a value owned by the target for each assignment and check that the source is used before the target value is destroyed, with the value of the pointer assignment test stored inline by `make_proxy_inplace` so that the test also fails when the target value is relocated away before the source is used, tests for the copy and converting copy assignments of a facade with a non-throwing copy that forbids relocation, tests for the copy and converting copy assignments of a facade with a non-throwing copy whose relocation may throw, a test for the converting copy assignment of a facade with trivial copyability whose relocation may throw, a test for the pointer assignment from an lvalue whose copy cannot throw while its move may throw, and a static assertion for the `noexcept` specification of the pointer assignment. --- docs/spec/proxy/assignment.md | 14 +- include/proxy/v5/detail/core.h | 74 +++++--- tests/proxy_lifetime_tests.cpp | 327 +++++++++++++++++++++++++++++++-- 3 files changed, 362 insertions(+), 53 deletions(-) diff --git a/docs/spec/proxy/assignment.md b/docs/spec/proxy/assignment.md index 40e13fbc..370be5ab 100644 --- a/docs/spec/proxy/assignment.md +++ b/docs/spec/proxy/assignment.md @@ -50,14 +50,14 @@ proxy& operator=(P&& ptr) F::destructibility >= lifetime_requirement::supported); ``` -Assigns a new value to `proxy` or destroys the contained value. +Assigns a new value to `proxy` or destroys the contained value. For `(2-6)`, the source of the assignment may be owned by the contained value of `*this`, such as in `p = std::move(p->next)`. Therefore, if `*this` contains a value, the new value is first constructed from the source as a temporary `temp`, then the contained value is destroyed, and then the contained value is initialized from `temp` as specified below. If an exception is thrown while the new value is constructed from the source, `*this` is not modified. If an exception is thrown otherwise, `*this` does not contain a value. -- `(1)` Destroys the current contained value if it exists. After the call, `*this` does not contain a value. -- `(2)` Copy assignment operator copies the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). When `F::copyability == lifetime_requirement::trivial`, copy assignment is defaulted. It is trivial only if the metadata storage and accessor base classes also permit trivial copy assignment. -- `(3)` Move assignment operator moves the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If the move construction throws when `F::relocatability == lifetime_requirement::supported`, `*this` does not contain a value. After move assignment, `rhs` is in a valid state with an unspecified value. The move assignment operator does not participate in overload resolution when `F::copyability == lifetime_requirement::trivial`, falling back to the defaulted copy assignment operator. -- `(4)` Converting copy assignment operator copies the contained value of `rhs` to `*this`, as if by constructing a `proxy` from `rhs` and assigning it. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). Participates in overload resolution only if `F2` is not `F` and `F` is a super of `F2`, reachable via `typename F2::super_types` transitively. -- `(5)` Converting move assignment operator moves the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If the move construction throws when `F::relocatability == lifetime_requirement::supported`, `*this` does not contain a value. After the assignment, `rhs` does not contain a value. Participates in overload resolution only if `F2` is not `F` and `F` is a super of `F2`, reachable via `typename F2::super_types` transitively. The converting move assignment operator does not participate in overload resolution when `F::copyability == lifetime_requirement::trivial`, falling back to `(4)`. -- `(6)` Let `VP` be `std::decay_t

`. Sets the contained value to an object of type `VP`, direct-non-list-initialized with `std::forward

(ptr)`. Participates in overload resolution only if `VP` is not a specialization of `proxy` and is a pointer-like type eligible for `proxy` (see [*ProxyFacade* requirements](../ProxyFacade.md)). *Since 3.3.0*: If [`proxiable`](../proxiable.md) is `false`, the program is ill-formed and a diagnostic is generated. +- `(1)` Destroys the current contained value if it exists. After the call, `*this` does not contain a value, even if the destruction throws. +- `(2)` Copy assignment operator copies the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If `*this` contains a value, `temp` is a `proxy`, and the contained value is initialized as if by `proxy(std::move(temp))` if `std::is_nothrow_move_constructible_v` is `true` or `std::is_nothrow_copy_constructible_v` is `false`, or as if by `proxy(std::as_const(temp))` otherwise. When `F::copyability == lifetime_requirement::trivial`, copy assignment is defaulted. It is trivial only if the metadata storage and accessor base classes also permit trivial copy assignment. +- `(3)` Move assignment operator moves the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If `*this` contains a value, `temp` is a `proxy`, and the contained value is initialized as if by `proxy(std::move(temp))`. If moving the contained value of `rhs` throws, `rhs` does not contain a value. After move assignment, `rhs` is in a valid state with an unspecified value. The move assignment operator does not participate in overload resolution when `F::copyability == lifetime_requirement::trivial`, falling back to the defaulted copy assignment operator. +- `(4)` Converting copy assignment operator copies the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If `*this` contains a value, `temp` is a `proxy`, and the contained value is initialized from it as in `(2)`. Participates in overload resolution only if `F2` is not `F` and `F` is a super of `F2`, reachable via `typename F2::super_types` transitively. +- `(5)` Converting move assignment operator moves the contained value of `rhs` to `*this`. If `rhs` does not contain a value, it destroys the contained value of `*this` (if any). If `*this` contains a value, `temp` is a `proxy`, and the contained value is initialized from it as in `(3)`. After the assignment, `rhs` does not contain a value, even if an exception is thrown. Participates in overload resolution only if `F2` is not `F` and `F` is a super of `F2`, reachable via `typename F2::super_types` transitively. The converting move assignment operator does not participate in overload resolution when `F::copyability == lifetime_requirement::trivial`, falling back to `(4)`. +- `(6)` Let `VP` be `std::decay_t

`. Sets the contained value to an object of type `VP`, direct-non-list-initialized with `std::forward

(ptr)`. If `*this` contains a value, `temp` is an object of type `VP`, and the contained value is direct-non-list-initialized with `std::move(temp)` if `std::is_move_constructible_v` is `true` and either `std::is_nothrow_move_constructible_v` is `true` or `std::is_nothrow_copy_constructible_v` is `false`, or with `std::as_const(temp)` otherwise. Participates in overload resolution only if `VP` is not a specialization of `proxy` and is a pointer-like type eligible for `proxy` (see [*ProxyFacade* requirements](../ProxyFacade.md)). *Since 3.3.0*: If [`proxiable`](../proxiable.md) is `false`, the program is ill-formed and a diagnostic is generated. ## Return Value diff --git a/include/proxy/v5/detail/core.h b/include/proxy/v5/detail/core.h index 910dae4c..cdf7e423 100644 --- a/include/proxy/v5/detail/core.h +++ b/include/proxy/v5/detail/core.h @@ -204,6 +204,12 @@ template concept nothrow_constructible = constructible && noexcept(::new (std::declval()) T(std::declval()...)); +template +using move_or_copy_t = + std::conditional_t && + (std::is_nothrow_move_constructible_v || + !std::is_nothrow_copy_constructible_v), + T&&, const T&>; template struct copyability_traits : inapplicable_traits {}; @@ -1255,14 +1261,11 @@ class proxy : public detail::proxy_traits::direct_accessor, F::destructibility >= lifetime_requirement::supported) { if (this != std::addressof(rhs)) [[likely]] { - if constexpr (F::copyability == lifetime_requirement::nothrow) { - destroy(); - initialize(rhs); - } else if constexpr (F::relocatability >= - lifetime_requirement::supported) { - *this = proxy{rhs}; - } else { + if (meta_) { + proxy temp = rhs; reset(); + initialize(static_cast>(temp)); + } else { initialize(rhs); } } @@ -1277,8 +1280,13 @@ class proxy : public detail::proxy_traits::direct_accessor, F::copyability != lifetime_requirement::trivial) { if (this != std::addressof(rhs)) [[likely]] { - reset(); - initialize(std::move(rhs)); + if (meta_) { + proxy temp = std::move(rhs); + reset(); + initialize(std::move(temp)); + } else { + initialize(std::move(rhs)); + } } return *this; } @@ -1292,13 +1300,11 @@ class proxy : public detail::proxy_traits::direct_accessor, F::copyability >= lifetime_requirement::supported && F::destructibility >= lifetime_requirement::supported) { - if constexpr (F::copyability >= lifetime_requirement::nothrow) { - destroy(); - initialize(rhs); - } else if constexpr (F::relocatability >= lifetime_requirement::supported) { - *this = proxy{rhs}; - } else { + if (meta_) { + proxy temp = rhs; reset(); + initialize(static_cast>(temp)); + } else { initialize(rhs); } return *this; @@ -1314,8 +1320,13 @@ class proxy : public detail::proxy_traits::direct_accessor, F::destructibility >= lifetime_requirement::supported && F::copyability != lifetime_requirement::trivial) { - reset(); - initialize(std::move(rhs)); + if (meta_) { + proxy temp = std::move(rhs); + reset(); + initialize(std::move(temp)); + } else { + initialize(std::move(rhs)); + } return *this; } template @@ -1327,15 +1338,13 @@ class proxy : public detail::proxy_traits::direct_accessor, std::is_constructible_v, P> && F::destructibility >= lifetime_requirement::supported) { - if constexpr (std::is_nothrow_constructible_v, P>) { - destroy(); - initialize>(std::forward

(ptr)); - } else if constexpr (F::relocatability >= lifetime_requirement::supported || - F::copyability >= lifetime_requirement::nothrow) { - *this = proxy{std::forward

(ptr)}; - } else { + using VP = std::decay_t

; + if (meta_) { + VP temp(std::forward

(ptr)); reset(); - initialize>(std::forward

(ptr)); + initialize(static_cast>(temp)); + } else { + initialize(std::forward

(ptr)); } return *this; } @@ -1410,14 +1419,16 @@ class proxy : public detail::proxy_traits::direct_accessor, if (meta_) { if (rhs.meta_) { proxy temp = *this; - *this = rhs; - rhs = temp; + reset(); + initialize(rhs); + rhs.reset(); + rhs.initialize(temp); } else { - rhs = *this; + rhs.initialize(*this); reset(); } } else if (rhs.meta_) { - *this = rhs; + initialize(rhs); rhs.reset(); } } @@ -1501,7 +1512,10 @@ class proxy : public detail::proxy_traits::direct_accessor, } } template - void initialize(proxy&& rhs) { + void initialize(proxy&& rhs) + requires(F::relocatability >= lifetime_requirement::supported && + F::copyability != lifetime_requirement::trivial) + { PRO5D_DEBUG(std::ignore = &pro_symbol_guard;) if (rhs.has_value()) { detail::proxy_helper::meta_resetting_guard guard{rhs}; diff --git a/tests/proxy_lifetime_tests.cpp b/tests/proxy_lifetime_tests.cpp index cf02f887..621fa586 100644 --- a/tests/proxy_lifetime_tests.cpp +++ b/tests/proxy_lifetime_tests.cpp @@ -6,9 +6,13 @@ #include #include #include +#include #include #include +#include #include +#include +#include namespace proxy_lifetime_tests_detail { @@ -53,6 +57,8 @@ static_assert(!std::is_convertible_v, static_assert(!std::is_convertible_v, pro::proxy>); +static_assert(std::is_nothrow_assignable_v&, int*>); + struct TestNonDestructibleFacade : pro::facade_builder // ::add_convention // @@ -148,6 +154,74 @@ struct TestNonDestructibleCopyFacade ::support_destruction // ::build {}; +struct TestOwningCopyFacade + : pro::facade_builder // + ::add_convention // + ::support_copy // + ::support_relocation // + ::build {}; + +struct TestOwningDerivedFacade : pro::facade_builder // + ::add_facade // + ::build {}; + +enum class AssignmentEvent { kSourceUsed, kOwnerDestroyed }; + +const std::vector kSourceUsedBeforeOwnerDestroyed{ + AssignmentEvent::kSourceUsed, AssignmentEvent::kOwnerDestroyed}; + +struct AssignmentOrder { + const void* source = nullptr; + std::vector events; +}; + +template +class OrderedPtr { +public: + using element_type = int; + + OrderedPtr(int* value, AssignmentOrder* order) noexcept + : value_(value), order_(order) { + order_->source = this; + } + OrderedPtr(const OrderedPtr& rhs) noexcept + : value_(rhs.value_), order_(rhs.order_) { + rhs.Use(); + } + OrderedPtr(OrderedPtr&& rhs) noexcept + requires(Movable) + : value_(rhs.value_), order_(rhs.order_) { + rhs.Use(); + } + OrderedPtr(OrderedPtr&&) + requires(!Movable) + = delete; + int& operator*() const noexcept { return *value_; } + +private: + void Use() const noexcept { + if (order_->source == this) { + order_->events.push_back(AssignmentEvent::kSourceUsed); + } + } + + int* value_; + AssignmentOrder* order_; +}; + +template +struct OrderedOwner { + template + explicit OrderedOwner(AssignmentOrder* order, Args&&... args) + : order(order), member(std::forward(args)...) {} + ~OrderedOwner() { order->events.push_back(AssignmentEvent::kOwnerDestroyed); } + + friend std::string to_string(const OrderedOwner&) { return "owner"; } + + AssignmentOrder* order; + T member; +}; + } // namespace proxy_lifetime_tests_detail namespace detail = proxy_lifetime_tests_detail; @@ -515,18 +589,15 @@ TEST(ProxyLifetimeTests, TestPolyAssignment_FromValue_ToNull) { pro::proxy p; p = utils::LifetimeTracker::Session{&tracker}; ASSERT_TRUE(p.has_value()); - ASSERT_EQ(ToString(*p), "Session 3"); + ASSERT_EQ(ToString(*p), "Session 2"); expected_ops.emplace_back(1, utils::LifetimeOperationType::kValueConstruction); expected_ops.emplace_back(2, utils::LifetimeOperationType::kMoveConstruction); - expected_ops.emplace_back(3, - utils::LifetimeOperationType::kMoveConstruction); - expected_ops.emplace_back(2, utils::LifetimeOperationType::kDestruction); expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); ASSERT_TRUE(tracker.GetOperations() == expected_ops); } - expected_ops.emplace_back(3, utils::LifetimeOperationType::kDestruction); + expected_ops.emplace_back(2, utils::LifetimeOperationType::kDestruction); ASSERT_TRUE(tracker.GetOperations() == expected_ops); } @@ -843,18 +914,15 @@ TEST(ProxyLifetimeTests, TestCopyAssignment_FromValue_ToNull) { utils::LifetimeOperationType::kValueConstruction); p1 = p2; ASSERT_TRUE(p1.has_value()); - ASSERT_EQ(ToString(*p1), "Session 3"); + ASSERT_EQ(ToString(*p1), "Session 2"); ASSERT_TRUE(p2.has_value()); ASSERT_EQ(ToString(*p2), "Session 1"); expected_ops.emplace_back(2, utils::LifetimeOperationType::kCopyConstruction); - expected_ops.emplace_back(3, - utils::LifetimeOperationType::kMoveConstruction); - expected_ops.emplace_back(2, utils::LifetimeOperationType::kDestruction); ASSERT_TRUE(tracker.GetOperations() == expected_ops); } expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); - expected_ops.emplace_back(3, utils::LifetimeOperationType::kDestruction); + expected_ops.emplace_back(2, utils::LifetimeOperationType::kDestruction); ASSERT_TRUE(tracker.GetOperations() == expected_ops); } @@ -936,15 +1004,18 @@ TEST(ProxyLifetimeTests, TestMoveAssignment_FromValue_ToValue) { utils::LifetimeOperationType::kValueConstruction); p1 = std::move(p2); ASSERT_TRUE(p1.has_value()); - ASSERT_EQ(ToString(*p1), "Session 3"); + ASSERT_EQ(ToString(*p1), "Session 4"); ASSERT_FALSE(p2.has_value()); - expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); expected_ops.emplace_back(3, utils::LifetimeOperationType::kMoveConstruction); expected_ops.emplace_back(2, utils::LifetimeOperationType::kDestruction); + expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); + expected_ops.emplace_back(4, + utils::LifetimeOperationType::kMoveConstruction); + expected_ops.emplace_back(3, utils::LifetimeOperationType::kDestruction); ASSERT_TRUE(tracker.GetOperations() == expected_ops); } - expected_ops.emplace_back(3, utils::LifetimeOperationType::kDestruction); + expected_ops.emplace_back(4, utils::LifetimeOperationType::kDestruction); ASSERT_TRUE(tracker.GetOperations() == expected_ops); } @@ -969,12 +1040,13 @@ TEST(ProxyLifetimeTests, TestMoveAssignment_FromValue_ToValue_Exception) { ASSERT_EQ(e.type_, utils::LifetimeOperationType::kMoveConstruction); } ASSERT_TRUE(exception_thrown); - ASSERT_FALSE(p1.has_value()); + ASSERT_TRUE(p1.has_value()); + ASSERT_EQ(ToString(*p1), "Session 1"); ASSERT_FALSE(p2.has_value()); - expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); expected_ops.emplace_back(2, utils::LifetimeOperationType::kDestruction); ASSERT_TRUE(tracker.GetOperations() == expected_ops); } + expected_ops.emplace_back(1, utils::LifetimeOperationType::kDestruction); ASSERT_TRUE(tracker.GetOperations() == expected_ops); } @@ -1316,6 +1388,41 @@ TEST(ProxyLifetimeTests, Test_CopyAssignment_NoRelocation) { ASSERT_EQ(ToString(*p1), "111"); } +TEST(ProxyLifetimeTests, Test_CopyAssignment_NoRelocation_NothrowCopy) { + struct Pinned : pro::facade_builder // + ::add_convention // + ::support_copy // + ::support_relocation // + ::build {}; + int v1 = 111, v2 = 222; + pro::proxy p1 = &v1; + pro::proxy p2 = &v2; + static_assert(noexcept(p1 = p2)); + p1 = p2; + ASSERT_EQ(ToString(*p1), "222"); + ASSERT_EQ(ToString(*p2), "222"); +} + +TEST(ProxyLifetimeTests, Test_CopyAssignment_ThrowingRelocation_NothrowCopy) { + struct ThrowingRelocation + : pro::facade_builder // + ::add_convention // + ::support_copy // + ::support_relocation // + ::build {}; + int v1 = 111, v2 = 222; + pro::proxy p1{ + std::in_place_type>, &v1}; + pro::proxy p2{ + std::in_place_type>, &v2}; + static_assert(noexcept(p1 = p2)); + p1 = p2; + ASSERT_EQ(ToString(*p1), "222"); + ASSERT_EQ(ToString(*p2), "222"); +} + TEST(ProxyLifetimeTests, Test_PointerAssignment_NoRelocationNoCopy) { struct Pinned : pro::facade_builder // ::add_convention // + ::support_relocation // + ::build {}; + int v1 = 111, v2 = 222; + pro::proxy p{ + std::in_place_type>, &v1}; + utils::ThrowOnMovePtr ptr{&v2}; + static_assert(noexcept(p = ptr)); + p = ptr; + ASSERT_EQ(ToString(*p), "222"); +} + TEST(ProxyLifetimeTests, TestSwap_NoRelocation) { struct Pinned : pro::facade_builder // ::add_convention p2{ std::in_place_type, &tracker}; p1 = p2; - ASSERT_EQ(ToString(*p1), "Session 3"); + ASSERT_EQ(ToString(*p1), "Session 4"); ASSERT_TRUE(p2.has_value()); ASSERT_EQ(ToString(*p2), "Session 2"); } +TEST(ProxyLifetimeTests, + Test_ConvertingCopyAssignment_NoRelocation_NothrowCopy) { + struct Pinned : pro::facade_builder // + ::add_convention // + ::support_copy // + ::support_relocation // + ::build {}; + struct PinnedDerived : pro::facade_builder // + ::add_facade // + ::build {}; + int v1 = 111, v2 = 222; + pro::proxy p1 = &v1; + pro::proxy p2 = &v2; + static_assert(noexcept(p1 = p2)); + p1 = p2; + ASSERT_EQ(ToString(*p1), "222"); + ASSERT_EQ(ToString(*p2), "222"); +} + +TEST(ProxyLifetimeTests, + Test_ConvertingCopyAssignment_ThrowingRelocation_NothrowCopy) { + struct ThrowingRelocation + : pro::facade_builder // + ::add_convention // + ::support_copy // + ::support_relocation // + ::build {}; + struct ThrowingRelocationDerived : pro::facade_builder // + ::add_facade // + ::build {}; + int v1 = 111, v2 = 222; + pro::proxy p1{ + std::in_place_type>, &v1}; + pro::proxy p2{ + std::in_place_type>, &v2}; + static_assert(noexcept(p1 = p2)); + p1 = p2; + ASSERT_EQ(ToString(*p1), "222"); + ASSERT_EQ(ToString(*p2), "222"); +} + +TEST(ProxyLifetimeTests, + Test_ConvertingCopyAssignment_ThrowingRelocation_TrivialCopy) { + struct TrivialCopy + : pro::facade_builder // + ::add_convention // + ::support_copy // + ::support_relocation // + ::build {}; + struct TrivialCopyDerived : pro::facade_builder // + ::add_facade // + ::build {}; + int v1 = 111, v2 = 222; + pro::proxy p1{std::in_place_type>, + &v1}; + pro::proxy p2{ + std::in_place_type>, &v2}; + static_assert(noexcept(p1 = p2)); + p1 = p2; + ASSERT_EQ(ToString(*p1), "222"); + ASSERT_EQ(ToString(*p2), "222"); +} + TEST(ProxyLifetimeTests, Test_NonDestructible_Construction) { using Proxy = pro::proxy; alignas(Proxy) std::byte null_storage[sizeof(Proxy)]; @@ -1710,6 +1899,112 @@ TEST(ProxyLifetimeTests, Test_NonDestructible_NonDestructiblePtr) { ASSERT_EQ(ToString(**p2), "123"); } +TEST(ProxyLifetimeTests, TestCopyAssignment_FromOwnedValue) { + using F = detail::TestOwningCopyFacade; + detail::AssignmentOrder order; + int v = 7; + auto owner = std::make_shared>>( + &order, std::in_place_type>, &v, &order); + pro::proxy& source = owner->member; + pro::proxy target = std::move(owner); + target = source; + ASSERT_EQ(ToString(*target), "7"); + ASSERT_EQ(order.events, detail::kSourceUsedBeforeOwnerDestroyed); +} + +TEST(ProxyLifetimeTests, TestCopyAssignment_FromOwnedValue_NoRelocation) { + struct F : pro::facade_builder // + ::add_convention // + ::support_copy // + ::support_relocation // + ::build {}; + detail::AssignmentOrder order; + int v = 7; + auto owner = std::make_shared>>( + &order, std::in_place_type>, &v, &order); + pro::proxy& source = owner->member; + pro::proxy target = std::move(owner); + target = source; + ASSERT_EQ(ToString(*target), "7"); + ASSERT_EQ(order.events, detail::kSourceUsedBeforeOwnerDestroyed); +} + +TEST(ProxyLifetimeTests, TestMoveAssignment_FromOwnedValue) { + using F = detail::TestOwningCopyFacade; + detail::AssignmentOrder order; + int v = 7; + auto owner = std::make_shared>>( + &order, std::in_place_type>, &v, &order); + pro::proxy& source = owner->member; + pro::proxy target = std::move(owner); + target = std::move(source); + ASSERT_EQ(ToString(*target), "7"); + ASSERT_EQ(order.events, detail::kSourceUsedBeforeOwnerDestroyed); +} + +TEST(ProxyLifetimeTests, Test_ConvertingCopyAssignment_FromOwnedValue) { + using F = detail::TestOwningCopyFacade; + using F2 = detail::TestOwningDerivedFacade; + detail::AssignmentOrder order; + int v = 7; + auto owner = std::make_shared>>( + &order, std::in_place_type>, &v, &order); + pro::proxy& source = owner->member; + pro::proxy target = std::move(owner); + target = source; + ASSERT_EQ(ToString(*target), "7"); + ASSERT_EQ(order.events, detail::kSourceUsedBeforeOwnerDestroyed); +} + +TEST(ProxyLifetimeTests, Test_ConvertingMoveAssignment_FromOwnedValue) { + using F = detail::TestOwningCopyFacade; + using F2 = detail::TestOwningDerivedFacade; + detail::AssignmentOrder order; + int v = 7; + auto owner = std::make_shared>>( + &order, std::in_place_type>, &v, &order); + pro::proxy& source = owner->member; + pro::proxy target = std::move(owner); + target = std::move(source); + ASSERT_EQ(ToString(*target), "7"); + ASSERT_EQ(order.events, detail::kSourceUsedBeforeOwnerDestroyed); +} + +TEST(ProxyLifetimeTests, TestPolyAssignment_FromOwnedValue) { + struct F : pro::facade_builder // + ::add_convention // + ::add_skill // + ::restrict_layout<4 * sizeof(void*)> // + ::support_relocation // + ::build {}; + using Owner = detail::OrderedOwner>; + detail::AssignmentOrder order; + int v = 7; + auto target = pro::make_proxy_inplace(&order, &v, &order); + detail::OrderedPtr& source = proxy_cast(*target).member; + target = std::move(source); + ASSERT_EQ(ToString(*target), "7"); + ASSERT_EQ(order.events, detail::kSourceUsedBeforeOwnerDestroyed); +} + +TEST(ProxyLifetimeTests, TestPolyAssignment_FromOwnedValue_NonMovablePtr) { + struct F : pro::facade_builder // + ::add_convention // + ::support_relocation // + ::build {}; + detail::AssignmentOrder order; + int v = 7; + auto owner = + std::make_shared>>( + &order, &v, &order); + detail::OrderedPtr& source = owner->member; + pro::proxy target = std::move(owner); + static_assert(noexcept(target = source)); + target = source; + ASSERT_EQ(ToString(*target), "7"); + ASSERT_EQ(order.events, detail::kSourceUsedBeforeOwnerDestroyed); +} + TEST(ProxyLifetimeTests, Test_Substitution_TrivialDerived) { struct Super : pro::facade_builder // ::add_convention