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


##########
src/butil/containers/optional.h:
##########
@@ -240,9 +251,35 @@ class optional {
         return *this;
     }
 
-    optional& operator=(const optional& rhs) = default;
+    optional& operator=(const optional& rhs) {
+        if (this != &rhs) {
+            if (!rhs._engaged) {
+                reset();
+            } else if (_engaged) {
+                *_storage = *rhs._storage;
+            } else {
+                _storage.Init(*rhs._storage);
+                _engaged = true;
+            }
+        }
+        return *this;
+    }
 
-    optional& operator=(optional&& rhs) = default;
+    optional& operator=(optional&& rhs) noexcept(
+        std::is_nothrow_move_constructible<T>::value &&
+        std::is_nothrow_move_assignable<T>::value) {
+        if (this != &rhs) {
+            if (!rhs._engaged) {
+                reset();
+            } else if (_engaged) {
+                *_storage = std::move(*rhs._storage);
+            } else {
+                _storage.Init(std::move(*rhs._storage));
+                _engaged = true;
+            }
+        }
+        return *this;
+    }

Review Comment:
   These user-defined assignment operators are always declared, which can make 
`optional<T>` appear copy/move-assignable (e.g., via type traits) even when `T` 
is not copy/move-assignable, because the operator is not implicitly deleted 
like a `= default` would be. This can lead to surprising compile failures when 
the operator is actually ODR-used and the body instantiates `T`’s assignment. 
Consider conditionally enabling these operators (or explicitly `= delete` them) 
based on `std::is_copy_assignable<T>` / `std::is_move_assignable<T>` (and 
constructibility where needed) so `optional<T>`’s assignability traits remain 
correct.



##########
.github/workflows/ci-linux.yml:
##########
@@ -387,6 +387,13 @@ jobs:
     - uses: ./.github/actions/init-ut-make-config
       with:
         options: --with-bthread-tracer --with-asan
+    - name: Test C++14 optional with sanitizers
+      run: |
+        clang++-12 -std=c++14 -DBRPC_TEST_OPTIONAL_CXX14 \
+          -fsanitize=address,undefined -fno-omit-frame-pointer -I src \
+          test/optional_unittest.cpp -lgtest_main -lgtest -pthread \

Review Comment:
   This step hard-pins `clang++-12` and links `-lgtest_main -lgtest` without an 
explicit installation/build step or library path, which can be brittle across 
runner images/toolchain updates. Consider using the workflow’s configured 
compiler (e.g., `clang++` from a toolchain setup step or a matrix variable) and 
building/running the test via the existing project build system (or explicitly 
installing/building GTest and adding the appropriate include/library paths) to 
make CI more reliable.



##########
src/butil/containers/optional.h:
##########
@@ -141,9 +141,20 @@ class optional {
 
     optional(nullopt_t) : optional() {}
 
-    optional(const optional& rhs) = default;
+    optional(const optional& rhs) : _engaged(false) {
+        if (rhs._engaged) {
+            _storage.Init(*rhs._storage);
+            _engaged = true;
+        }
+    }
 
-    optional(optional&& rhs) noexcept = default;
+    optional(optional&& rhs) 
noexcept(std::is_nothrow_move_constructible<T>::value)
+        : _engaged(false) {
+        if (rhs._engaged) {
+            _storage.Init(std::move(*rhs._storage));
+            _engaged = true;
+        }
+    }

Review Comment:
   Move construction is now conditionally `noexcept`, but copy construction 
remains unconditionally potentially-throwing. To keep exception traits accurate 
(and allow better optimization/behavior in containers), consider adding 
`noexcept(std::is_nothrow_copy_constructible<T>::value)` to the copy 
constructor as well (and similarly consider conditional `noexcept` for copy 
assignment if the project cares about nothrow traits).



-- 
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