[libcxx-commits] [PATCH] D146372: [libc++]Don't warn when using operator<=> with 0

Igor Zhukov via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sat Mar 18 19:19:20 PDT 2023


fsb4000 created this revision.
fsb4000 added reviewers: libc++, Mordante, philnik, ldionne.
fsb4000 added a project: libc++.
Herald added a subscriber: mikhail.ramalho.
Herald added a project: All.
fsb4000 requested review of this revision.
Herald added a subscriber: libcxx-commits.

Fixes https://github.com/llvm/llvm-project/issues/43670

>From https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0768r1.pdf :

> The relational and equality operators for the comparison category types are specified with an
> anonymous parameter of unspecified type. This type shall be selected by the implementation such
> that these parameters can accept literal 0 as a corresponding argument. [Example: nullptr_t
> satisfies this requirement. — end example] In this context, the behavior of a program that supplies
> an argument other than a literal 0 is undefined

So after this patch we will accept not literal 0 but consteval zero like

  #include <compare>
  
  consteval int test() { return 0;}
  auto b = 1 <=> 2 < test();

But before we accepted `nullptr`. So I guess it's fine and the behavior is undefined in the both cases.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146372

Files:
  libcxx/include/__compare/ordering.h
  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


Index: libcxx/test/std/language.support/cmp/cmp.weakord/weakord.pass.cpp
===================================================================
--- libcxx/test/std/language.support/cmp/cmp.weakord/weakord.pass.cpp
+++ libcxx/test/std/language.support/cmp/cmp.weakord/weakord.pass.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 // UNSUPPORTED: c++03, c++11, c++14, c++17
+// ADDITIONAL_COMPILE_FLAGS: -Wzero-as-null-pointer-constant
 
 // <compare>
 
Index: libcxx/test/std/language.support/cmp/cmp.strongord/strongord.pass.cpp
===================================================================
--- libcxx/test/std/language.support/cmp/cmp.strongord/strongord.pass.cpp
+++ libcxx/test/std/language.support/cmp/cmp.strongord/strongord.pass.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 // UNSUPPORTED: c++03, c++11, c++14, c++17
+// ADDITIONAL_COMPILE_FLAGS: -Wzero-as-null-pointer-constant
 
 // <compare>
 
Index: libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp
===================================================================
--- libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp
+++ libcxx/test/std/language.support/cmp/cmp.partialord/partialord.pass.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 // UNSUPPORTED: c++03, c++11, c++14, c++17
+// ADDITIONAL_COMPILE_FLAGS: -Wzero-as-null-pointer-constant
 
 // <compare>
 
Index: libcxx/include/__compare/ordering.h
===================================================================
--- libcxx/include/__compare/ordering.h
+++ libcxx/include/__compare/ordering.h
@@ -40,8 +40,12 @@
 inline constexpr bool __one_of_v = (is_same_v<_Tp, _Args> || ...);
 
 struct _CmpUnspecifiedParam {
-  _LIBCPP_HIDE_FROM_ABI constexpr
-  _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
+  _LIBCPP_HIDE_FROM_ABI consteval
+  _CmpUnspecifiedParam(int __literal_zero) noexcept {
+    if (__literal_zero != 0) {
+      __builtin_abort();
+    }
+  }
 
   template<class _Tp, class = enable_if_t<!__one_of_v<_Tp, int, partial_ordering, weak_ordering, strong_ordering>>>
   _CmpUnspecifiedParam(_Tp) = delete;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146372.506343.patch
Type: text/x-patch
Size: 2274 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230319/955631f3/attachment.bin>


More information about the libcxx-commits mailing list