Move literal types check out of QQmlJSTypeResolver

The type resolver should not do any semantic analysis on its own. This
fixes a bug triggered by invoking the literal type analysis from
qmlcachegen (which is not interested in literal bindings at all).

While this doesn't fix the root cause, qmlcachegen will definitely not
crash in this place anymore. There will be a follow-up bug to figure out
what is actually happening.

Pick-to: 6.3
Fixes: QTBUG-102598
Change-Id: Ic5b1f1cdebc960e0ec56f54d8bdd11bac08f0521
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Maximilian Goldstein <max.goldstein@qt.io>
This commit is contained in:
Ulf Hermann 2022-04-27 09:19:54 +02:00
parent 3265013769
commit dc815b797c
7 changed files with 146 additions and 29 deletions

View File

@ -18,6 +18,7 @@ qt_internal_add_module(QmlCompilerPrivate
qqmljsfunctioninitializer.cpp qqmljsfunctioninitializer_p.h
qqmljsimporter.cpp qqmljsimporter_p.h
qqmljsimportvisitor.cpp qqmljsimportvisitor_p.h
qqmljsliteralbindingcheck.cpp qqmljsliteralbindingcheck_p.h
qqmljsloadergenerator.cpp qqmljsloadergenerator_p.h
qqmljslogger_p.h qqmljslogger.cpp
qqmljsmetatypes_p.h qqmljsmetatypes.cpp

View File

@ -32,6 +32,7 @@
#include <QtQmlCompiler/private/qqmljsimporter_p.h>
#include <QtQmlCompiler/private/qqmljsimportvisitor_p.h>
#include <QtQmlCompiler/private/qqmljsliteralbindingcheck_p.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qfileinfo.h>
@ -494,6 +495,9 @@ QQmlJSLinter::LintResult QQmlJSLinter::lintFile(const QString &filename,
typeResolver.init(&v, parser.rootNode());
QQmlJSLiteralBindingCheck literalCheck;
literalCheck.run(&v, &typeResolver);
if (m_enablePlugins) {
QQmlSA::PassManager passMan(&v, &typeResolver);

View File

@ -0,0 +1,76 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qqmljsliteralbindingcheck_p.h"
#include <private/qqmljsimportvisitor_p.h>
#include <private/qqmljstyperesolver_p.h>
#include <private/qqmljsmetatypes_p.h>
QT_BEGIN_NAMESPACE
void QQmlJSLiteralBindingCheck::run(QQmlJSImportVisitor *visitor, QQmlJSTypeResolver *resolver)
{
QQmlJSLogger *logger = visitor->logger();
const auto literalScopes = visitor->literalScopesToCheck();
for (const auto &scope : literalScopes) {
const auto bindings = scope->ownPropertyBindings();
for (const auto &binding : bindings) {
if (!binding.hasLiteral())
continue;
const QString propertyName = binding.propertyName();
const QQmlJSMetaProperty property = scope->property(propertyName);
if (!property.isValid())
continue;
// If the property is defined in the same scope where it is set,
// we are in fact allowed to set it, even if it's not writable.
if (!property.isWritable() && !scope->hasOwnProperty(propertyName)) {
logger->log(u"Cannot assign to read-only property %1"_qs.arg(propertyName),
Log_Type, binding.sourceLocation());
continue;
}
if (!resolver->canConvertFromTo(binding.literalType(resolver), property.type())) {
logger->log(u"Cannot assign binding of type %1 to %2"_qs
.arg(binding.literalTypeName(), property.typeName()),
Log_Type, binding.sourceLocation());
continue;
}
if (resolver->equals(property.type(), resolver->stringType())
&& resolver->isNumeric(binding.literalType(resolver))) {
logger->log(u"Cannot assign a numeric constant to a string property"_qs,
Log_Type, binding.sourceLocation());
}
}
}
}
QT_END_NAMESPACE

View File

@ -0,0 +1,58 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QQMLJSLITERALBINDINGCHECK_P_H
#define QQMLJSLITERALBINDINGCHECK_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
#include <QtCore/qglobal.h>
#include <private/qtqmlcompilerexports_p.h>
QT_BEGIN_NAMESPACE
class QQmlJSImportVisitor;
class QQmlJSTypeResolver;
class Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSLiteralBindingCheck
{
public:
void run(QQmlJSImportVisitor *visitor, QQmlJSTypeResolver *resolver);
};
QT_END_NAMESPACE
#endif // QQMLJSLITERALBINDINGCHECK_P_H

View File

@ -135,34 +135,6 @@ void QQmlJSTypeResolver::init(QQmlJSImportVisitor *visitor, QQmlJS::AST::Node *p
m_objectsByLocation = visitor->scopesBylocation();
m_signalHandlers = visitor->signalHandlers();
m_imports = visitor->imports();
for (const auto &scope : visitor->literalScopesToCheck()) {
for (const auto &binding : scope->ownPropertyBindings()) {
if (!binding.hasLiteral())
continue;
const QQmlJSMetaProperty property = scope->property(binding.propertyName());
if (property.isValid()) {
// If the property is defined in the same scope where it is set,
// we are in fact allowed to set it, even if it's not writable.
if (!property.isWritable() && !scope->hasOwnProperty(binding.propertyName())) {
m_logger->log(u"Cannot assign to read-only property %1"_qs.arg(
binding.propertyName()),
Log_Type, binding.sourceLocation());
continue;
}
if (!canConvertFromTo(binding.literalType(this), property.type())) {
m_logger->log(u"Cannot assign binding of type %1 to %2"_qs
.arg(binding.literalTypeName(), property.typeName()),
Log_Type, binding.sourceLocation());
} else if (equals(property.type(), m_stringType)
&& isNumeric(binding.literalType(this))) {
m_logger->log(u"Cannot assign a numeric constant to a string property"_qs,
Log_Type, binding.sourceLocation());
}
}
}
}
}
QQmlJSScope::ConstPtr

View File

@ -167,13 +167,14 @@ public:
const QQmlJSScope::ConstPtr &b) const;
bool canHoldUndefined(const QQmlJSRegisterContent &content) const;
bool isNumeric(const QQmlJSScope::ConstPtr &type) const;
protected:
QQmlJSRegisterContent memberType(const QQmlJSScope::ConstPtr &type, const QString &name) const;
QQmlJSRegisterContent memberEnumType(const QQmlJSScope::ConstPtr &type,
const QString &name) const;
bool isPrimitive(const QQmlJSScope::ConstPtr &type) const;
bool isNumeric(const QQmlJSScope::ConstPtr &type) const;
bool checkEnums(const QQmlJSScope::ConstPtr &scope, const QString &name,
QQmlJSRegisterContent *result, BaseOrExtension mode) const;
bool canPrimitivelyConvertFromTo(

View File

@ -29,6 +29,7 @@
#include "qmltctyperesolver.h"
#include <private/qqmljsimporter_p.h>
#include <private/qqmljsliteralbindingcheck_p.h>
#include <private/qv4value_p.h>
#include <QtCore/qqueue.h>
@ -41,6 +42,10 @@ Q_LOGGING_CATEGORY(lcTypeResolver2, "qml.qmltc.typeresolver", QtInfoMsg);
void QmltcTypeResolver::init(QmltcVisitor &visitor, QQmlJS::AST::Node *program)
{
QQmlJSTypeResolver::init(&visitor, program);
QQmlJSLiteralBindingCheck literalCheck;
literalCheck.run(&visitor, this);
m_root = visitor.result();
QQueue<QQmlJSScope::Ptr> objects;