aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier De Cannière <olivier.decanniere@qt.io>2025-04-07 09:35:09 +0200
committerOlivier De Cannière <olivier.decanniere@qt.io>2025-04-10 13:35:00 +0200
commit5ff76ab872de6bae9766d52f4f794256ae9eeebf (patch)
treee10a18e28262b3d19d7f2955ea7a42579d094eeb
parent8ff4d90918bfc9cc866ccf9d25182b266a2027e0 (diff)
Compiler: Use regular lookup for redundant optional lookups
If we can determine that the base of an optional lookup cannot hold null or undefined, we can simply omit the optional part and generate a regular get lookup. This is also true whenever we lookup an enum. Amends fc4ee77116624c784d8c42f2b8e5dbf2f78b6d89 Fixes: QTBUG-135649 Pick-to: 6.9 6.8 Change-Id: I64984fdecc75cd4dbc2274a08aa73b5274fb09b7 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r--src/qmlcompiler/qqmljscodegenerator.cpp2
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt1
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/GetOptionalLookupNonVoidableBase.qml6
-rw-r--r--tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp11
4 files changed, 20 insertions, 0 deletions
diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp
index 90e115ca29..a3d666f83b 100644
--- a/src/qmlcompiler/qqmljscodegenerator.cpp
+++ b/src/qmlcompiler/qqmljscodegenerator.cpp
@@ -1547,6 +1547,8 @@ void QQmlJSCodeGenerator::generate_GetOptionalLookup(int index, int offset)
} else if (accumulatorIn.isStoredIn(m_typeResolver->jsValueType())) {
m_body += u"if (%1.isNull() || %1.isUndefined())\n"_s.arg(accumulatorVarIn);
generateJumpCodeWithTypeConversions(offset);
+ } else if (!m_typeResolver->canHoldUndefined(accumulatorIn.storage())) {
+ // The base cannot hold undefined and isn't a reference type, generate a regular get lookup
} else {
Q_UNREACHABLE(); // No other accumulatorIn stored types should be possible
}
diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
index 53435306fa..89386965fc 100644
--- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
+++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
@@ -61,6 +61,7 @@ set(qml_files
Enums.qml
FinalProperty.qml
Foozle.qml
+ GetOptionalLookupNonVoidableBase.qml
GetOptionalLookupOnQJSValueNonStrict.qml
GetOptionalLookupShadowed.qml
Loopy.qml
diff --git a/tests/auto/qml/qmlcppcodegen/data/GetOptionalLookupNonVoidableBase.qml b/tests/auto/qml/qmlcppcodegen/data/GetOptionalLookupNonVoidableBase.qml
new file mode 100644
index 0000000000..7b95666965
--- /dev/null
+++ b/tests/auto/qml/qmlcppcodegen/data/GetOptionalLookupNonVoidableBase.qml
@@ -0,0 +1,6 @@
+import QtQml
+
+QtObject {
+ property url foo
+ property bool b: foo?.toString()
+}
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
index 9dd3016809..e2c353fb60 100644
--- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
+++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
@@ -131,6 +131,7 @@ private slots:
void getLookupOfScript();
void getOptionalLookup();
void getOptionalLookup_data();
+ void getOptionalLookupNonVoidableBase();
void getOptionalLookupOnQJSValueNonStrict();
void getOptionalLookupShadowed();
void globals();
@@ -2390,6 +2391,16 @@ void tst_QmlCppCodegen::getOptionalLookup()
QCOMPARE(actual, expected);
}
+void tst_QmlCppCodegen::getOptionalLookupNonVoidableBase()
+{
+ QQmlEngine engine;
+ const QUrl document(u"qrc:/qt/qml/TestTypes/GetOptionalLookupNonVoidableBase.qml"_s);
+ QQmlComponent c(&engine, document);
+ QVERIFY2(c.isReady(), qPrintable(c.errorString()));
+ QScopedPointer<QObject> o(c.create());
+ QVERIFY(o);
+}
+
void tst_QmlCppCodegen::getOptionalLookupOnQJSValueNonStrict()
{
QQmlEngine engine;