Blame SOURCES/0022-Fix-IC-properties-in-same-file.patch

383017
From bb0ce1ffd48aa69da03dc43bd314351519ebf0d7 Mon Sep 17 00:00:00 2001
383017
From: Fabian Kosmale <fabian.kosmale@qt.io>
383017
Date: Tue, 8 Dec 2020 14:12:47 +0100
383017
Subject: [PATCH 22/28] Fix IC properties in same file
383017
383017
Also fixes typename and metatype registration for inline components.
383017
383017
Done-with: Fabian Kosmale <fabian.kosmale@qt.io>
383017
Fixes: QTBUG-89173
383017
Change-Id: I97d65d5539b577a8828d5711e5f2e79c8568b441
383017
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
383017
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
383017
(cherry picked from commit c2ca14ce22551ea72544b6e2b3a19823b6dc3050)
383017
---
383017
 src/qml/qml/qqmlpropertycachecreator.cpp      |  9 +++++++
383017
 src/qml/qml/qqmlpropertycachecreator_p.h      |  2 ++
383017
 src/qml/qml/qqmlpropertyvalidator.cpp         | 25 +++++++++++++++++++
383017
 src/qml/qml/qqmltypedata.cpp                  |  4 +--
383017
 .../data/inlineComponentsSameFile.qml         | 11 ++++++++
383017
 .../qml/qqmllanguage/tst_qqmllanguage.cpp     |  1 +
383017
 6 files changed, 49 insertions(+), 3 deletions(-)
383017
 create mode 100644 tests/auto/qml/qqmllanguage/data/inlineComponentsSameFile.qml
383017
383017
diff --git a/src/qml/qml/qqmlpropertycachecreator.cpp b/src/qml/qml/qqmlpropertycachecreator.cpp
383017
index 36581bda4e..88d80d88ab 100644
383017
--- a/src/qml/qml/qqmlpropertycachecreator.cpp
383017
+++ b/src/qml/qml/qqmlpropertycachecreator.cpp
383017
@@ -90,6 +90,15 @@ QByteArray QQmlPropertyCacheCreatorBase::createClassNameTypeByUrl(const QUrl &ur
383017
             QByteArray::number(classIndexCounter.fetchAndAddRelaxed(1));
383017
 }
383017
 
383017
+QByteArray QQmlPropertyCacheCreatorBase::createClassNameForInlineComponent(const QUrl &baseUrl, int icId)
383017
+{
383017
+    QByteArray baseName = createClassNameTypeByUrl(baseUrl);
383017
+    if (baseName.isEmpty())
383017
+        baseName = QByteArray("ANON_QML_IC_") + QByteArray::number(classIndexCounter.fetchAndAddRelaxed(1));
383017
+    baseName += "_" + QByteArray::number(icId);
383017
+    return baseName;
383017
+}
383017
+
383017
 QQmlBindingInstantiationContext::QQmlBindingInstantiationContext(int referencingObjectIndex, const QV4::CompiledData::Binding *instantiatingBinding,
383017
                                                                  const QString &instantiatingPropertyName, QQmlPropertyCache *referencingObjectPropertyCache)
383017
     : referencingObjectIndex(referencingObjectIndex)
383017
diff --git a/src/qml/qml/qqmlpropertycachecreator_p.h b/src/qml/qml/qqmlpropertycachecreator_p.h
383017
index 6b02d6fb98..77e3763a49 100644
383017
--- a/src/qml/qml/qqmlpropertycachecreator_p.h
383017
+++ b/src/qml/qml/qqmlpropertycachecreator_p.h
383017
@@ -104,6 +104,8 @@ public:
383017
     static int metaTypeForPropertyType(QV4::CompiledData::BuiltinType type);
383017
 
383017
     static QByteArray createClassNameTypeByUrl(const QUrl &url;;
383017
+
383017
+    static QByteArray createClassNameForInlineComponent(const QUrl &baseUrl, int icId);
383017
 };
383017
 
383017
 template <typename ObjectContainer>
