From 790d257ef01e0f95b776db873ad34bebe16396a3 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Thu, 16 Dec 2021 10:10:25 +0100 Subject: [PATCH] qmltc: Do not crash on import namespaces Pick-to: 6.3 Change-Id: I9767857076ea6ae565c7efd75d64cb47a82b7be7 Reviewed-by: Ulf Hermann --- tests/auto/qml/qmltc/CMakeLists.txt | 1 + tests/auto/qml/qmltc/data/importNamespace.qml | 4 ++++ tests/auto/qml/qmltc/tst_qmltc.cpp | 9 +++++++++ tests/auto/qml/qmltc/tst_qmltc.h | 1 + tools/qmltc/prototype/typeresolver.h | 8 ++++++++ 5 files changed, 23 insertions(+) create mode 100644 tests/auto/qml/qmltc/data/importNamespace.qml diff --git a/tests/auto/qml/qmltc/CMakeLists.txt b/tests/auto/qml/qmltc/CMakeLists.txt index 92573f3fc6..90731f2e02 100644 --- a/tests/auto/qml/qmltc/CMakeLists.txt +++ b/tests/auto/qml/qmltc/CMakeLists.txt @@ -21,6 +21,7 @@ set(qml_sources data/properties.qml data/ObjectWithId.qml data/documentWithIds.qml + data/importNamespace.qml data/signalHandlers.qml data/javaScriptFunctions.qml diff --git a/tests/auto/qml/qmltc/data/importNamespace.qml b/tests/auto/qml/qmltc/data/importNamespace.qml new file mode 100644 index 0000000000..2786356843 --- /dev/null +++ b/tests/auto/qml/qmltc/data/importNamespace.qml @@ -0,0 +1,4 @@ +import QtQuick as QQ +QQ.Text { + text: "hello, world" +} diff --git a/tests/auto/qml/qmltc/tst_qmltc.cpp b/tests/auto/qml/qmltc/tst_qmltc.cpp index 56ca309491..d16b1157c1 100644 --- a/tests/auto/qml/qmltc/tst_qmltc.cpp +++ b/tests/auto/qml/qmltc/tst_qmltc.cpp @@ -37,6 +37,7 @@ #include "properties.h" #include "objectwithid.h" #include "documentwithids.h" +#include "importnamespace.h" #include "signalhandlers.h" #include "javascriptfunctions.h" @@ -133,6 +134,7 @@ void tst_qmltc::initTestCase() QUrl("qrc:/QmltcTests/data/properties.qml"), QUrl("qrc:/QmltcTests/data/ObjectWithId.qml"), QUrl("qrc:/QmltcTests/data/documentWithIds.qml"), + QUrl("qrc:/QmltcTests/data/importNamespace.qml"), QUrl("qrc:/QmltcTests/data/signalHandlers.qml"), QUrl("qrc:/QmltcTests/data/javaScriptFunctions.qml"), @@ -529,6 +531,13 @@ void tst_qmltc::ids() } } +void tst_qmltc::importNamespace() +{ + QQmlEngine e; + PREPEND_NAMESPACE(importNamespace) created(&e); // compilation of this type shouldn't crash + QCOMPARE(created.text(), u"hello, world"_qs); +} + void tst_qmltc::signalHandlers() { QQmlEngine e; diff --git a/tests/auto/qml/qmltc/tst_qmltc.h b/tests/auto/qml/qmltc/tst_qmltc.h index 9e0d732d4d..d6c4d3aec3 100644 --- a/tests/auto/qml/qmltc/tst_qmltc.h +++ b/tests/auto/qml/qmltc/tst_qmltc.h @@ -51,6 +51,7 @@ private slots: void methods(); void properties(); void ids(); + void importNamespace(); void signalHandlers(); void jsFunctions(); diff --git a/tools/qmltc/prototype/typeresolver.h b/tools/qmltc/prototype/typeresolver.h index 6dd98bb470..dc747fd042 100644 --- a/tools/qmltc/prototype/typeresolver.h +++ b/tools/qmltc/prototype/typeresolver.h @@ -49,10 +49,18 @@ public: QHash builtins = m_importer->builtinInternalNames(); cppNames.reserve(builtins.size() + m_imports.size()); const auto getInternalName = [](const QQmlJSScope::ConstPtr &t) { + if (!t) + return QString(); return t->internalName(); }; std::transform(builtins.cbegin(), builtins.cend(), std::back_inserter(cppNames), getInternalName); + + // builtins must be valid: all QQmlJSScopes are not nullptr and have + // non-empty internal names. m_imports may have nullptrs, due to import + // namespaces + Q_ASSERT(std::find(cppNames.cbegin(), cppNames.cend(), QString()) == cppNames.cend()); + std::transform(m_imports.cbegin(), m_imports.cend(), std::back_inserter(cppNames), getInternalName); return cppNames;