Skip to content

Commit 27b7410

Browse files
scheglovCommit Queue
authored andcommitted
Issue 61914. Fixes for FunctionReferenceImpl and PrefixedIdentifierImpl function, when prefix is not in scope.
Bug: #61914 Change-Id: I331cdb812e5bfe890e2f9d05c87cb4e9582d08b6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/460205 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent cbfd802 commit 27b7410

File tree

4 files changed

+200
-149
lines changed

4 files changed

+200
-149
lines changed

pkg/analysis_server/test/analysis/get_hover_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ void f(A<int> a) {
12541254
expect(hover.elementDescription, 'int foo<U>(int t, U u)');
12551255
expect(hover.elementKind, 'method');
12561256
// types
1257-
expect(hover.staticType, isNull);
1257+
expect(hover.staticType, 'int Function<U>(int, U)');
12581258
expect(hover.propagatedType, isNull);
12591259
// no parameter
12601260
expect(hover.parameter, isNull);
@@ -1281,7 +1281,7 @@ void f() {
12811281
expect(hover.elementDescription, 'int foo<U>(U u)');
12821282
expect(hover.elementKind, 'method');
12831283
// types
1284-
expect(hover.staticType, isNull);
1284+
expect(hover.staticType, 'int Function<U>(U)');
12851285
expect(hover.propagatedType, isNull);
12861286
// no parameter
12871287
expect(hover.parameter, isNull);

pkg/analyzer/lib/src/dart/resolver/function_reference_resolver.dart

Lines changed: 24 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,6 @@ class FunctionReferenceResolver {
8888
}
8989
}
9090

91-
/// Checks for a type instantiation of a `dynamic`-typed expression.
92-
///
93-
/// Returns `true` if an error was reported, and resolution can stop.
94-
bool _checkDynamicTypeInstantiation(
95-
FunctionReferenceImpl node,
96-
PrefixedIdentifierImpl function,
97-
Element prefixElement,
98-
) {
99-
DartType? prefixType;
100-
if (prefixElement is VariableElement) {
101-
prefixType = prefixElement.type;
102-
} else if (prefixElement is PropertyAccessorElement) {
103-
prefixType = prefixElement.variable.type;
104-
}
105-
106-
if (prefixType is DynamicType) {
107-
_diagnosticReporter.atNode(
108-
function,
109-
CompileTimeErrorCode.genericMethodTypeInstantiationOnDynamic,
110-
);
111-
node.recordStaticType(InvalidTypeImpl.instance, resolver: _resolver);
112-
return true;
113-
}
114-
return false;
115-
}
116-
11791
List<TypeImpl> _checkTypeArguments(
11892
TypeArgumentList typeArgumentList,
11993
String? name,
@@ -433,63 +407,30 @@ class FunctionReferenceResolver {
433407
FunctionReferenceImpl node,
434408
PrefixedIdentifierImpl function,
435409
) {
436-
var prefixElement = function.prefix.scopeLookupResult!.getter;
410+
_resolver.analyzeExpression(function, _resolver.operations.unknownType);
411+
_resolver.popRewrite();
437412

438-
if (prefixElement == null) {
439-
_diagnosticReporter.atNode(
440-
function.prefix,
441-
CompileTimeErrorCode.undefinedIdentifier,
442-
arguments: [function.name],
443-
);
444-
function.setPseudoExpressionStaticType(InvalidTypeImpl.instance);
413+
var propertyType = function.typeOrThrow;
414+
415+
if (function.element is ExtensionElement) {
445416
node.recordStaticType(InvalidTypeImpl.instance, resolver: _resolver);
446417
return;
447418
}
448419

449-
function.prefix.element = prefixElement;
450-
function.prefix.setPseudoExpressionStaticType(
451-
prefixElement is PromotableElementImpl
452-
? _resolver.localVariableTypeProvider.getType(
453-
function.prefix,
454-
isRead: true,
455-
)
456-
: prefixElement.referenceType,
457-
);
458-
var functionName = function.identifier.name;
459-
460-
if (prefixElement is PrefixElement) {
461-
var functionElement = prefixElement.scope.lookup(functionName).getter;
462-
if (functionElement == null) {
463-
_diagnosticReporter.atNode(
464-
function.identifier,
465-
CompileTimeErrorCode.undefinedPrefixedName,
466-
arguments: [functionName, function.prefix.name],
467-
);
468-
function.setPseudoExpressionStaticType(InvalidTypeImpl.instance);
469-
node.recordStaticType(InvalidTypeImpl.instance, resolver: _resolver);
470-
return;
471-
} else {
472-
_resolveReceiverPrefix(node, prefixElement, function, functionElement);
473-
return;
474-
}
475-
}
476-
477-
if (_checkDynamicTypeInstantiation(node, function, prefixElement)) {
420+
if (propertyType is InvalidType) {
421+
node.recordStaticType(InvalidTypeImpl.instance, resolver: _resolver);
478422
return;
479423
}
480424

481-
if (prefixElement is TopLevelFunctionElement &&
482-
functionName == MethodElement.CALL_METHOD_NAME) {
483-
_resolve(node: node, rawType: prefixElement.type, name: functionName);
425+
if (propertyType is DynamicTypeImpl) {
426+
_diagnosticReporter.atNode(
427+
function,
428+
CompileTimeErrorCode.genericMethodTypeInstantiationOnDynamic,
429+
);
430+
node.recordStaticType(InvalidTypeImpl.instance, resolver: _resolver);
484431
return;
485432
}
486433

487-
var propertyType = _resolveTypeProperty(
488-
receiver: function.prefix,
489-
name: function.identifier,
490-
nameErrorEntity: function,
491-
);
492-
493434
var callMethod = _getCallMethod(node, propertyType);
494435
if (callMethod is MethodElement) {
495436
_resolveAsImplicitCallReference(node, callMethod);
@@ -498,20 +439,22 @@ class FunctionReferenceResolver {
498439

499440
if (propertyType is FunctionType) {
500441
function.setPseudoExpressionStaticType(propertyType);
442+
var functionName = function.identifier.name;
501443
_resolve(node: node, rawType: propertyType, name: functionName);
502444
return;
503445
}
504446

505-
if (propertyType != null) {
506-
// If the property is unknown, [UNDEFINED_GETTER] is reported elsewhere.
507-
// If it is known, we must report the bad type instantiation here.
508-
_diagnosticReporter.atNode(
509-
function.identifier,
510-
CompileTimeErrorCode.disallowedTypeInstantiationExpression,
511-
);
447+
if (function.prefix.element case PrefixElement prefixElement) {
448+
if (function.element case var functionElement?) {
449+
_resolveReceiverPrefix(node, prefixElement, function, functionElement);
450+
return;
451+
}
512452
}
513-
_resolver.analyzeExpression(function, _resolver.operations.unknownType);
514-
_resolver.popRewrite();
453+
454+
_diagnosticReporter.atNode(
455+
function.identifier,
456+
CompileTimeErrorCode.disallowedTypeInstantiationExpression,
457+
);
515458
node.recordStaticType(InvalidTypeImpl.instance, resolver: _resolver);
516459
}
517460

@@ -635,36 +578,12 @@ class FunctionReferenceResolver {
635578
_resolveConstructorReference(node);
636579
return;
637580
} else if (element is InterfaceElementImpl) {
638-
_resolver.analyzeExpression(
639-
node.function,
640-
_resolver.operations.unknownType,
641-
);
642-
_resolver.popRewrite();
643581
_resolveDirectTypeLiteral(node, prefix, element);
644582
return;
645583
} else if (element is TypeAliasElementImpl) {
646-
_resolver.analyzeExpression(prefix, _resolver.operations.unknownType);
647-
_resolver.popRewrite();
648584
_resolveTypeAlias(node: node, element: element, typeAlias: prefix);
649585
return;
650586
}
651-
} else if (element is ExecutableElement) {
652-
_resolver.analyzeExpression(
653-
node.function,
654-
_resolver.operations.unknownType,
655-
);
656-
_resolver.popRewrite();
657-
var callMethod = _getCallMethod(node, node.function.staticType);
658-
if (callMethod is MethodElement) {
659-
_resolveAsImplicitCallReference(node, callMethod);
660-
return;
661-
}
662-
_resolve(
663-
node: node,
664-
rawType: node.function.typeOrThrow as FunctionType,
665-
name: element.name,
666-
);
667-
return;
668587
} else if (element is ExtensionElement) {
669588
prefix.identifier.element = element;
670589
prefix.identifier.setPseudoExpressionStaticType(InvalidTypeImpl.instance);

0 commit comments

Comments
 (0)