https://gcc.gnu.org/g:882ec218ac1330cfada7266a1df06f626a2c0781

commit r16-8899-g882ec218ac1330cfada7266a1df06f626a2c0781
Author: Jason Merrill <[email protected]>
Date:   Tue May 12 15:25:29 2026 -0400

    c++: -Wzero-as-null-pointer-constant and <=> [PR100903]
    
    (x <=> y) < 0 is the pattern suggested by the standard, we shouldn't warn
    about it because of our implementation strategy.
    
    We already disable the warning in <compare> to avoid warnings on the uses of
    this pattern within the header, so no library changes are needed.
    
            PR c++/100903
    
    gcc/cp/ChangeLog:
    
            * call.cc (build_over_call): Avoid -Wzero-as-null-pointer-constant
            if the warning is disabled around the called function.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/spaceship-warn1.C: New test.
    
    (cherry picked from commit 03bf757085091d95a9fe4ab964ee62da157ef563)

Diff:
---
 gcc/cp/call.cc                               | 16 ++++++++++++----
 gcc/testsuite/g++.dg/cpp2a/spaceship-warn1.C | 10 ++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 92c3a23ab8d9..97cb0d6f0124 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -10715,10 +10715,18 @@ build_over_call (struct z_candidate *cand, int flags, 
tsubst_flags_t complain)
              func(NULL);
            }
       */
-      bool const conversion_warning = !(null_node_p (current_arg)
-                                       && DECL_TEMPLATE_INFO (fn)
-                                       && cand->template_decl
-                                       && !cand->explicit_targs);
+      bool conversion_warning = !(null_node_p (current_arg)
+                                 && DECL_TEMPLATE_INFO (fn)
+                                 && cand->template_decl
+                                 && !cand->explicit_targs);
+
+      /* Also don't warn about (x <=> y) < 0 (c++/100903).  */
+      if (conversion_warning
+         && integer_zerop (current_arg)
+         && warn_zero_as_null_pointer_constant
+         && !warning_enabled_at (DECL_SOURCE_LOCATION (fn),
+                                 OPT_Wzero_as_null_pointer_constant))
+       conversion_warning = false;
 
       tsubst_flags_t const arg_complain
        = conversion_warning ? complain : complain & ~tf_warning;
diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-warn1.C 
b/gcc/testsuite/g++.dg/cpp2a/spaceship-warn1.C
new file mode 100644
index 000000000000..998abef6543d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-warn1.C
@@ -0,0 +1,10 @@
+// PR c++/100903
+// { dg-additional-options -Wzero-as-null-pointer-constant }
+// { dg-do compile { target c++20 } }
+
+#include <compare>
+
+int main()
+{
+  return (1.0 <=> 2.0) < 0;
+}

Reply via email to