From 029af22ef51ec23871d0700bc2ef93477e06e149 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 11 Dec 2024 10:59:59 +0100 Subject: [PATCH] QStringView: refuse construction from QStringRef When QStringRef was moved out of QtCore, it was also incorrectly removed from the if_compatible_container constraint, causing QStringView{sr} to still match the general container QStringView ctor overload, which doesn't preserve null'ness if data() doesn't return null. By refusing to provide a constructor from QStringRef, we force the compiler to use QStringRef's implicit conversion operators instead. This transitively affects QAnyStringView in the same way. The tests can, naturally, only be in qt5compat, so define a macro to communicate to tst_QStringRef whether it compiles against a fixed QtCore or not. Fixes: QTBUG-122797 Fixes: QTBUG-122798 Pick-to: 6.5 Change-Id: I64b75a8e421d2b6185615e3288ce3ad7fd8f15f9 Reviewed-by: Ahmad Samir (cherry picked from commit a0e65398483729259cf58781949133c6055fdc7c) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 9e86f73f8264c2b5f3c8ec2b1144710ae70bfef4) --- src/corelib/text/qstringfwd.h | 1 + src/corelib/text/qstringview.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/corelib/text/qstringfwd.h b/src/corelib/text/qstringfwd.h index 374a8ff3cbe..aefac03b416 100644 --- a/src/corelib/text/qstringfwd.h +++ b/src/corelib/text/qstringfwd.h @@ -36,6 +36,7 @@ class QLatin1String; using QLatin1StringView = QLatin1String; #endif class QString; +class QStringRef; // defined in qt5compat class QStringView; template class QBasicUtf8StringView; class QAnyStringView; diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index b050a9aef7c..454dd1fdad9 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -64,6 +64,8 @@ struct IsContainerCompatibleWithQStringView, QString>>, +#define QSTRINGVIEW_REFUSES_QSTRINGREF 1 + std::negation, QStringRef>>, // QStringRef::op QStringView() // Don't make an accidental copy constructor std::negation, QStringView>>