aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-07-29 16:19:21 +0200
committerUlf Hermann <ulf.hermann@qt.io>2022-08-04 14:24:47 +0200
commitd1a6e6884bfe8db7070a5ba21d59aa1b86eeecb6 (patch)
tree9938f04bc940dcd69109ff0283f2b9c274e488a5
parent25a0ca5e1163fe8de0efdc78f1a9f2a4724f41c9 (diff)
QmlCompiler: Allow any conversion possible via QJSPrimitiveValue
All of those are legal in ECMAScript, and so we need to support them in script bindings. As we have stricter rules for literal bindings, add an extra method there to check for what QQmlPropertyValidator does. Fixes: QTBUG-105252 Task-number: QTBUG-105188 Change-Id: I0621b2c3aa196414f669873e93670557284a8bca Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit b184d026484572915c3ff34224dfc371a4e6a3cc)
-rw-r--r--src/qmlcompiler/qqmljsliteralbindingcheck.cpp39
-rw-r--r--src/qmlcompiler/qqmljstyperesolver.cpp4
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt1
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/notNotString.qml7
-rw-r--r--tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp13
-rw-r--r--tests/auto/qml/qmllint/data/templateStringSubstitution.qml (renamed from tests/auto/qml/qmllint/data/badTemplateStringSubstitution.qml)0
-rw-r--r--tests/auto/qml/qmllint/tst_qmllint.cpp38
7 files changed, 69 insertions, 33 deletions
diff --git a/src/qmlcompiler/qqmljsliteralbindingcheck.cpp b/src/qmlcompiler/qqmljsliteralbindingcheck.cpp
index d56333414f..17930ee92a 100644
--- a/src/qmlcompiler/qqmljsliteralbindingcheck.cpp
+++ b/src/qmlcompiler/qqmljsliteralbindingcheck.cpp
@@ -11,6 +11,34 @@ QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
+// This makes no sense, but we want to warn about things QQmlPropertyResolver complains about.
+static bool canConvertForLiteralBinding(
+ QQmlJSTypeResolver *resolver, const QQmlJSScope::ConstPtr &from,
+ const QQmlJSScope::ConstPtr &to) {
+ if (resolver->equals(from, to))
+ return true;
+
+ if (!resolver->canConvertFromTo(from, to))
+ return false;
+
+ const bool fromIsString = resolver->equals(from, resolver->stringType());
+
+ if (resolver->equals(to, resolver->stringType())
+ || resolver->equals(to, resolver->stringListType())
+ || resolver->equals(to, resolver->byteArrayType())
+ || resolver->equals(to, resolver->urlType())) {
+ return fromIsString;
+ }
+
+ if (resolver->isNumeric(to))
+ return resolver->isNumeric(from);
+
+ if (resolver->equals(to, resolver->boolType()))
+ return resolver->equals(from, resolver->boolType());
+
+ return true;
+}
+
void QQmlJSLiteralBindingCheck::run(QQmlJSImportVisitor *visitor, QQmlJSTypeResolver *resolver)
{
QQmlJSLogger *logger = visitor->logger();
@@ -34,19 +62,14 @@ void QQmlJSLiteralBindingCheck::run(QQmlJSImportVisitor *visitor, QQmlJSTypeReso
continue;
}
- if (!resolver->canConvertFromTo(binding.literalType(resolver), property.type())) {
- logger->log(u"Cannot assign binding of type %1 to %2"_s.arg(
+ if (!canConvertForLiteralBinding(
+ resolver, binding.literalType(resolver), property.type())) {
+ logger->log(u"Cannot assign literal of type %1 to %2"_s.arg(
QQmlJSScope::prettyName(binding.literalTypeName()),
QQmlJSScope::prettyName(property.typeName())),
Log_Type, binding.sourceLocation());
continue;
}
-
- if (resolver->equals(property.type(), resolver->stringType())
- && resolver->isNumeric(binding.literalType(resolver))) {
- logger->log(u"Cannot assign a numeric constant to a string property"_s,
- Log_Type, binding.sourceLocation());
- }
}
}
}
diff --git a/src/qmlcompiler/qqmljstyperesolver.cpp b/src/qmlcompiler/qqmljstyperesolver.cpp
index 4fc5e678be..046a79ce01 100644
--- a/src/qmlcompiler/qqmljstyperesolver.cpp
+++ b/src/qmlcompiler/qqmljstyperesolver.cpp
@@ -964,6 +964,10 @@ bool QQmlJSTypeResolver::canPrimitivelyConvertFromTo(
return true;
}
+ // We can convert anything that fits into QJSPrimitiveValue
+ if (canConvertFromTo(from, m_jsPrimitiveType) && canConvertFromTo(m_jsPrimitiveType, to))
+ return true;
+
return false;
}
diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
index d5711293b0..f3e608a6ae 100644
--- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
+++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
@@ -114,6 +114,7 @@ set(qml_files
nonNotifyable.qml
noscope.qml
notEqualsInt.qml
+ notNotString.qml
nullAccess.qml
objectInVar.qml
outOfBounds.qml
diff --git a/tests/auto/qml/qmlcppcodegen/data/notNotString.qml b/tests/auto/qml/qmlcppcodegen/data/notNotString.qml
new file mode 100644
index 0000000000..d46c2cddc9
--- /dev/null
+++ b/tests/auto/qml/qmlcppcodegen/data/notNotString.qml
@@ -0,0 +1,7 @@
+pragma Strict
+import QML
+
+QtObject {
+ id: self
+ property bool notNotString: !!self.objectName
+}
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
index 2d8c1b00c7..186758518e 100644
--- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
+++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
@@ -131,6 +131,7 @@ private slots:
void enumLookup();
void stringToByteArray();
void listPropertyAsModel();
+ void notNotString();
};
void tst_QmlCppCodegen::simpleBinding()
@@ -2402,6 +2403,18 @@ void tst_QmlCppCodegen::listPropertyAsModel()
QCOMPARE(children.count(), 5);
}
+void tst_QmlCppCodegen::notNotString()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, QUrl(u"qrc:/TestTypes/notNotString.qml"_s));
+ QVERIFY2(c.isReady(), qPrintable(c.errorString()));
+ QScopedPointer<QObject> o(c.create());
+
+ QCOMPARE(o->property("notNotString").value<bool>(), false);
+ o->setObjectName(u"a"_s);
+ QCOMPARE(o->property("notNotString").value<bool>(), true);
+}
+
void tst_QmlCppCodegen::runInterpreted()
{
#ifdef Q_OS_ANDROID
diff --git a/tests/auto/qml/qmllint/data/badTemplateStringSubstitution.qml b/tests/auto/qml/qmllint/data/templateStringSubstitution.qml
index 64d6e0158d..64d6e0158d 100644
--- a/tests/auto/qml/qmllint/data/badTemplateStringSubstitution.qml
+++ b/tests/auto/qml/qmllint/data/templateStringSubstitution.qml
diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp
index e57541162f..e0e690ed99 100644
--- a/tests/auto/qml/qmllint/tst_qmllint.cpp
+++ b/tests/auto/qml/qmllint/tst_qmllint.cpp
@@ -562,24 +562,20 @@ void TestQmllint::dirtyQmlCode_data()
QTest::newRow("bad template literal (simple)")
<< QStringLiteral("badTemplateStringSimple.qml")
<< Result { { Message {
- QStringLiteral("Cannot assign binding of type string to int") } } };
- QTest::newRow("bad template literal (substitution)")
- << QStringLiteral("badTemplateStringSubstitution.qml")
- << Result { { Message {
- QStringLiteral("Cannot assign binding of type QString to int") } } };
+ QStringLiteral("Cannot assign literal of type string to int") } } };
QTest::newRow("bad constant number to string")
<< QStringLiteral("numberToStringProperty.qml")
<< Result { { Message { QStringLiteral(
- "Cannot assign a numeric constant to a string property") } } };
+ "Cannot assign literal of type double to QString") } } };
QTest::newRow("bad unary minus to string")
<< QStringLiteral("unaryMinusToStringProperty.qml")
<< Result { { Message { QStringLiteral(
- "Cannot assign a numeric constant to a string property") } } };
+ "Cannot assign literal of type double to QString") } } };
QTest::newRow("bad tranlsation binding (qsTr)") << QStringLiteral("bad_qsTr.qml") << Result {};
QTest::newRow("bad string binding (QT_TR_NOOP)")
<< QStringLiteral("bad_QT_TR_NOOP.qml")
<< Result { { Message {
- QStringLiteral("Cannot assign binding of type string to int") } } };
+ QStringLiteral("Cannot assign literal of type string to int") } } };
QTest::newRow("BadScriptBindingOnGroup")
<< QStringLiteral("badScriptBinding.group.qml")
<< Result { { Message {
@@ -679,7 +675,7 @@ void TestQmllint::dirtyQmlCode_data()
QTest::newRow("badAttachedPropertyTypeString")
<< QStringLiteral("badAttachedPropertyTypeString.qml")
<< Result { { Message {
- QStringLiteral("Cannot assign binding of type string to int") } } };
+ QStringLiteral("Cannot assign literal of type string to int") } } };
QTest::newRow("badAttachedPropertyTypeQtObject")
<< QStringLiteral("badAttachedPropertyTypeQtObject.qml")
<< Result { { Message { QStringLiteral(
@@ -789,18 +785,10 @@ expression: \${expr} \${expr} \\\${expr} \\\${expr}`)",
QTest::newRow("WithStatement") << QStringLiteral("WithStatement.qml")
<< Result { { Message { QStringLiteral(
"with statements are strongly discouraged") } } };
- QTest::newRow("BindingTypeMismatch")
- << QStringLiteral("bindingTypeMismatch.qml")
- << Result { { Message {
- QStringLiteral("Cannot assign binding of type QString to int") } } };
- QTest::newRow("BindingTypeMismatchFunction")
- << QStringLiteral("bindingTypeMismatchFunction.qml")
- << Result { { Message {
- QStringLiteral("Cannot assign binding of type QString to int") } } };
QTest::newRow("BadLiteralBinding")
<< QStringLiteral("badLiteralBinding.qml")
<< Result { { Message {
- QStringLiteral("Cannot assign binding of type string to int") } } };
+ QStringLiteral("Cannot assign literal of type string to int") } } };
QTest::newRow("BadLiteralBindingDate")
<< QStringLiteral("badLiteralBindingDate.qml")
<< Result { { Message {
@@ -939,11 +927,7 @@ expression: \${expr} \${expr} \\\${expr} \\\${expr}`)",
{ Message { QStringLiteral("Ready") } } } };
QTest::newRow("nullBinding") << QStringLiteral("nullBinding.qml")
<< Result{ { Message{ QStringLiteral(
- "Cannot assign binding of type null to double") } } };
- QTest::newRow("nullBindingFunction")
- << QStringLiteral("nullBindingFunction.qml")
- << Result{ { Message{
- QStringLiteral("Cannot assign binding of type null to double") } } };
+ "Cannot assign literal of type null to double") } } };
QTest::newRow("missingRequiredAlias")
<< QStringLiteral("missingRequiredAlias.qml")
<< Result { { Message {
@@ -1157,6 +1141,10 @@ void TestQmllint::cleanQmlCode_data()
QTest::newRow("prefixedAttachedProperty") << QStringLiteral("prefixedAttachedProperty.qml");
QTest::newRow("stringToByteArray") << QStringLiteral("stringToByteArray.qml");
QTest::newRow("jsLibrary") << QStringLiteral("jsLibrary.qml");
+ QTest::newRow("nullBindingFunction") << QStringLiteral("nullBindingFunction.qml");
+ QTest::newRow("BindingTypeMismatchFunction") << QStringLiteral("bindingTypeMismatchFunction.qml");
+ QTest::newRow("BindingTypeMismatch") << QStringLiteral("bindingTypeMismatch.qml");
+ QTest::newRow("template literal (substitution)") << QStringLiteral("templateStringSubstitution.qml");
}
void TestQmllint::cleanQmlCode()
@@ -1691,9 +1679,9 @@ void TestQmllint::quickPlugin()
u"Cannot specify top, bottom, and verticalCenter anchors at the same time."_s },
Message{
u"Baseline anchor cannot be used in conjunction with top, bottom, or verticalCenter anchors."_s },
- Message { u"Cannot assign binding of type null to QQuickAnchorLine"_s, 5,
+ Message { u"Cannot assign literal of type null to QQuickAnchorLine"_s, 5,
35 },
- Message { u"Cannot assign binding of type null to QQuickAnchorLine"_s, 6,
+ Message { u"Cannot assign literal of type null to QQuickAnchorLine"_s, 6,
33 } } });
runTest("pluginQuick_anchorsUndefined.qml", Result::clean());
runTest("pluginQuick_layoutChildren.qml",