[libcxx-commits] [libcxx] 46c1742 - [libcxx] modifies `_CmpUnspecifiedParam` ignore types outside its domain

Christopher Di Bella via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 12 17:46:14 PDT 2021


Author: Christopher Di Bella
Date: 2021-05-13T00:45:39Z
New Revision: 46c17429bc86dc5ccddb5512b77bd1ede39c9ccd

URL: https://github.com/llvm/llvm-project/commit/46c17429bc86dc5ccddb5512b77bd1ede39c9ccd
DIFF: https://github.com/llvm/llvm-project/commit/46c17429bc86dc5ccddb5512b77bd1ede39c9ccd.diff

LOG: [libcxx] modifies `_CmpUnspecifiedParam` ignore types outside its domain

D85051's honeypot solution was a bit too aggressive swallowed up the
comparison types, which made comparing objects of different ordering
types ambiguous.

Depends on D101707.

Differential Revision: https://reviews.llvm.org/D101708

Added: 
    

Modified: 
    libcxx/include/compare
    libcxx/test/std/language.support/cmp/cmp.categories.pre/zero_type.verify.cpp
    libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp
    libcxx/test/std/language.support/cmp/cmp.strongord/strongord.pass.cpp
    libcxx/test/std/language.support/cmp/cmp.weakord/weakord.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/compare b/libcxx/include/compare
index 493369045a774..c7bfb45f6e290 100644
--- a/libcxx/include/compare
+++ b/libcxx/include/compare
@@ -148,11 +148,18 @@ enum class _LIBCPP_ENUM_VIS _NCmpResult : signed char {
   __unordered = -127
 };
 
+class partial_ordering;
+class weak_ordering;
+class strong_ordering;
+
+template<class _Tp, class... _Args>
+inline constexpr bool __one_of_v = (is_same_v<_Tp, _Args> || ...);
+
 struct _CmpUnspecifiedParam {
   _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEVAL
   _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
 
-  template<typename _Tp, typename = enable_if_t<!is_same_v<_Tp, int>>>
+  template<class _Tp, class = enable_if_t<!__one_of_v<_Tp, int, partial_ordering, weak_ordering, strong_ordering>>>
   _CmpUnspecifiedParam(_Tp) = delete;
 };
 

diff  --git a/libcxx/test/std/language.support/cmp/cmp.categories.pre/zero_type.verify.cpp b/libcxx/test/std/language.support/cmp/cmp.categories.pre/zero_type.verify.cpp
index c0fe4cb9613a0..7fd6749aefd16 100644
--- a/libcxx/test/std/language.support/cmp/cmp.categories.pre/zero_type.verify.cpp
+++ b/libcxx/test/std/language.support/cmp/cmp.categories.pre/zero_type.verify.cpp
@@ -17,40 +17,35 @@
 
 #include <compare>
 
-#define TEST_OP(v, op)                                                         \
+#define TEST_FAIL(v, op)                                                       \
   void(v op 0L);                                                               \
   void(0L op v);                                                               \
   void(v op nullptr);                                                          \
   void(nullptr op v);                                                          \
   void(v op(1 - 1));                                                           \
