c++: operator<=> and -Wzero-as-null-pointer-constant [PR95242]
authorJason Merrill <jason@redhat.com>
Tue, 26 May 2020 21:27:55 +0000 (17:27 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 27 May 2020 14:05:57 +0000 (10:05 -0400)
In C++20, if there is no viable operator< available, lhs < rhs gets
rewritten to (lhs <=> rhs) < 0, where operator< for the comparison
categories is intended to accept literal 0 on the RHS but not other
integers.  We don't want this to produce a warning from
-Wzero-as-null-pointer-constant.

gcc/cp/ChangeLog:

* call.c (build_new_op_1): Suppress
warn_zero_as_null_pointer_constant across comparison of <=> result
to 0.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/spaceship-synth2.C: Add
-Wzero-as-null-pointer-constant.

gcc/cp/call.c
gcc/testsuite/g++.dg/cpp2a/spaceship-synth2.C

index d8582883917bdd71cd2b3ce3b7c4cad95ef93168..a51ebb5d9e39a1c3d4c9a2e28a89682f998d9796 100644 (file)
@@ -6410,6 +6410,7 @@ build_new_op_1 (const op_location_t &loc, enum tree_code code, int flags,
                    tree rhs = integer_zero_node;
                    if (cand->reversed ())
                      std::swap (lhs, rhs);
+                   warning_sentinel ws (warn_zero_as_null_pointer_constant);
                    result = build_new_op (loc, code,
                                           LOOKUP_NORMAL|LOOKUP_REWRITTEN,
                                           lhs, rhs, NULL_TREE,
index e6401d29ef0d89de52ee3ca9786d6cec6c28bbbe..9b6cfa081d1620b112d46dde89ac5d67f27673a4 100644 (file)
@@ -1,6 +1,9 @@
 // Test with only spaceship defaulted.
 // { dg-do run { target c++20 } }
 
+// Add this warning to test PR c++/95242
+// { dg-additional-options -Wzero-as-null-pointer-constant }
+
 #include <compare>
 
 struct D