When implementing DR 1512 in r11-467 I neglected to reject ordered
comparison of two null pointers, like nullptr < nullptr.  This patch
fixes that omission.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

        DR 1512
        PR c++/99701

gcc/cp/ChangeLog:

        * cp-gimplify.c (cp_fold): Remove {LE,LT,GE,GT_EXPR} from
        a switch.
        * typeck.c (cp_build_binary_op): Reject ordered comparison
        of two null pointers.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp0x/nullptr11.C: Remove invalid tests.
        * g++.dg/cpp0x/nullptr46.C: Add dg-error.
        * g++.dg/expr/ptr-comp4.C: New test.

libstdc++-v3/ChangeLog:

        * testsuite/20_util/tuple/comparison_operators/overloaded.cc:
        Add dg-error.
---
 gcc/cp/cp-gimplify.c                          |  4 ----
 gcc/cp/typeck.c                               | 15 +++----------
 gcc/testsuite/g++.dg/cpp0x/nullptr11.C        | 16 --------------
 gcc/testsuite/g++.dg/cpp0x/nullptr46.C        |  3 ++-
 gcc/testsuite/g++.dg/expr/ptr-comp4.C         | 21 +++++++++++++++++++
 .../tuple/comparison_operators/overloaded.cc  |  2 ++
 6 files changed, 28 insertions(+), 33 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/expr/ptr-comp4.C

diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index ff0bff771df..0520fa45b91 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -2433,13 +2433,9 @@ cp_fold (tree x)
          switch (code)
            {
            case EQ_EXPR:
-           case LE_EXPR:
-           case GE_EXPR:
              x = constant_boolean_node (true, TREE_TYPE (x));
              break;
            case NE_EXPR:
-           case LT_EXPR:
-           case GT_EXPR:
              x = constant_boolean_node (false, TREE_TYPE (x));
              break;
            default:
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index a483e1f988d..738e69a0440 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -5483,7 +5483,9 @@ cp_build_binary_op (const op_location_t &location,
        result_type = composite_pointer_type (location,
                                              type0, type1, op0, op1,
                                              CPO_COMPARISON, complain);
-      else if (code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
+      else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
+              || (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
+              || (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
        {
          /* Core Issue 1512 made this ill-formed.  */
          if (complain & tf_error)
@@ -5491,17 +5493,6 @@ cp_build_binary_op (const op_location_t &location,
                      "integer zero (%qT and %qT)", type0, type1);
          return error_mark_node;
        }
-      else if (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
-       {
-         /* Core Issue 1512 made this ill-formed.  */
-         if (complain & tf_error)
-           error_at (location, "ordered comparison of pointer with "
-                     "integer zero (%qT and %qT)", type0, type1);
-         return error_mark_node;
-       }
-      else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
-       /* One of the operands must be of nullptr_t type.  */
-        result_type = TREE_TYPE (nullptr_node);
       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
        {
          result_type = type0;
diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr11.C 
b/gcc/testsuite/g++.dg/cpp0x/nullptr11.C
index f81f0c3c1c8..b8bc682f2ea 100644
--- a/gcc/testsuite/g++.dg/cpp0x/nullptr11.C
+++ b/gcc/testsuite/g++.dg/cpp0x/nullptr11.C
@@ -9,31 +9,15 @@ void fun()
 {
   assert_true(nullptr == nullptr);
   assert_false(nullptr != nullptr);
-  assert_false(nullptr < nullptr);
-  assert_false(nullptr > nullptr);
-  assert_true(nullptr <= nullptr);
-  assert_true(nullptr >= nullptr);
 
   decltype(nullptr) mynull = 0;
 
   assert_true(mynull == nullptr);
   assert_false(mynull != nullptr);
-  assert_false(mynull < nullptr);
-  assert_false(mynull > nullptr);
-  assert_true(mynull <= nullptr);
-  assert_true(mynull >= nullptr);
 
   assert_true(nullptr == mynull);
   assert_false(nullptr != mynull);
-  assert_false(nullptr < mynull);
-  assert_false(nullptr > mynull);
-  assert_true(nullptr <= mynull);
-  assert_true(nullptr >= mynull);
 
   assert_true(mynull == mynull);
   assert_false(mynull != mynull);
-  assert_false(mynull < mynull);
-  assert_false(mynull > mynull);
-  assert_true(mynull <= mynull);
-  assert_true(mynull >= mynull);
 }
diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr46.C 
b/gcc/testsuite/g++.dg/cpp0x/nullptr46.C
index 1514cee3c3b..6c08eaa4d8f 100644
--- a/gcc/testsuite/g++.dg/cpp0x/nullptr46.C
+++ b/gcc/testsuite/g++.dg/cpp0x/nullptr46.C
@@ -7,5 +7,6 @@ decltype(nullptr) foo ();
 bool
 bar ()
 {
-  return foo () > nullptr || foo () < nullptr;
+  return foo () > nullptr // { dg-error "ordered comparison" }
+    || foo () < nullptr; // { dg-error "ordered comparison" }
 }
diff --git a/gcc/testsuite/g++.dg/expr/ptr-comp4.C 
b/gcc/testsuite/g++.dg/expr/ptr-comp4.C
new file mode 100644
index 00000000000..da4951806c1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/expr/ptr-comp4.C
@@ -0,0 +1,21 @@
+// PR c++/99701
+// DR 1512
+// { dg-do compile { target c++11 } }
+
+/* Relational comparisons between null pointer constants and pointers are
+   ill-formed.  */
+
+decltype(nullptr) foo ();
+
+auto lt = nullptr < nullptr; // { dg-error "ordered comparison" }
+auto gt = nullptr > nullptr; // { dg-error "ordered comparison" }
+auto le = nullptr <= nullptr; // { dg-error "ordered comparison" }
+auto ge = nullptr >= nullptr; // { dg-error "ordered comparison" }
+auto eq = nullptr == nullptr;
+auto ne = nullptr != nullptr;
+auto a1 = nullptr > 0; // { dg-error "ordered comparison" }
+auto a2 = 0 < nullptr; // { dg-error "ordered comparison" }
+auto a3 = foo () > 0; // { dg-error "ordered comparison" }
+auto a4 = 0 < foo (); // { dg-error "ordered comparison" }
+auto a5 = 0 <= foo (); // { dg-error "ordered comparison" }
+auto a6 = foo () >= 0; // { dg-error "ordered comparison" }
diff --git 
a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc 
b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc
index a25c374a932..25bfdfa00f4 100644
--- a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc
+++ b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc
@@ -49,3 +49,5 @@ TwistedLogic operator<(const Compares&, const Compares&) { 
return {false}; }
 auto a = std::make_tuple(nullptr, Compares{}, 2, 'U');
 auto b = a == a;
 auto c = a < a;
+
+// { dg-error "ordered comparison" "" { target *-*-* } 0 }

base-commit: 94ba897be8b59ef5926eed4c77fd53812fb20add
-- 
2.31.1

Reply via email to