From a83d113f663a40a10bf2222cf07d665743f70190 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 5 Aug 2024 17:40:38 +0200 Subject: [PATCH] Fix partial_ordering::unordered != 0 comparison The standard does not provide any details on implementing operator!=(), because it relies on the fact that it will be synthesized by the compiler. Our C++17 implementation, however, has to provide operator!=(), and we made the mistake of copy-pasting the implementation from other operators. However, the isOrdered() check does not make sense for operator!=(), because an unordered value is never equal to literal zero. Fix the implementation for both Qt::partial_ordering and legacy QPartialOrdering. Amends 405244fe301ac18d20aae245ba2faafaec74e453 (for QPartialOrdering) and bdd41f491c0f85ae0897a1c7372c5ecda62a5aab (for Qt::partial_ordering). [ChangeLog][QtCore][QtCompare] Fixed a bug where partial_ordering::unordered != 0 comparison produced an incorrect result. Fixes: QTBUG-127759 Pick-to: 6.7 6.5 6.2 5.15 Change-Id: I5008f72831c17dc7fa4ae181bfc8115198a691f0 Reviewed-by: Marc Mutz (cherry picked from commit d39441a2eb5658f3d4a01046c592adb827525118) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/global/qcompare.h | 8 +++---- .../corelib/global/qcompare/tst_qcompare.cpp | 24 +++++-------------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/corelib/global/qcompare.h b/src/corelib/global/qcompare.h index 410605455b8..fd9fb84c6b9 100644 --- a/src/corelib/global/qcompare.h +++ b/src/corelib/global/qcompare.h @@ -79,7 +79,7 @@ public: friend constexpr bool operator!=(partial_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept - { return lhs.isOrdered() && lhs.m_order != 0; } + { return !lhs.isOrdered() || lhs.m_order != 0; } friend constexpr bool operator< (partial_ordering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept @@ -104,7 +104,7 @@ public: friend constexpr bool operator!=(QtPrivate::CompareAgainstLiteralZero, partial_ordering rhs) noexcept - { return rhs.isOrdered() && 0 != rhs.m_order; } + { return !rhs.isOrdered() || 0 != rhs.m_order; } friend constexpr bool operator< (QtPrivate::CompareAgainstLiteralZero, partial_ordering rhs) noexcept @@ -721,7 +721,7 @@ public: friend constexpr bool operator!=(QPartialOrdering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept - { return lhs.isOrdered() && lhs.m_order != 0; } + { return !lhs.isOrdered() || lhs.m_order != 0; } friend constexpr bool operator< (QPartialOrdering lhs, QtPrivate::CompareAgainstLiteralZero) noexcept @@ -746,7 +746,7 @@ public: friend constexpr bool operator!=(QtPrivate::CompareAgainstLiteralZero, QPartialOrdering rhs) noexcept - { return rhs.isOrdered() && 0 != rhs.m_order; } + { return !rhs.isOrdered() || 0 != rhs.m_order; } friend constexpr bool operator< (QtPrivate::CompareAgainstLiteralZero, QPartialOrdering rhs) noexcept diff --git a/tests/auto/corelib/global/qcompare/tst_qcompare.cpp b/tests/auto/corelib/global/qcompare/tst_qcompare.cpp index d28833abb16..8d764ad5257 100644 --- a/tests/auto/corelib/global/qcompare/tst_qcompare.cpp +++ b/tests/auto/corelib/global/qcompare/tst_qcompare.cpp @@ -53,21 +53,21 @@ void tst_QCompare::legacyPartialOrdering() static_assert(QPartialOrdering::Greater == QPartialOrdering::Greater); static_assert(!is_eq (QPartialOrdering::Unordered)); - static_assert(!is_neq (QPartialOrdering::Unordered)); + static_assert( is_neq (QPartialOrdering::Unordered)); static_assert(!is_lt (QPartialOrdering::Unordered)); static_assert(!is_lteq(QPartialOrdering::Unordered)); static_assert(!is_gt (QPartialOrdering::Unordered)); static_assert(!is_gteq(QPartialOrdering::Unordered)); static_assert(!(QPartialOrdering::Unordered == 0)); - static_assert(!(QPartialOrdering::Unordered != 0)); + static_assert( (QPartialOrdering::Unordered != 0)); static_assert(!(QPartialOrdering::Unordered < 0)); static_assert(!(QPartialOrdering::Unordered <= 0)); static_assert(!(QPartialOrdering::Unordered > 0)); static_assert(!(QPartialOrdering::Unordered >= 0)); static_assert(!(0 == QPartialOrdering::Unordered)); - static_assert(!(0 != QPartialOrdering::Unordered)); + static_assert( (0 != QPartialOrdering::Unordered)); static_assert(!(0 < QPartialOrdering::Unordered)); static_assert(!(0 <= QPartialOrdering::Unordered)); static_assert(!(0 > QPartialOrdering::Unordered)); @@ -225,21 +225,21 @@ void tst_QCompare::partialOrdering() static_assert(Qt::partial_ordering::greater == Qt::partial_ordering::greater); static_assert(!is_eq (Qt::partial_ordering::unordered)); - static_assert(!is_neq (Qt::partial_ordering::unordered)); + static_assert( is_neq (Qt::partial_ordering::unordered)); static_assert(!is_lt (Qt::partial_ordering::unordered)); static_assert(!is_lteq(Qt::partial_ordering::unordered)); static_assert(!is_gt (Qt::partial_ordering::unordered)); static_assert(!is_gteq(Qt::partial_ordering::unordered)); static_assert(!(Qt::partial_ordering::unordered == 0)); - static_assert(!(Qt::partial_ordering::unordered != 0)); + static_assert( (Qt::partial_ordering::unordered != 0)); static_assert(!(Qt::partial_ordering::unordered < 0)); static_assert(!(Qt::partial_ordering::unordered <= 0)); static_assert(!(Qt::partial_ordering::unordered > 0)); static_assert(!(Qt::partial_ordering::unordered >= 0)); static_assert(!(0 == Qt::partial_ordering::unordered)); - static_assert(!(0 != Qt::partial_ordering::unordered)); + static_assert( (0 != Qt::partial_ordering::unordered)); static_assert(!(0 < Qt::partial_ordering::unordered)); static_assert(!(0 <= Qt::partial_ordering::unordered)); static_assert(!(0 > Qt::partial_ordering::unordered)); @@ -859,32 +859,20 @@ void tst_QCompare::unorderedNeqLiteralZero() QVERIFY(0 != stdUnordered); QVERIFY(is_neq(stdUnordered)); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QCOMPARE_EQ(qtUnordered != 0, stdUnordered != 0); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QCOMPARE_EQ(0 != qtUnordered, 0 != stdUnordered); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QCOMPARE_EQ(is_neq(qtUnordered), is_neq(stdUnordered)); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QCOMPARE_EQ(qtLegacyUnordered != 0, stdUnordered != 0); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QCOMPARE_EQ(0 != qtLegacyUnordered, 0 != stdUnordered); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QCOMPARE_EQ(is_neq(qtLegacyUnordered), is_neq(stdUnordered)); #endif - QEXPECT_FAIL("", "QTBUG-127759", Continue); QVERIFY(qtUnordered != 0); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QVERIFY(0 != qtUnordered); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QVERIFY(is_neq(qtUnordered)); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QVERIFY(qtLegacyUnordered != 0); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QVERIFY(0 != qtLegacyUnordered); - QEXPECT_FAIL("", "QTBUG-127759", Continue); QVERIFY(is_neq(qtLegacyUnordered)); }