Copilot commented on code in PR #3724:
URL: https://github.com/apache/brpc/pull/3724#discussion_r4520539533


##########
src/butil/containers/optional.h:
##########
@@ -172,9 +301,10 @@ class optional {
         std::is_constructible<T, U&&>::value &&
         !internal::is_constructible_convertible_from_optional<T, U>::value &&
         std::is_convertible<U&&, T>::value, bool> = false>
-    optional(optional<U>&& rhs) : _engaged(rhs.has_value()) {
-        if (_engaged) {
+    optional(optional<U>&& rhs) {
+        if (rhs) {
             _storage.Init(std::move(*rhs));
+            _engaged = true;
             rhs.reset();

Review Comment:
   Resetting `rhs` here changes the existing and `std::optional` 
converting-move contract: an engaged source remains engaged after a move, with 
a moved-from contained value. This also makes the source state depend on 
whether `T` and `U` differ, unlike the same-type move constructor and 
converting assignment. Remove this `rhs.reset()` call.



##########
test/optional_unittest.cpp:
##########
@@ -105,6 +139,102 @@ TEST(OptionalTest, move) {
     butil::optional<int> empty_move_assign;
     empty_move_assign = std::move(empty);
     ASSERT_FALSE(empty_move_assign);
+
+    butil::optional<std::string> string_source(std::string(128, 'x'));
+    butil::optional<std::string> string_moved(std::move(string_source));
+    ASSERT_TRUE(string_source);
+    ASSERT_EQ(std::string(128, 'x'), *string_moved);
+
+    butil::optional<std::string> string_move_source(std::string(128, 'y'));
+    string_moved = std::move(string_move_source);
+    ASSERT_TRUE(string_move_source);
+    ASSERT_TRUE(string_moved);
+    ASSERT_EQ(std::string(128, 'y'), *string_moved);
+}
+
+TEST(OptionalTest, nontrivial_lifetime) {
+    LifetimeTracker::Reset();
+    {
+        butil::optional<LifetimeTracker> source(butil::in_place);
+        butil::optional<LifetimeTracker> copied(source);
+        butil::optional<LifetimeTracker> moved(std::move(source));
+        butil::optional<LifetimeTracker> assigned;
+        assigned = copied;
+        assigned = butil::nullopt;
+    }
+    ASSERT_EQ(0, LifetimeTracker::live);
+    ASSERT_EQ(LifetimeTracker::constructed, LifetimeTracker::destroyed);
+}
+
+TEST(OptionalTest, nontrivial_assignment_states) {
+    const std::string value(128, 'x');
+    butil::optional<std::string> source(value);
+    butil::optional<std::string> empty;
+    butil::optional<std::string> target;
+
+    target = empty;
+    ASSERT_FALSE(target);
+    target = source;
+    ASSERT_EQ(value, *target);
+    target = source;
+    ASSERT_EQ(value, *target);
+    target = empty;
+    ASSERT_FALSE(target);
+
+    target = std::move(empty);
+    ASSERT_FALSE(target);
+    target = std::move(source);
+    ASSERT_EQ(value, *target);
+    source = value;
+    target = std::move(source);
+    ASSERT_EQ(value, *target);
+    target = std::move(empty);
+    ASSERT_FALSE(target);
+}
+
+TEST(OptionalTest, move_only_value) {
+    butil::optional<std::unique_ptr<int>> source(std::unique_ptr<int>(new 
int(42)));
+    butil::optional<std::unique_ptr<int>> moved(std::move(source));
+    ASSERT_EQ(42, **moved);
+    butil::optional<std::unique_ptr<int>> assigned;
+    assigned = std::move(moved);
+    ASSERT_EQ(42, **assigned);
+}
+
+struct CopyConstructOnly {
+    CopyConstructOnly() = default;
+    CopyConstructOnly(const CopyConstructOnly&) = default;
+    CopyConstructOnly(CopyConstructOnly&&) = delete;
+    CopyConstructOnly& operator=(const CopyConstructOnly&) = delete;
+    CopyConstructOnly& operator=(CopyConstructOnly&&) = delete;
+};
+
+struct AssignOnly {
+    AssignOnly() = default;
+    AssignOnly(const AssignOnly&) = delete;
+    AssignOnly(AssignOnly&&) = delete;
+    AssignOnly& operator=(const AssignOnly&) = default;
+    AssignOnly& operator=(AssignOnly&&) = default;
+};
+
+TEST(OptionalTest, copy_move_traits_follow_value_type) {
+    using MoveOnly = butil::optional<std::unique_ptr<int>>;
+    static_assert(!std::is_copy_constructible<MoveOnly>::value, "");
+    static_assert(!std::is_copy_assignable<MoveOnly>::value, "");
+    static_assert(std::is_move_constructible<MoveOnly>::value, "");
+    static_assert(std::is_move_assignable<MoveOnly>::value, "");
+
+    using CopyOnly = butil::optional<CopyConstructOnly>;
+    static_assert(std::is_copy_constructible<CopyOnly>::value, "");
+    static_assert(std::is_move_constructible<CopyOnly>::value, "");

Review Comment:
   This assertion cannot compile: the explicitly deleted 
`CopyConstructOnly(CopyConstructOnly&&)` makes 
`std::is_move_constructible<CopyConstructOnly>` false, so 
`OptionalMoveConstructible<false>` deletes the optional's move constructor as 
well. Either assert false for this type or remove the deleted move declaration 
if the test is intended to exercise copy-from-rvalue fallback.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to