Skip to content

Commit d7b3562

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot shorthands: FunctionReferences have dot shorthand flag.
`FunctionReference`s like `.foo<T>` can also be dot shorthands. This CL adds the flag onto that AST. Unit test added and co19 tests that have function references are now passing. Bug: #59835 Change-Id: I8c85e7915b665de5c4de2b249b22c6e8d8fc364d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426001 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 57bfb6a commit d7b3562

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8700,9 +8700,10 @@ abstract final class FunctionReference
87008700
///
87018701
/// In error-free code, this is either a [SimpleIdentifier] (indicating a
87028702
/// function that is in scope), a [PrefixedIdentifier] (indicating a either
8703-
/// function imported via prefix or a static method in a class), or a
8703+
/// function imported via prefix or a static method in a class), a
87048704
/// [PropertyAccess] (indicating a static method in a class imported via
8705-
/// prefix). In code with errors, this could be other kinds of expressions.
8705+
/// prefix), or a [DotShorthandPropertyAccess] (indicating a static method in
8706+
/// a class). In code with errors, this could be other kinds of expressions.
87068707
/// For example, `(...)<int>` parses as a [FunctionReference] whose referent
87078708
/// is a [ParenthesizedExpression].
87088709
Expression get function;
@@ -8720,6 +8721,7 @@ abstract final class FunctionReference
87208721
}
87218722

87228723
final class FunctionReferenceImpl extends CommentReferableExpressionImpl
8724+
with DotShorthandMixin
87238725
implements FunctionReference {
87248726
ExpressionImpl _function;
87258727

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,11 +2967,22 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
29672967

29682968
@override
29692969
void visitFunctionReference(
2970-
FunctionReference node, {
2970+
covariant FunctionReferenceImpl node, {
29712971
TypeImpl contextType = UnknownInferredType.instance,
29722972
}) {
29732973
inferenceLogWriter?.enterExpression(node, contextType);
2974-
_functionReferenceResolver.resolve(node as FunctionReferenceImpl);
2974+
2975+
// If [isDotShorthand] is set, cache the context type for resolution.
2976+
if (isDotShorthand(node)) {
2977+
pushDotShorthandContext(node, SharedTypeSchemaView(contextType));
2978+
}
2979+
2980+
_functionReferenceResolver.resolve(node);
2981+
2982+
if (isDotShorthand(node)) {
2983+
popDotShorthandContext();
2984+
}
2985+
29752986
inferenceLogWriter?.exitExpression(node);
29762987
}
29772988

pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,38 @@ DotShorthandPropertyAccess
398398
''');
399399
}
400400

401+
test_functionReference() async {
402+
await assertNoErrorsInCode(r'''
403+
class C<T> {
404+
static String foo<X>() => "C<$X>";
405+
406+
@override
407+
bool operator ==(Object other) {
408+
return false;
409+
}
410+
}
411+
412+
void test<T extends num>() {
413+
C() == .foo<T>;
414+
}
415+
416+
main() {
417+
test<int>();
418+
}
419+
''');
420+
421+
var identifier = findNode.singleDotShorthandPropertyAccess;
422+
assertResolvedNodeText(identifier, r'''
423+
DotShorthandPropertyAccess
424+
period: .
425+
propertyName: SimpleIdentifier
426+
token: foo
427+
element: <testLibraryFragment>::@class::C::@method::foo#element
428+
staticType: String Function<X>()
429+
staticType: String Function<X>()
430+
''');
431+
}
432+
401433
test_futureOr() async {
402434
await assertNoErrorsInCode('''
403435
import 'dart:async';

0 commit comments

Comments
 (0)