383017
diff --git a/src/qml/qml/qqmlpropertyvalidator.cpp b/src/qml/qml/qqmlpropertyvalidator.cpp
383017
index 3587609301..3a1f33113f 100644
383017
--- a/src/qml/qml/qqmlpropertyvalidator.cpp
383017
+++ b/src/qml/qml/qqmlpropertyvalidator.cpp
383017
@@ -651,6 +651,19 @@ bool QQmlPropertyValidator::canCoerce(int to, QQmlPropertyCache *fromMo) const
383017
 {
383017
     QQmlPropertyCache *toMo = enginePrivate->rawPropertyCacheForType(to);
383017
 
383017
+    if (toMo == nullptr) {
383017
+        // if we have an inline component from the current file,
383017
+        // it is not properly registered at this point, as registration
383017
+        // only occurs after the whole file has been validated
383017
+        // Therefore we need to check the ICs here
383017
+        for (const auto& icDatum : compilationUnit->inlineComponentData) {
383017
+            if (icDatum.typeIds.id == to) {
383017
+                toMo = compilationUnit->propertyCaches.at(icDatum.objectIndex);
383017
+                break;
383017
+            }
383017
+        }
383017
+    }
383017
+
383017
     while (fromMo) {
383017
         if (fromMo == toMo)
383017
             return true;
383017
@@ -746,6 +759,18 @@ QQmlError QQmlPropertyValidator::validateObjectBinding(QQmlPropertyData *propert
383017
         // effect the properties on the type, but don't effect assignability
383017
         // Using -1 for the minor version ensures that we get the raw metaObject.
383017
         QQmlPropertyCache *propertyMetaObject = enginePrivate->rawPropertyCacheForType(propType, -1);
383017
+        if (!propertyMetaObject) {
383017
+            // if we have an inline component from the current file,
383017
+            // it is not properly registered at this point, as registration
383017
+            // only occurs after the whole file has been validated
383017
+            // Therefore we need to check the ICs here
383017
+            for (const auto& icDatum: compilationUnit->inlineComponentData) {
383017
+                if (icDatum.typeIds.id == property->propType()) {
383017
+                    propertyMetaObject = compilationUnit->propertyCaches.at(icDatum.objectIndex);
383017
+                    break;
383017
+                }
383017
+            }
383017
+        }
383017
 
383017
         if (propertyMetaObject) {
383017
             // Will be true if the assigned type inherits propertyMetaObject
383017
diff --git a/src/qml/qml/qqmltypedata.cpp b/src/qml/qml/qqmltypedata.cpp
383017
index fc1d0cfbcf..92a90ea677 100644
383017
--- a/src/qml/qml/qqmltypedata.cpp
383017
+++ b/src/qml/qml/qqmltypedata.cpp
383017
@@ -283,9 +283,7 @@ void setupICs(const ObjectContainer &container, QHash<int, InlineComponentData>
383017
     for (int i = 0; i != container->objectCount(); ++i) {
383017
         auto root = container->objectAt(i);
383017
         for (auto it = root->inlineComponentsBegin(); it != root->inlineComponentsEnd(); ++it) {
383017
-            auto url = finalUrl;
383017
-            url.setFragment(QString::number(it->objectIndex));
383017
-            const QByteArray &className = QQmlPropertyCacheCreatorBase::createClassNameTypeByUrl(url);
383017
+            const QByteArray &className = QQmlPropertyCacheCreatorBase::createClassNameForInlineComponent(finalUrl, it->objectIndex);
383017
             InlineComponentData icDatum(QQmlMetaType::registerInternalCompositeType(className), int(it->objectIndex), int(it->nameIndex), 0, 0, 0);
383017
             icData->insert(it->objectIndex, icDatum);
383017
         }
383017
diff --git a/tests/auto/qml/qqmllanguage/data/inlineComponentsSameFile.qml b/tests/auto/qml/qqmllanguage/data/inlineComponentsSameFile.qml
383017
new file mode 100644
383017
index 0000000000..87cac10200
383017
--- /dev/null
383017
+++ b/tests/auto/qml/qqmllanguage/data/inlineComponentsSameFile.qml
383017
@@ -0,0 +1,11 @@
383017
+import QtQml 2.15
383017
+
383017
+QtObject {
383017
+    component IC : QtObject {
383017
+        property string name
383017
+        property int age
383017
+    }
383017
+
383017
+    property IC other: IC { name: "Toby"; age: 30 }
383017
+    property list<IC> listProp: [IC { name: "Alfred Ill"; age: 65 }, IC { name: "Claire Zachanassian"; age: 62}]
383017
+}
383017
diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
383017
index 8adcbc1837..e247a139ec 100644
383017
--- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
383017
+++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp
383017
@@ -5604,6 +5604,7 @@ void tst_qqmllanguage::inlineComponent_data()
383017
     QTest::newRow("Alias resolves correctly") << testFileUrl("inlineComponentWithAlias.qml") << QColorConstants::Svg::lime << 42 << true;
383017
 
383017
     QTest::newRow("Two inline components in same do not crash (QTBUG-86989)") << testFileUrl("twoInlineComponents.qml") << QColor() << 0 << false;
383017
+    QTest::newRow("Inline components used in same file (QTBUG-89173)") << testFileUrl("inlineComponentsSameFile.qml") << QColor() << 0 << false;
383017
 }
383017
 
383017
 void tst_qqmllanguage::inlineComponentReferenceCycle_data()
383017
-- 
383017
2.31.1
383017