From a32424b46cd3ef6fd8d644354e3cec434d5ab951 Mon Sep 17 00:00:00 2001
From: Edward Welbourne <edward.welbourne@qt.io>
Date: Tue, 15 Nov 2016 17:15:56 +0100
Subject: [PATCH] AreArgumentsNarrowedBase: Correct logic for narrowing
 connect() casts

The prior test deemed there to be narrowing if source and destination
integral-or-enum types didn't have the same signedness; but all values
of an unsigned source type can be represented in a larger signed
destination type, so there is no narrowing in this case.
Updated QObject test-case to match.

Change-Id: I517a5997adcad70e185d7469a8d26788e463cb75
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
---
 src/corelib/kernel/qobjectdefs_impl.h         | 10 ++++-
 .../corelib/kernel/qobject/tst_qobject.cpp    | 40 +++++++++----------
 2 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/src/corelib/kernel/qobjectdefs_impl.h b/src/corelib/kernel/qobjectdefs_impl.h
index e94e713e1f..79c9c8303e 100644
--- a/src/corelib/kernel/qobjectdefs_impl.h
+++ b/src/corelib/kernel/qobjectdefs_impl.h
@@ -229,8 +229,14 @@ namespace QtPrivate {
               (std::is_floating_point<From>::value && std::is_integral<To>::value) ||
               (std::is_floating_point<From>::value && std::is_floating_point<To>::value && sizeof(From) > sizeof(To)) ||
               ((std::is_integral<From>::value || std::is_enum<From>::value) && std::is_floating_point<To>::value) ||
-              (std::is_integral<From>::value && std::is_integral<To>::value && (sizeof(From) > sizeof(To) || std::is_signed<From>::value != std::is_signed<To>::value)) ||
-              (std::is_enum<From>::value && std::is_integral<To>::value && (sizeof(From) > sizeof(To) || IsEnumUnderlyingTypeSigned<From>::value != std::is_signed<To>::value))
+              (std::is_integral<From>::value && std::is_integral<To>::value
+               && (sizeof(From) > sizeof(To)
+                   || (std::is_signed<From>::value ? !std::is_signed<To>::value
+                       : (std::is_signed<To>::value && sizeof(From) == sizeof(To))))) ||
+              (std::is_enum<From>::value && std::is_integral<To>::value
+               && (sizeof(From) > sizeof(To)
+                   || (IsEnumUnderlyingTypeSigned<From>::value ? !std::is_signed<To>::value
+                       : (std::is_signed<To>::value && sizeof(From) == sizeof(To)))))
               >
     {
     };
diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
index 0f7f75fb2e..91810cdcd8 100644
--- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
+++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
@@ -6969,10 +6969,10 @@ void tst_QObject::checkArgumentsForNarrowing()
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<signed char, unsigned long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned char, signed char>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned char, short>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned char, int>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned char, long>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned char, long long>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<unsigned char, short>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<unsigned char, int>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<unsigned char, long>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<unsigned char, long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<short, unsigned short>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<short, unsigned int>::value));
@@ -6980,23 +6980,23 @@ void tst_QObject::checkArgumentsForNarrowing()
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<short, unsigned long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned short, short>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned short, int>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned short, long>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned short, long long>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<unsigned short, int>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<unsigned short, long>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<unsigned short, long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<int, unsigned int>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<int, unsigned long>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<int, unsigned long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned int, int>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned int, long>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned int, long long>::value));
+    QCOMPARE((QtPrivate::AreArgumentsNarrowedBase<unsigned int, long>::value), sizeof(int) >= sizeof(long));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<unsigned int, long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<long, unsigned long>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<long, unsigned long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned long, long>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned long, long long>::value));
+    QCOMPARE((QtPrivate::AreArgumentsNarrowedBase<unsigned long, long long>::value), sizeof(long) >= sizeof(long long));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<long long, unsigned long long>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<unsigned long long, long long>::value));
@@ -7206,28 +7206,28 @@ void tst_QObject::checkArgumentsForNarrowing()
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByLongLong, unsigned long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, signed char>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, short>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, int>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, long>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, long long>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, short>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, int>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, long>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUChar, long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUShort, signed char>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUShort, short>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUShort, int>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUShort, long>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUShort, long long>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUShort, int>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUShort, long>::value));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUShort, long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUInt, signed char>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUInt, short>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUInt, int>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUInt, long>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUInt, long long>::value));
+    QCOMPARE((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUInt, long>::value), sizeof(ScopedEnumBackedByUInt) >= sizeof(long));
+    QVERIFY((!QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByUInt, long long>::value));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByULong, signed char>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByULong, short>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByULong, int>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByULong, long>::value));
-    QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByULong, long long>::value));
+    QCOMPARE((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByULong, long long>::value), sizeof(ScopedEnumBackedByULong) >= sizeof(long long));
 
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByULongLong, signed char>::value));
     QVERIFY((QtPrivate::AreArgumentsNarrowedBase<ScopedEnumBackedByULongLong, short>::value));