QmlCompiler: Do not generate lookups for bare meta enums

Such code is necessarily dead or uncompilable.

Pick-to: 6.5 6.2
Fixes: QTBUG-119090
Change-Id: I7319f7ceeb0b4994d5e974bbe8a9c3ba3bf72fc5
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 73f66c8f94)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ulf Hermann 2023-11-17 10:30:34 +01:00 committed by Qt Cherry-pick Bot
parent 6049b22c97
commit 3896fb0dc6
4 changed files with 36 additions and 2 deletions

View File

@ -782,9 +782,15 @@ void QQmlJSCodeGenerator::generateEnumLookup(int index)
{
const QString enumMember = m_state.accumulatorOut().enumMember();
// If we're referring to the type, there's nothing to do.
if (enumMember.isEmpty())
if (enumMember.isEmpty()) {
// If we're referring to the type, there's nothing to do.
// However, we should not get here since no one can ever use the enum metatype.
// The lookup is dead code and should be optimized away.
// ... unless you are actually trying to store the metatype itself in a property.
// We cannot compile such code.
reject(u"Lookup of enum metatype"_s);
return;
}
// If the metaenum has the value, just use it and skip all the rest.
const QQmlJSMetaEnum metaEnum = m_state.accumulatorOut().enumeration();

View File

@ -55,6 +55,7 @@ set(qml_files
SelectionRectangle.qml
ShadowedObjectName.qml
ShadowedObjectNameDerived.qml
StoreMetaEnum.qml
Test.qml
TestCase.qml
WindowDerived.qml

View File

@ -0,0 +1,12 @@
import QtQml
QtObject {
enum Foo {
Bar,
Baz
}
property var eF: StoreMetaEnum.Foo
property int bar: eF.Bar
property int baz: eF.Baz
}

View File

@ -176,6 +176,7 @@ private slots:
void signatureIgnored();
void simpleBinding();
void storeElementSideEffects();
void storeMetaEnum();
void stringArg();
void stringLength();
void stringToByteArray();
@ -3657,6 +3658,20 @@ void tst_QmlCppCodegen::storeElementSideEffects()
QCOMPARE(prop.property(0).toInt(), 10);
}
void tst_QmlCppCodegen::storeMetaEnum()
{
QQmlEngine engine;
QQmlComponent c(&engine, QUrl(u"qrc:/qt/qml/TestTypes/StoreMetaEnum.qml"_s));
QVERIFY2(c.isReady(), qPrintable(c.errorString()));
QScopedPointer<QObject> o(c.create());
QVERIFY(o);
QCOMPARE(o->property("bar").toInt(), 0);
QCOMPARE(o->property("baz").toInt(), 1);
}
void tst_QmlCppCodegen::stringArg()
{
QQmlEngine engine;