[libcxx-commits] [libcxx] [libc++] restrict the expected conversion constructor not compete against copy constructor (PR #96101)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jun 19 11:51:31 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Hui (huixie90)

<details>
<summary>Changes</summary>

fixes #<!-- -->92676


---
Full diff: https://github.com/llvm/llvm-project/pull/96101.diff


2 Files Affected:

- (modified) libcxx/include/__expected/expected.h (+3-1) 
- (modified) libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp (+18) 


``````````diff
diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 0f994e297a877..da0a0fa85714c 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -507,7 +507,9 @@ class expected : private __expected_base<_Tp, _Err> {
       _And< is_constructible<_Tp, _UfQual>,
             is_constructible<_Err, _OtherErrQual>,
             _If<_Not<is_same<remove_cv_t<_Tp>, bool>>::value,
-                _And< _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,
+                _And< 
+                      _Not<_And<is_same<_Tp, _Up>, is_same<_Err, _OtherErr>>>,
+                      _Not<is_constructible<_Tp, expected<_Up, _OtherErr>&>>,
                       _Not<is_constructible<_Tp, expected<_Up, _OtherErr>>>,
                       _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>&>>,
                       _Not<is_constructible<_Tp, const expected<_Up, _OtherErr>>>,
diff --git a/libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp b/libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp
index 581df51207da2..6c278be9de98a 100644
--- a/libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp
+++ b/libcxx/test/std/utilities/expected/expected.expected/ctor/ctor.copy.pass.cpp
@@ -62,6 +62,17 @@ static_assert(!std::is_trivially_copy_constructible_v<std::expected<CopyableNonT
 static_assert(!std::is_trivially_copy_constructible_v<std::expected<int, CopyableNonTrivial>>);
 static_assert(!std::is_trivially_copy_constructible_v<std::expected<CopyableNonTrivial, CopyableNonTrivial>>);
 
+
+struct Any {
+  constexpr Any() = default;
+  constexpr Any(const Any&) = default;
+  constexpr Any& operator=(const Any&)=default;
+
+  template <class T>
+    requires (!std::is_same_v<Any, std::decay_t<T>> && std::is_copy_constructible_v<std::decay_t<T>>)
+  constexpr Any(T&&){}
+};
+
 constexpr bool test() {
   // copy the value non-trivial
   {
@@ -109,6 +120,13 @@ constexpr bool test() {
     assert(!e2.has_value());
   }
 
+  {
+    // https://github.com/llvm/llvm-project/issues/92676
+    std::expected<Any, int> e1;
+    auto e2 = e1;
+    assert(e2.has_value());
+  }
+
   return true;
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/96101


More information about the libcxx-commits mailing list