aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2023-11-16 16:22:38 +0100
committerUlf Hermann <ulf.hermann@qt.io>2023-11-17 15:28:57 +0100
commit096f3c3097cb2f60c2f44d4d8c734fc33ffd0a3e (patch)
treec68b6ac957f904b9593b551d090ea54ccddc7a0c
parentfa09df7e1ab43a7ca4f81076c518d8530eea711d (diff)
QmlCompiler: Don't access unavailable input accumulator on 'as'
We only read it if the target type is composite. Therefore, check for composite first, before looking for the accumulator. Pick-to: 6.6 6.5 6.2 Fixes: QTBUG-119122 Change-Id: I2a50214f76ee4ffe2f877dc690704e7475b8bd77 Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qmlcompiler/qqmljscodegenerator.cpp5
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/asCast.qml10
-rw-r--r--tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp10
3 files changed, 22 insertions, 3 deletions
diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp
index f3c57276e1..3d87a709d7 100644
--- a/src/qmlcompiler/qqmljscodegenerator.cpp
+++ b/src/qmlcompiler/qqmljscodegenerator.cpp
@@ -2861,9 +2861,8 @@ void QQmlJSCodeGenerator::generate_As(int lhs)
? input
: convertStored(inputContent.storedType(), genericContained, input);
- if (m_typeResolver->equals(
- m_state.accumulatorIn().storedType(), m_typeResolver->metaObjectType())
- && contained->isComposite()) {
+ if (contained->isComposite() && m_typeResolver->equals(
+ m_state.accumulatorIn().storedType(), m_typeResolver->metaObjectType())) {
m_body += conversion(
genericContained, outputContent,
m_state.accumulatorVariableIn + u"->cast("_s + inputConversion + u')');
diff --git a/tests/auto/qml/qmlcppcodegen/data/asCast.qml b/tests/auto/qml/qmlcppcodegen/data/asCast.qml
index 1befc08d0a..cb8155ca6c 100644
--- a/tests/auto/qml/qmlcppcodegen/data/asCast.qml
+++ b/tests/auto/qml/qmlcppcodegen/data/asCast.qml
@@ -28,4 +28,14 @@ Item {
property QtObject dummyAsItem: dummy as Item
property QtObject dummyAsRectangle: dummy as Rectangle
property QtObject dummyAsDummy: dummy as Dummy
+
+ property QtObject nullAsObject: null as QtObject
+ property QtObject nullAsItem: null as Item
+ property QtObject nullAsRectangle: null as Rectangle
+ property QtObject nullAsDummy: null as Dummy
+
+ property QtObject undefinedAsObject: undefined as QtObject
+ property QtObject undefinedAsItem: undefined as Item
+ property QtObject undefinedAsRectangle: undefined as Rectangle
+ property QtObject undefinedAsDummy: undefined as Dummy
}
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
index 37f874ba91..5d44b84475 100644
--- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
+++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
@@ -657,6 +657,16 @@ void tst_QmlCppCodegen::asCast()
QCOMPARE(qvariant_cast<QObject *>(root->property("dummyAsItem")), dummy);
QCOMPARE(qvariant_cast<QObject *>(root->property("dummyAsRectangle")), nullptr);
QCOMPARE(qvariant_cast<QObject *>(root->property("dummyAsDummy")), dummy);
+
+ QCOMPARE(qvariant_cast<QObject *>(root->property("nullAsObject")), nullptr);
+ QCOMPARE(qvariant_cast<QObject *>(root->property("nullAsItem")), nullptr);
+ QCOMPARE(qvariant_cast<QObject *>(root->property("nullAsRectangle")), nullptr);
+ QCOMPARE(qvariant_cast<QObject *>(root->property("nullAsDummy")), nullptr);
+
+ QCOMPARE(qvariant_cast<QObject *>(root->property("undefinedAsObject")), nullptr);
+ QCOMPARE(qvariant_cast<QObject *>(root->property("undefinedAsItem")), nullptr);
+ QCOMPARE(qvariant_cast<QObject *>(root->property("undefinedAsRectangle")), nullptr);
+ QCOMPARE(qvariant_cast<QObject *>(root->property("undefinedAsDummy")), nullptr);
}
void tst_QmlCppCodegen::attachedBaseEnum()