Fix comparison AOT-lookup and intended type in value types

We currently force the lookup metatype to be exactly the same data type
with the caller's. As a result, the conversion from enum to integral
data type is not recognized. Relax this comparison by using
isTypeCompatible helper.

Pick-to: 6.4
Fixes: QTBUG-109007
Change-Id: I188dc3e6c1fd7100e9ed5c4ba5d0c90d85d79be4
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Semih Yavuz 2022-12-02 22:07:12 +01:00
parent 4f92f018ea
commit 9793c663cc
5 changed files with 58 additions and 1 deletions

View File

@ -1047,7 +1047,7 @@ static bool initValueLookup(QV4::Lookup *l, QV4::ExecutableCompilationUnit *comp
const QByteArray name = compilationUnit->runtimeStrings[l->nameIndex]->toQString().toUtf8();
const int coreIndex = metaObject->indexOfProperty(name.constData());
QMetaType lookupType = metaObject->property(coreIndex).metaType();
if (type.isValid() && lookupType != type)
if (!isTypeCompatible(type, lookupType))
return false;
l->qgadgetLookup.metaObject = quintptr(metaObject) + 1;
l->qgadgetLookup.coreIndex = coreIndex;

View File

@ -7,6 +7,7 @@ set(cpp_sources
cppbaseclass.h
dynamicmeta.h
enumproblems.h
enumProperty.h
gadgetwithenum.h
invisible.h
multiforeign.h
@ -75,6 +76,7 @@ set(qml_files
deadStoreLoop.qml
dialog.qml
dynamicscene.qml
enumConversion.qml
enumInvalid.qml
enumLookup.qml
enumProblems.qml

View File

@ -0,0 +1,7 @@
import TestTypes
MyType {
id: root
property int test: myEnumType.type
property bool test_1: myEnumType.type
}

View File

@ -0,0 +1,34 @@
#ifndef ENUMPROPERTY_H
#define ENUMPROPERTY_H
#include <QObject>
#include <QtQml>
class MyEnumType
{
Q_GADGET
QML_ANONYMOUS
public:
enum MyEnum {
Sin = 0x01,
Saw = 0x02,
Tri = 0x04,
};
Q_ENUM(MyEnum)
Q_PROPERTY(MyEnum type READ type)
MyEnum type() const { return MyEnum::Tri; }
};
class MyType : public QObject
{
Q_OBJECT
Q_PROPERTY(MyEnumType myEnumType READ myEnumType)
QML_ELEMENT
public:
MyEnumType myEnumType() const { return m_type; }
private:
MyEnumType m_type;
};
#endif // ENUMPROPERTY_H

View File

@ -151,6 +151,7 @@ private slots:
void multiForeign();
void namespaceWithEnum();
void enumProblems();
void enumConversion();
};
void tst_QmlCppCodegen::initTestCase()
@ -2902,6 +2903,19 @@ void tst_QmlCppCodegen::enumProblems()
QCOMPARE(fighter->type(), Foo::Fighter);
}
void tst_QmlCppCodegen::enumConversion()
{
QQmlEngine engine;
QQmlComponent c(&engine, QUrl(u"qrc:/qt/qml/TestTypes/enumConversion.qml"_s));
QVERIFY2(c.isReady(), qPrintable(c.errorString()));
QScopedPointer<QObject> o(c.create());
QVERIFY(o);
QCOMPARE(o->property("test").toInt(), 0x04);
QCOMPARE(o->property("test_1").toBool(), true);
};
QTEST_MAIN(tst_QmlCppCodegen)
#include "tst_qmlcppcodegen.moc"