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:
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)
"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;
{
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);
}
bool
bar ()
{
- return foo () > nullptr || foo () < nullptr;
+ return foo () > nullptr // { dg-error "ordered comparison" }
+ || foo () < nullptr; // { dg-error "ordered comparison" }
}
--- /dev/null
+// PR c++/99701
+// { dg-do compile { target c++20 } }
+
+#include <compare>
+
+decltype(nullptr) foo ();
+
+auto b0 = nullptr <=> nullptr; // { dg-error "ordered comparison" }
+auto b1 = 0 <=> nullptr; // { dg-error "ordered comparison" }
+auto b2 = nullptr <=> 0; // { dg-error "ordered comparison" }
+auto b3 = foo () <=> 0; // { dg-error "ordered comparison" }
+auto b4 = 0 <=> foo (); // { dg-error "ordered comparison" }
+auto b5 = foo () <=> nullptr; // { dg-error "ordered comparison" }
+auto b6 = nullptr <=> foo (); // { dg-error "ordered comparison" }
--- /dev/null
+// 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" }
auto a = std::make_tuple(nullptr, Compares{}, 2, 'U');
auto b = a == a;
-auto c = a < a;
--- /dev/null
+// { dg-do compile { target c++11 } }
+
+// Copyright (C) 2014-2021 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <tuple>
+
+// A type that is contextually convertible to bool but cannot be used with
+// the usual logical operators, and/or/not.
+struct TwistedLogic {
+ bool value;
+
+ explicit operator bool() const noexcept { return value; }
+};
+
+template<typename T>
+bool operator&&(const T&, TwistedLogic) = delete;
+
+template<typename T>
+bool operator&&(TwistedLogic, const T&) = delete;
+
+template<typename T>
+bool operator||(const T&, TwistedLogic) = delete;
+
+template<typename T>
+bool operator||(TwistedLogic, const T&) = delete;
+
+bool operator!(TwistedLogic) noexcept = delete;
+
+struct Compares {};
+
+TwistedLogic operator==(const Compares&, const Compares&) { return {true}; }
+TwistedLogic operator<(const Compares&, const Compares&) { return {false}; }
+
+auto a = std::make_tuple(nullptr, Compares{}, 2, 'U');
+auto b = a < a;
+
+// { dg-error "ordered comparison" "" { target *-*-* } 0 }