From d2507f238370f6432cd8d65aa7f335dd68484e5a Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 4 Oct 2021 16:44:46 +0200 Subject: [PATCH] qmllint: Allow assigning to readonly properties in same scope If the property lives in the same scope where it's assigned we can assign it even if it's readonly. That's the whole point of it being readonly. Pick-to: 6.2 Change-Id: I172d5b997641490201a0216fd7ebb5a3de30a63b Reviewed-by: Fabian Kosmale --- src/qmlcompiler/qqmljstypepropagator.cpp | 2 +- src/qmlcompiler/qqmljstyperesolver.cpp | 8 +++++--- tests/auto/qml/qmllint/data/initReadonly.qml | 5 +++++ tests/auto/qml/qmllint/tst_qmllint.cpp | 1 + 4 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 tests/auto/qml/qmllint/data/initReadonly.qml diff --git a/src/qmlcompiler/qqmljstypepropagator.cpp b/src/qmlcompiler/qqmljstypepropagator.cpp index d957383bf4..587dac3440 100644 --- a/src/qmlcompiler/qqmljstypepropagator.cpp +++ b/src/qmlcompiler/qqmljstypepropagator.cpp @@ -532,7 +532,7 @@ void QQmlJSTypePropagator::generate_StoreNameSloppy(int nameIndex) return; } - if (!type.isWritable()) { + if (!type.isWritable() && !m_currentScope->hasOwnProperty(name)) { setError(u"Can't assign to read-only property %1"_qs.arg(name)); m_logger->logWarning(u"Cannot assign to read-only property %1"_qs.arg(name), Log_Property, diff --git a/src/qmlcompiler/qqmljstyperesolver.cpp b/src/qmlcompiler/qqmljstyperesolver.cpp index 3efd09ca41..ebd700d2a6 100644 --- a/src/qmlcompiler/qqmljstyperesolver.cpp +++ b/src/qmlcompiler/qqmljstyperesolver.cpp @@ -135,9 +135,11 @@ void QQmlJSTypeResolver::init(QQmlJSImportVisitor &visitor) if (!binding.isLiteralBinding()) continue; - if (QQmlJSMetaProperty property = scope->property(binding.propertyName()); - property.isValid()) { - if (!property.isWritable()) { + const QQmlJSMetaProperty property = scope->property(binding.propertyName()); + if (property.isValid()) { + // If the property is defined in the same scope where it is set, + // we are in fact allowed to set it, even if it's not writable. + if (!property.isWritable() && !scope->hasOwnProperty(binding.propertyName())) { m_logger->logWarning(u"Cannot assign to read-only property %1"_qs .arg(binding.propertyName()), Log_Type, binding.sourceLocation()); diff --git a/tests/auto/qml/qmllint/data/initReadonly.qml b/tests/auto/qml/qmllint/data/initReadonly.qml new file mode 100644 index 0000000000..5a3cafff23 --- /dev/null +++ b/tests/auto/qml/qmllint/data/initReadonly.qml @@ -0,0 +1,5 @@ +import QtQml + +QtObject { + readonly property int i: 14 +} diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp index da8ee83acf..15388109a3 100644 --- a/tests/auto/qml/qmllint/tst_qmllint.cpp +++ b/tests/auto/qml/qmllint/tst_qmllint.cpp @@ -853,6 +853,7 @@ void TestQmllint::cleanQmlCode_data() QTest::newRow("GoodModulePrefix") << QStringLiteral("goodModulePrefix.qml"); QTest::newRow("required property in Component") << QStringLiteral("requiredPropertyInComponent.qml"); QTest::newRow("bytearray") << QStringLiteral("bytearray.qml"); + QTest::newRow("initReadonly") << QStringLiteral("initReadonly.qml"); } void TestQmllint::cleanQmlCode()