-  void((1 - 1) op v);
+  void((1 - 1) op v)
+
+#define TEST_PASS(v, op)                                                       \
+  void(v op 0);                                                                \
+  void(0 op v)
 
 template <typename T>
 void test_category(T v) {
-  TEST_OP(v, ==);  // expected-error 18 {{}}
-  TEST_OP(v, !=);  // expected-error 18 {{}}
-  TEST_OP(v, <);   // expected-error 18 {{}}
-  TEST_OP(v, <=);  // expected-error 18 {{}}
-  TEST_OP(v, >);   // expected-error 18 {{}}
-  TEST_OP(v, >=);  // expected-error 18 {{}}
-  TEST_OP(v, <=>); // expected-error 18 {{}}
-
-  void(v == 0);
-  void(0 == v);
-  void(v != 0);
-  void(0 != v);
-  void(v < 0);
-  void(0 < v);
-  void(v <= 0);
-  void(0 <= v);
-  void(v > 0);
-  void(0 > v);
-  void(v >= 0);
-  void(0 >= v);
-#ifndef _LIBCPP_HAS_NO_SPACESHIP_OPERATOR
-  void(v <=> 0);
-  void(0 <=> v);
-#endif
+  TEST_FAIL(v, ==);  // expected-error 18 {{}}
+  TEST_FAIL(v, !=);  // expected-error 18 {{}}
+  TEST_FAIL(v, <);   // expected-error 18 {{}}
+  TEST_FAIL(v, <=);  // expected-error 18 {{}}
+  TEST_FAIL(v, >);   // expected-error 18 {{}}
+  TEST_FAIL(v, >=);  // expected-error 18 {{}}
+  TEST_FAIL(v, <=>); // expected-error 18 {{}}
+
+  TEST_PASS(v, ==);
+  TEST_PASS(v, !=);
+  TEST_PASS(v, <);
+  TEST_PASS(v, >);
+  TEST_PASS(v, <=);
+  TEST_PASS(v, >=);
+  TEST_PASS(v, <=>);
 }
 
 int main(int, char**) {

diff  --git a/libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp b/libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp
index 0b5169f4ac4f7..60f4c97239f05 100644
--- a/libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp
+++ b/libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp
@@ -52,6 +52,17 @@ void test_signatures() {
 #endif
 }
 
+constexpr void test_equality() {
+#ifndef TEST_HAS_NO_SPACESHIP_OPERATOR
+  auto& PartialEq = std::partial_ordering::equivalent;
+  auto& WeakEq = std::weak_ordering::equivalent;
+  assert(PartialEq == WeakEq);
+
+  auto& StrongEq = std::strong_ordering::equal;
+  assert(PartialEq == StrongEq);
+#endif
+}
+
 constexpr bool test_constexpr() {
   auto& Eq = std::partial_ordering::equivalent;
   auto& Less = std::partial_ordering::less;
@@ -167,6 +178,8 @@ constexpr bool test_constexpr() {
     static_assert(std::partial_ordering::unordered ==
                   std::partial_ordering::unordered);
   }
+
+  test_equality();
 #endif
 
   return true;
@@ -175,6 +188,7 @@ constexpr bool test_constexpr() {
 int main(int, char**) {
   test_static_members();
   test_signatures();
+  test_equality();
   static_assert(test_constexpr(), "constexpr test failed");
 
   return 0;

diff  --git a/libcxx/test/std/language.support/cmp/cmp.strongord/strongord.pass.cpp b/libcxx/test/std/language.support/cmp/cmp.strongord/strongord.pass.cpp
index e8b973344485f..a0e5037de44d3 100644
--- a/libcxx/test/std/language.support/cmp/cmp.strongord/strongord.pass.cpp
+++ b/libcxx/test/std/language.support/cmp/cmp.strongord/strongord.pass.cpp
@@ -52,6 +52,17 @@ void test_signatures() {
 #endif
 }
 
+constexpr void test_equality() {
+#ifndef TEST_HAS_NO_SPACESHIP_OPERATOR
+  auto& StrongEq = std::strong_ordering::equal;
+  auto& PartialEq = std::partial_ordering::equivalent;
+  assert(StrongEq == PartialEq);
+
+  auto& WeakEq = std::weak_ordering::equivalent;
+  assert(StrongEq == WeakEq);
+#endif
+}
+
 constexpr bool test_conversion() {
   static_assert(std::is_convertible<const std::strong_ordering&,
       std::partial_ordering>::value, "");
@@ -175,6 +186,8 @@ constexpr bool test_constexpr() {
     static_assert(std::strong_ordering::greater ==
                   std::strong_ordering::greater);
   }
+
+  test_equality();
 #endif
 
   return true;
@@ -183,6 +196,7 @@ constexpr bool test_constexpr() {
 int main(int, char**) {
   test_static_members();
   test_signatures();
+  test_equality();
   static_assert(test_conversion(), "conversion test failed");
   static_assert(test_constexpr(), "constexpr test failed");
 

diff  --git a/libcxx/test/std/language.support/cmp/cmp.weakord/weakord.pass.cpp b/libcxx/test/std/language.support/cmp/cmp.weakord/weakord.pass.cpp
index d7f19ce18f759..4f560dbbc2f2f 100644
--- a/libcxx/test/std/language.support/cmp/cmp.weakord/weakord.pass.cpp
+++ b/libcxx/test/std/language.support/cmp/cmp.weakord/weakord.pass.cpp
@@ -72,6 +72,17 @@ constexpr bool test_conversion() {
   return true;
 }
 
+constexpr void test_equality() {
+#ifndef TEST_HAS_NO_SPACESHIP_OPERATOR
+  auto& WeakEq = std::weak_ordering::equivalent;
+  auto& PartialEq = std::partial_ordering::equivalent;
+  assert(WeakEq == PartialEq);
+
+  auto& StrongEq = std::strong_ordering::equal;
+  assert(WeakEq == StrongEq);
+#endif
+}
+
 constexpr bool test_constexpr() {
   auto& Eq = std::weak_ordering::equivalent;
   auto& Less = std::weak_ordering::less;
@@ -157,6 +168,8 @@ constexpr bool test_constexpr() {
                   std::weak_ordering::equivalent);
     static_assert(std::weak_ordering::greater == std::weak_ordering::greater);
   }
+
+  test_equality();
 #endif
 
   return true;
@@ -165,6 +178,7 @@ constexpr bool test_constexpr() {
 int main(int, char**) {
   test_static_members();
   test_signatures();
+  test_equality();
   static_assert(test_conversion(), "conversion test failed");
   static_assert(test_constexpr(), "constexpr test failed");
 


        


More information about the libcxx-commits mailing list