diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-06-24 10:50:51 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-06-28 11:37:51 +0200 |
commit | e9c2ccd8be271c79996065d045642c586817b4d6 (patch) | |
tree | 539e562416650293fce59240c828a284f0eed8d8 | |
parent | a87e2eb0f5a1da8d47d4765e13b16205cdbc0730 (diff) |
QmlCompiler: Add missing parentheses to in-place arithmetic operations
Fixes: QTBUG-104512
Change-Id: I3d592eeda5cefd9e9805b1811b37bebec5d6fc9c
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 7230508c3bf7d42fc2d262d29f9302ac46c3a4f0)
-rw-r--r-- | src/qmlcompiler/qqmljscodegenerator.cpp | 2 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/conversionDecrement.qml | 18 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp | 22 |
4 files changed, 42 insertions, 1 deletions
diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp index 24fbdfe140..d4402a0599 100644 --- a/src/qmlcompiler/qqmljscodegenerator.cpp +++ b/src/qmlcompiler/qqmljscodegenerator.cpp @@ -2324,7 +2324,7 @@ void QQmlJSCodeGenerator::generateInPlaceOperation(const QString &cppOperator) m_body += u"auto converted = "_s + var + u";\n"_s; m_body += m_state.accumulatorVariableOut + u" = "_s + conversion( m_typeResolver->original(m_state.accumulatorOut()), - m_state.accumulatorOut(), cppOperator + u"converted"_s) + u";\n"_s; + m_state.accumulatorOut(), u'(' + cppOperator + u"converted)"_s) + u";\n"_s; m_body += u"}\n"_s; generateOutputVariantConversion(m_typeResolver->containedType(original)); } diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt index ae2a172654..2228f4ad22 100644 --- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt +++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt @@ -55,6 +55,7 @@ set(qml_files compositesingleton.qml construct.qml contextParam.qml + conversionDecrement.qml conversions.qml conversions2.qml curlygrouped.qml diff --git a/tests/auto/qml/qmlcppcodegen/data/conversionDecrement.qml b/tests/auto/qml/qmlcppcodegen/data/conversionDecrement.qml new file mode 100644 index 0000000000..fdce0fe65c --- /dev/null +++ b/tests/auto/qml/qmlcppcodegen/data/conversionDecrement.qml @@ -0,0 +1,18 @@ +pragma Strict +import QtQml + +QtObject { + id: panelGrid + property var pages: 4 + property int currentPageIndex: 0 + + onPagesChanged: { + if (panelGrid.currentPageIndex === 0) { + panelGrid.currentPageIndex = panelGrid.pages - 2 + } else if (panelGrid.currentPageIndex === panelGrid.pages - 1) { + panelGrid.currentPageIndex = 0 + } else { + panelGrid.currentPageIndex -= 1 + } + } +} diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp index 40b0fb78c2..81179be36d 100644 --- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp +++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp @@ -123,6 +123,7 @@ private slots: void objectToString(); void throwObjectName(); void javaScriptArgument(); + void conversionDecrement(); }; void tst_QmlCppCodegen::simpleBinding() @@ -2242,6 +2243,27 @@ void tst_QmlCppCodegen::javaScriptArgument() QCOMPARE(o->property("d").toString(), u"9"_qs); } +void tst_QmlCppCodegen::conversionDecrement() +{ + QQmlEngine engine; + QQmlComponent c(&engine, QUrl(u"qrc:/TestTypes/conversionDecrement.qml"_s)); + + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + + QScopedPointer<QObject> o(c.create()); + QVERIFY(!o.isNull()); + + QCOMPARE(o->property("currentPageIndex").toInt(), 0); + o->setProperty("pages", 5); + QCOMPARE(o->property("currentPageIndex").toInt(), 3); + o->setProperty("pages", 4); + QCOMPARE(o->property("currentPageIndex").toInt(), 0); + o->setProperty("pages", 6); + QCOMPARE(o->property("currentPageIndex").toInt(), 4); + o->setProperty("pages", 60); + QCOMPARE(o->property("currentPageIndex").toInt(), 3); +} + void tst_QmlCppCodegen::runInterpreted() { #ifdef Q_OS_ANDROID |