16 const QV4::Compiler::JSUnitGenerator *unitGenerator,
17 const QQmlJSTypeResolver *typeResolver, QQmlJSLogger *logger,
18 const BasicBlocks &basicBlocks,
const InstructionAnnotations &annotations,
20 : QQmlJSTypePropagator(unitGenerator, typeResolver, logger, basicBlocks, annotations),
21 m_passManager(passManager), m_contextPropertyInfo(contextPropertyInfo)
28 QQmlJSTypePropagator::generate_Ret();
30 if (m_function->isSignalHandler) {
32 }
else if (m_state.accumulatorIn().contains(m_typeResolver->voidType())) {
34 }
else if (!m_returnType.isValid() && m_state.accumulatorIn().isValid()) {
35 if (m_function->isFullyTyped) {
37 m_logger->log(u"Function without return type annotation returns %1"_s.arg(
38 m_state.accumulatorIn().containedTypeName()),
39 qmlIncompatibleType, currentFunctionSourceLocation());
41 }
else if (!canConvertFromTo(m_state.accumulatorIn(), m_returnType)) {
42 m_logger->log(u"Cannot assign binding of type %1 to %2"_s.arg(
43 m_state.accumulatorIn().containedTypeName(),
44 m_returnType.containedTypeName()),
45 qmlIncompatibleType, currentFunctionSourceLocation());
48 const QQmlJS::SourceLocation location = m_function->isProperty
49 ? currentFunctionSourceLocation()
50 : currentNonEmptySourceLocation();
51 QQmlSA::PassManagerPrivate::get(m_passManager)
53 QQmlJSScope::createQQmlSAElement(m_function->qmlScope.containedType()),
54 QQmlJSScope::createQQmlSAElement(m_state.accumulatorIn().containedType()),
55 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(location));
60 QQmlJSTypePropagator::generate_LoadQmlContextPropertyLookup(index);
62 Q_ASSERT(m_idMemberShadows);
64 const int nameIndex = m_jsUnitGenerator->lookupNameIndex(index);
65 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
67 const auto qmlScope = m_function->qmlScope.containedType();
68 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
69 QQmlJSScope::createQQmlSAElement(qmlScope), name,
70 QQmlJSScope::createQQmlSAElement(qmlScope),
71 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
72 currentNonEmptySourceLocation()));
74 const auto &accumulatorOut = m_state.accumulatorOut();
75 if (!accumulatorOut.isValid())
79 if (accumulatorOut.variant() == QQmlJSRegisterContent::Attachment
80 || accumulatorOut.variant() == QQmlJSRegisterContent::MetaType) {
81 if (m_renamedComponents) {
82 m_renamedComponents->handleRenamedType(accumulatorOut.scopeType(), name,
83 currentNonEmptySourceLocation(), m_logger);
87 const QQmlJSScope::ConstPtr scope = accumulatorOut.scopeType();
88 const QQmlJSScope::ConstPtr idScope = m_scopesById.scope(name, scope);
89 if (!idScope.isNull()) {
90 const auto log = [&](
const auto &memberType,
const auto &memberOwnerScope) {
94 if (m_idMemberShadows->contains(idMemberShadow))
97 m_idMemberShadows->insert(std::move(idMemberShadow));
98 const auto useLoc = currentSourceLocation();
99 m_logger->log(
"Id for object %1 shadows %2 \"%3\". Rename one or the other."_L1
100 .arg(idScope->baseTypeName(), memberType, name),
101 qmlIdShadowsMember, useLoc);
102 m_logger->log(
"Note: Id defined here"_L1, qmlIdShadowsMember,
103 idScope->idSourceLocation(),
true,
true, {}, useLoc.startLine);
106 if (scope->hasProperty(name)) {
107 log(
"property"_L1, scope->ownerOfProperty(scope, name).scope);
108 }
else if (scope->hasMethod(name)) {
109 const auto methods = scope->methods(name);
110 const auto &method = methods[0];
111 if (method.methodType() == QQmlSA::MethodType::Method)
112 log(
"method"_L1, scope->ownerOfMethod(scope, name).scope);
113 else if (method.methodType() == QQmlSA::MethodType::Signal)
114 log(
"signal"_L1, scope->ownerOfMethod(scope, name).scope);
121 QQmlJSTypePropagator::generate_GetOptionalLookup(index, offset);
123 auto suggMsg =
"Consider using non-optional chaining instead: '?.' -> '.'"_L1;
124 auto suggestion = std::make_optional(QQmlJSFixSuggestion(suggMsg, currentSourceLocation()));
125 if (m_state.accumulatorOut().variant() == QQmlJSRegisterContent::Enum) {
126 m_logger->log(
"Redundant optional chaining for enum lookup"_L1, qmlRedundantOptionalChaining,
127 currentSourceLocation(),
true,
true, suggestion);
128 }
else if (!m_state.accumulatorIn().containedType()->isReferenceType()
129 && !m_typeResolver->canHoldUndefined(m_state.accumulatorIn())) {
130 auto baseType = m_state.accumulatorIn().containedTypeName();
131 m_logger->log(
"Redundant optional chaining for lookup on non-voidable and non-nullable "_L1
132 "type %1"_L1.arg(baseType), qmlRedundantOptionalChaining,
133 currentSourceLocation(),
true,
true, suggestion);
139 QQmlJSTypePropagator::generate_StoreProperty(nameIndex, base);
141 auto callBase = m_state.registers[base].content;
142 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
143 const bool isAttached = callBase.variant() == QQmlJSRegisterContent::Attachment;
145 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
146 QQmlJSScope::createQQmlSAElement(callBase.containedType()),
148 QQmlJSScope::createQQmlSAElement(
149 m_state.accumulatorIn().containedType()),
150 QQmlJSScope::createQQmlSAElement(isAttached
151 ? callBase.attachee().containedType()
152 : m_function->qmlScope.containedType()),
153 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
154 currentNonEmptySourceLocation()));
159 QQmlJSTypePropagator::generate_CallProperty(nameIndex, base, argc, argv);
161 const auto saCheck = [&](
const QString &propertyName,
const QQmlJSScope::ConstPtr &baseType) {
162 const QQmlSA::Element saBaseType{ QQmlJSScope::createQQmlSAElement(baseType) };
163 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
164 m_function->qmlScope.containedType()) };
165 const QQmlSA::SourceLocation saLocation{
166 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
169 QQmlSA::PassManagerPrivate::get(m_passManager)
170 ->analyzeRead(saBaseType, propertyName, saContainedType, saLocation);
171 QQmlSA::PassManagerPrivate::get(m_passManager)
172 ->analyzeCall(saBaseType, propertyName, saContainedType, saLocation);
175 const auto callBase = m_state.registers[base].content;
176 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
177 const auto member = m_typeResolver->memberType(callBase, propertyName);
179 const bool isLoggingMethod = QQmlJSTypePropagator::isLoggingMethod(propertyName);
180 if (callBase.contains(m_typeResolver->mathObject()))
181 saCheck(propertyName, callBase.containedType());
182 else if (callBase.contains(m_typeResolver->consoleObject()) && isLoggingMethod)
183 saCheck(propertyName, callBase.containedType());
184 else if (!member.isMethod()) {
185 if (callBase.contains(m_typeResolver->jsValueType())
186 || callBase.contains(m_typeResolver->varType())) {
187 saCheck(propertyName, callBase.containedType());
194 QQmlJSTypePropagator::generate_CallPossiblyDirectEval(argc, argv);
196 const QQmlSA::SourceLocation saLocation{
197 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
199 const QQmlSA::Element saBaseType{ QQmlJSScope::createQQmlSAElement(
200 m_typeResolver->jsGlobalObject()) };
201 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
202 m_function->qmlScope.containedType()) };
204 QQmlSA::PassManagerPrivate::get(m_passManager)
205 ->analyzeCall(saBaseType,
"eval"_L1, saContainedType, saLocation);
210 QQmlJSTypePropagator::handleUnqualifiedAccess(name, isMethod);
212 auto location = currentSourceLocation();
214 const auto qmlScopeContained = m_function->qmlScope.containedType();
215 if (qmlScopeContained->isInCustomParserParent()) {
217 if (qmlScopeContained->baseType().isNull()
218 || qmlScopeContained->baseType()->internalName() != u"QQmlConnections"_s)
223 if (isCallingProperty(qmlScopeContained, name))
225 }
else if (propertyResolution(qmlScopeContained, name) != PropertyMissing) {
229 std::optional<QQmlJSFixSuggestion> suggestion;
231 const auto childScopes = m_function->qmlScope.containedType()->childScopes();
232 for (qsizetype i = 0, end = childScopes.size(); i < end; i++) {
233 auto &scope = childScopes[i];
234 if (location.offset > scope->sourceLocation().offset) {
236 && childScopes.at(i + 1)->sourceLocation().offset < location.offset)
238 if (scope->childScopes().size() == 0)
241 const auto jsId = scope->childScopes().first()->jsIdentifier(name);
243 if (jsId.has_value() && jsId->kind == QQmlJSScope::JavaScriptIdentifier::Injected) {
244 const QQmlJSScope::JavaScriptIdentifier id = jsId.value();
246 QQmlJS::SourceLocation fixLocation = id.location;
247 Q_UNUSED(fixLocation)
248 fixLocation.length = 0;
250 const auto handler = m_typeResolver->signalHandlers()[id.location];
252 QString fixString = handler.isMultiline ? u"function("_s : u"("_s;
253 const auto parameters = handler.signalParameters;
254 for (
int numParams = parameters.size(); numParams > 0; --numParams) {
255 fixString += parameters.at(parameters.size() - numParams);
257 fixString += u", "_s;
260 fixString += handler.isMultiline ? u") "_s : u") => "_s;
263 QQmlJSFixSuggestion{ u"\"%1\" is ambiguous. "
264 "Use a function instead: %2%3"_s.arg(
266 handler.isMultiline ?
"{ ... }"_L1 :
"..."_L1),
267 fixLocation, fixString };
268 suggestion->setAutoApplicable();
278 const auto qmlScope = m_function->qmlScope.containedType();
279 if (name == u"model" || name == u"index") {
280 if (
const QQmlJSScope::ConstPtr parent = qmlScope->parentScope(); !parent.isNull()) {
281 const auto bindings = parent->ownPropertyBindings(u"delegate"_s);
283 for (
auto it = bindings.first; it != bindings.second; it++) {
284 if (!it->hasObject())
286 if (it->objectType() == qmlScope) {
287 suggestion = QQmlJSFixSuggestion {
288 "'%1' is implicitly injected into this delegate. "
289 "Add a required property '%1' to the delegate instead."_L1
291 qmlScope->sourceLocation()
300 if (!suggestion.has_value()) {
301 for (QQmlJSScope::ConstPtr scope = qmlScope; !scope.isNull(); scope = scope->parentScope()) {
302 if (scope->hasProperty(name)) {
303 QQmlJSScopesById::MostLikelyCallback<QString> id;
304 m_function->addressableScopes.possibleIds(scope, qmlScope, Default, id);
306 QQmlJS::SourceLocation fixLocation = location;
307 fixLocation.length = 0;
308 QString m =
"%1 is a member of a parent element.\n You can qualify the "
309 "access with its id to avoid this warning%2.\n"_L1.arg(name);
310 m = m.arg(id.result.isEmpty() ?
" (You first have to give the element an id)"_L1 :
""_L1);
312 suggestion = QQmlJSFixSuggestion{
313 m, fixLocation, (id.result.isEmpty() ? u"<id>."_s : (id.result + u'.'))
316 if (!id.result.isEmpty())
317 suggestion->setAutoApplicable();
322 if (!suggestion.has_value() && !m_function->addressableScopes.componentsAreBound()
323 && m_function->addressableScopes.existsAnywhereInDocument(name)) {
324 const QLatin1String replacement =
"pragma ComponentBehavior: Bound"_L1;
325 QQmlJSFixSuggestion bindComponents {
326 "Set \"%1\" in order to use IDs from outer components in nested components."_L1
328 QQmlJS::s_documentOrigin,
329 replacement +
'\n'_L1
331 bindComponents.setAutoApplicable();
332 suggestion = std::move(bindComponents);
335 if (!suggestion.has_value()) {
336 if (
auto didYouMean = QQmlJSUtils::didYouMean(
337 name, qmlScope->properties().keys() + qmlScope->methods().keys(), location);
338 didYouMean.has_value()) {
339 suggestion = std::move(didYouMean);
343 m_logger->log(QLatin1String(
"Unqualified access"), qmlUnqualified, location,
true,
true,
362 const QString &name,
bool isMethod)
const
364 QQmlJSTypePropagator::handleUnqualifiedAccessAndContextProperties(name, isMethod);
366 if (m_contextPropertyInfo.userContextProperties.isUnqualifiedAccessDisabled(name))
369 const auto warningMessage = [&name,
this]() {
371 "Potential context property access detected."
372 " Context properties are discouraged in QML: use normal, required, or singleton properties instead."_L1;
374 if (shouldMentionRequiredProperties(m_function->qmlScope.containedType())) {
376 "\nNote: '%1' assumed to be a potential context property because it is not declared as required property."_L1
382 if (m_contextPropertyInfo.userContextProperties.isOnUsageWarned(name)) {
383 m_logger->log(warningMessage(), qmlContextProperties, currentSourceLocation());
388 handleUnqualifiedAccess(name, isMethod);
390 const QList<QQmlJS::HeuristicContextProperty> definitions =
391 m_contextPropertyInfo.heuristicContextProperties.definitionsForName(name);
392 if (definitions.isEmpty())
394 QString warning = warningMessage();
395 for (
const auto &candidate : definitions) {
396 warning.append(
"\nNote: candidate context property declaration '%1' at %2:%3:%4"_L1.arg(
397 name, QDir::cleanPath(candidate.filename),
398 QString::number(candidate.location.startLine),
399 QString::number(candidate.location.startColumn)));
401 m_logger->log(warning, qmlContextProperties, currentSourceLocation());
407 QQmlJSTypePropagator::checkDeprecated(scope, name, isMethod);
409 Q_ASSERT(!scope.isNull());
410 auto qmlScope = QQmlJSScope::findCurrentQMLScope(scope);
411 if (qmlScope.isNull())
414 QList<QQmlJSAnnotation> annotations;
416 QQmlJSMetaMethod method;
419 const QList<QQmlJSMetaMethod> methods = qmlScope->methods(name);
420 if (methods.isEmpty())
422 method = methods.constFirst();
423 annotations = method.annotations();
425 QQmlJSMetaProperty property = qmlScope->property(name);
426 if (!property.isValid())
428 annotations = property.annotations();
431 auto deprecationAnn = std::find_if(
432 annotations.constBegin(), annotations.constEnd(),
433 [](
const QQmlJSAnnotation &annotation) {
return annotation.isDeprecation(); });
435 if (deprecationAnn == annotations.constEnd())
438 QQQmlJSDeprecation deprecation = deprecationAnn->deprecation();
440 QString descriptor = name;
442 descriptor += u'(' + method.parameterNames().join(u", "_s) + u')';
444 QString message =
"%1 \"%2\" is deprecated"_L1
445 .arg(isMethod ? u"Method"_s : u"Property"_s, descriptor);
447 if (!deprecation.reason.isEmpty())
448 message.append(QStringLiteral(
" (Reason: %1)").arg(deprecation.reason));
450 m_logger->log(message, qmlDeprecated, currentSourceLocation());
454 const QString &name)
const
456 const bool res = QQmlJSTypePropagator::isCallingProperty(scope, name);
458 if (
const auto property = scope->property(name); property.isValid()) {
460 if (property.type() == m_typeResolver->varType()) {
461 errorType = u"a var property. It may or may not be a method. "_s
462 u"Use a regular function instead."_s;
463 }
else if (property.type() == m_typeResolver->jsValueType()) {
464 errorType = u"a QJSValue property. It may or may not be a method. "_s
465 u"Use a regular Q_INVOKABLE instead."_s;
467 errorType = u"not a method"_s;
470 m_logger->log(u"Property \"%1\" is %2"_s.arg(name, errorType),
471 qmlUseProperFunction, currentSourceLocation(),
true,
true, {});
479 const auto res = QQmlJSTypePropagator::handleImportNamespaceLookup(propertyName);
481 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
482 if (m_typeResolver->isPrefix(propertyName)) {
483 if (!accumulatorIn.containedType()->isReferenceType()) {
484 m_logger->log(u"Cannot use non-QObject type %1 to access prefixed import"_s.arg(
485 accumulatorIn.containedType()->internalName()),
486 qmlPrefixedImportType,
487 currentSourceLocation());
489 }
else if (accumulatorIn.isImportNamespace()) {
490 m_logger->log(u"Type not found in namespace"_s, qmlUnresolvedType,
491 currentSourceLocation());
499 QQmlJSTypePropagator::handleLookupError(propertyName);
501 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
502 const QString typeName = accumulatorIn.containedTypeName();
504 if (typeName == u"QVariant")
506 if (accumulatorIn.isList() && propertyName == u"length")
509 auto baseType = accumulatorIn.containedType();
512 if (propertyResolution(baseType, propertyName) != PropertyMissing)
515 if (baseType->isScript())
518 std::optional<QQmlJSFixSuggestion> fixSuggestion;
520 if (
auto suggestion = QQmlJSUtils::didYouMean(
521 propertyName, baseType->properties().keys(), currentSourceLocation());
522 suggestion.has_value()) {
523 fixSuggestion = std::move(suggestion);
526 if (!fixSuggestion.has_value()
527 && accumulatorIn.variant() == QQmlJSRegisterContent::MetaType) {
529 const QQmlJSScope::ConstPtr scopeType = accumulatorIn.scopeType();
530 const auto metaEnums = scopeType->enumerations();
531 const bool enforcesScoped = scopeType->enforcesScopedEnums();
533 QStringList enumKeys;
534 for (
const QQmlJSMetaEnum &metaEnum : metaEnums) {
535 if (!enforcesScoped || !metaEnum.isScoped())
536 enumKeys << metaEnum.keys();
539 if (
auto suggestion = QQmlJSUtils::didYouMean(
540 propertyName, enumKeys, currentSourceLocation());
541 suggestion.has_value()) {
542 fixSuggestion = std::move(suggestion);
546 m_logger->log(u"Member \"%1\" not found on type \"%2\""_s.arg(propertyName, typeName),
547 qmlMissingProperty, currentSourceLocation(),
true,
true, fixSuggestion);
551 const QString &propertyName)
553 const bool res = QQmlJSTypePropagator::checkForEnumProblems(base, propertyName);
555 if (base.isEnumeration()) {
556 const auto metaEnum = base.enumeration();
557 if (!metaEnum.hasKey(propertyName)) {
558 const auto fixSuggestion = QQmlJSUtils::didYouMean(propertyName, metaEnum.keys(),
559 currentSourceLocation());
560 const QString error = u"\"%1\" is not an entry of enum \"%2\"."_s
561 .arg(propertyName, metaEnum.name());
562 m_logger->log(error, qmlMissingEnumEntry, currentSourceLocation(),
true,
true,
572 QQmlJSTypePropagator::generate_StoreNameCommon(nameIndex);
574 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
575 const QQmlJSRegisterContent in = m_state.accumulatorIn();
576 const bool isAttached = in.variant() == QQmlJSRegisterContent::Attachment;
578 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
579 QQmlJSScope::createQQmlSAElement(
580 m_state.accumulatorIn().containedType()),
582 QQmlJSScope::createQQmlSAElement(isAttached
583 ? in.attachee().containedType()
584 : m_function->qmlScope.containedType()),
585 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
586 currentNonEmptySourceLocation()));
588 const auto qmlScope = m_function->qmlScope.containedType();
589 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
590 QQmlJSScope::createQQmlSAElement(qmlScope), name,
591 QQmlJSScope::createQQmlSAElement(in.containedType()),
592 QQmlJSScope::createQQmlSAElement(qmlScope),
593 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
594 currentNonEmptySourceLocation()));
599 QQmlJSTypePropagator::propagatePropertyLookup(name, lookupIndex);
601 const QQmlJSRegisterContent in = m_state.accumulatorIn();
602 const bool isAttached = in.variant() == QQmlJSRegisterContent::Attachment;
604 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
605 QQmlJSScope::createQQmlSAElement(
606 m_state.accumulatorIn().containedType()),
608 QQmlJSScope::createQQmlSAElement(isAttached
609 ? in.attachee().containedType()
610 : m_function->qmlScope.containedType()),
611 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
612 currentNonEmptySourceLocation()));
616 QQmlJSRegisterContent scope)
618 QQmlJSTypePropagator::propagateCall(methods, argc, argv, scope);
621 const QQmlJSMetaMethod match = bestMatchForCall(methods, argc, argv, &errors);
622 if (!match.isValid())
625 const QQmlSA::Element saBaseType = QQmlJSScope::createQQmlSAElement(scope.containedType());
626 const QQmlSA::SourceLocation saLocation{
627 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
629 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
630 m_function->qmlScope.containedType()) };
632 QQmlSA::PassManagerPrivate::get(m_passManager)
633 ->analyzeCall(saBaseType, match.methodName(), saContainedType, saLocation);
669 const QQmlJSScope::ConstPtr &rhs,
670 const QQmlJSTypeResolver *resolver)
675 if (resolver->isNumeric(lhs) && resolver->isNumeric(rhs))
678 if (isVoidOrUndefined(lhs, resolver) || isVoidOrUndefined(rhs, resolver))
681 if (isStringOrNumberOrBoolean(lhs, resolver)
682 && !mightContainStringOrNumberOrBoolean(rhs, resolver)) {
686 if (isStringOrNumberOrBoolean(rhs, resolver)
687 && !mightContainStringOrNumberOrBoolean(lhs, resolver)) {