diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2022-05-19 15:41:24 +0200 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2022-05-19 22:30:23 +0200 |
commit | 827193b3a196511091ec575905c01adbba55c8d4 (patch) | |
tree | a2a422d2e7f3f5a3572e933b39aa267bbc859c48 | |
parent | 4a10e1d84ac948fb9a50cd2e44fcbeaef972e5ea (diff) |
qqmljsimportvisitor: Fix assert being hit on empty block bindings
We previously hit an assert / crashed if we saw an empty script binding.
Fixes: QTBUG-103707
Change-Id: I117d984a7d315ecf860d2ada5568637d154ae083
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
-rw-r--r-- | src/qmlcompiler/qqmljsimportvisitor.cpp | 23 | ||||
-rw-r--r-- | tests/auto/qml/qqmljsscope/data/emptyBlockBinding.qml | 6 | ||||
-rw-r--r-- | tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp | 13 |
3 files changed, 25 insertions, 17 deletions
diff --git a/src/qmlcompiler/qqmljsimportvisitor.cpp b/src/qmlcompiler/qqmljsimportvisitor.cpp index a2df42e045..612cc66b15 100644 --- a/src/qmlcompiler/qqmljsimportvisitor.cpp +++ b/src/qmlcompiler/qqmljsimportvisitor.cpp @@ -1679,24 +1679,17 @@ QQmlJSImportVisitor::LiteralOrScriptParseResult QQmlJSImportVisitor::parseLiteralOrScriptBinding(const QString name, const QQmlJS::AST::Statement *statement) { + if (statement == nullptr) + return LiteralOrScriptParseResult::Invalid; + const auto *exprStatement = cast<const ExpressionStatement *>(statement); if (exprStatement == nullptr) { - if (const auto *blockStatement = cast<const Block *>(statement)) { - // this is a special case of script binding because: - // 1. we are trying to parse a binding (this function's logic) - // 2. we encounter a block statement - Q_ASSERT(blockStatement->statements); - auto first = blockStatement->statements->statement; - if (first == nullptr) - return LiteralOrScriptParseResult::Invalid; - QQmlJSMetaPropertyBinding binding(first->firstSourceLocation(), name); - binding.setScriptBinding(addFunctionOrExpression(m_currentScope, name), - QQmlJSMetaPropertyBinding::Script_PropertyBinding); - m_currentScope->addOwnPropertyBinding(binding); - return LiteralOrScriptParseResult::Script; - } - return LiteralOrScriptParseResult::Invalid; + QQmlJSMetaPropertyBinding binding(statement->firstSourceLocation(), name); + binding.setScriptBinding(addFunctionOrExpression(m_currentScope, name), + QQmlJSMetaPropertyBinding::Script_PropertyBinding); + m_currentScope->addOwnPropertyBinding(binding); + return LiteralOrScriptParseResult::Script; } auto expr = exprStatement->expression; diff --git a/tests/auto/qml/qqmljsscope/data/emptyBlockBinding.qml b/tests/auto/qml/qqmljsscope/data/emptyBlockBinding.qml new file mode 100644 index 0000000000..f4c1372db4 --- /dev/null +++ b/tests/auto/qml/qqmljsscope/data/emptyBlockBinding.qml @@ -0,0 +1,6 @@ +import QtQuick + +Item { + x: {} + y: {5} +} diff --git a/tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp b/tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp index ebf9bd9beb..cb1a03ab2b 100644 --- a/tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp +++ b/tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp @@ -129,6 +129,7 @@ private Q_SLOTS: void attachedProperties(); void scriptIndices(); void extensions(); + void emptyBlockBinding(); public: tst_qqmljsscope() @@ -177,14 +178,14 @@ void tst_qqmljsscope::orderedBindings() QVERIFY(root); auto [pBindingsBegin, pBindingsEnd] = root->ownPropertyBindings(u"p"_s); - QVERIFY(std::distance(pBindingsBegin, pBindingsEnd) == 2); + QCOMPARE(std::distance(pBindingsBegin, pBindingsEnd), 2); // check that the bindings are properly ordered QCOMPARE(pBindingsBegin->bindingType(), QQmlJSMetaPropertyBinding::Object); QCOMPARE(std::next(pBindingsBegin)->bindingType(), QQmlJSMetaPropertyBinding::Interceptor); auto [itemsBindingsBegin, itemsBindingsEnd] = root->ownPropertyBindings(u"items"_s); - QVERIFY(std::distance(itemsBindingsBegin, itemsBindingsEnd) == 2); + QCOMPARE(std::distance(itemsBindingsBegin, itemsBindingsEnd), 2); QCOMPARE(itemsBindingsBegin->bindingType(), QQmlJSMetaPropertyBinding::Object); QCOMPARE(std::next(itemsBindingsBegin)->bindingType(), QQmlJSMetaPropertyBinding::Object); @@ -631,5 +632,13 @@ void tst_qqmljsscope::extensions() QCOMPARE(owner, childScopes[4]->baseType()->extensionType().scope); } +void tst_qqmljsscope::emptyBlockBinding() +{ + QQmlJSScope::ConstPtr root = run(u"emptyBlockBinding.qml"_s); + QVERIFY(root); + QVERIFY(root->hasOwnPropertyBindings(u"x"_s)); + QVERIFY(root->hasOwnPropertyBindings(u"y"_s)); +} + QTEST_MAIN(tst_qqmljsscope) #include "tst_qqmljsscope.moc" |