Skip to content

Commit 2610a2b

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[cfe] Cache the context type when we resolve a dot shorthand.
Using the type analyzer to cache a shorthand context type. This mechanism will be shared between the analyzer and the CFE. There will be tests for this one the implementation strings through to this part. But otherwise, it's fairly straightforward logic. Bug: #59758 Change-Id: I88ac8283d2901d7d141992ab5ab3a83e40be5912 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410943 Reviewed-by: Paul Berry <paulberry@google.com> Commit-Queue: Kallen Tu <kallentu@google.com>
1 parent 3929dd2 commit 2610a2b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ mixin TypeAnalyzer<
271271
TypeDeclarationType extends Object,
272272
TypeDeclaration extends Object>
273273
implements TypeAnalysisNullShortingInterface<Expression, SharedTypeView> {
274+
/// Cached context types used to resolve dot shorthand heads.
275+
final _dotShorthands = <SharedTypeSchemaView>[];
276+
274277
TypeAnalyzerErrors<Node, Statement, Expression, Variable, SharedTypeView,
275278
Pattern, Error> get errors;
276279

@@ -535,6 +538,17 @@ mixin TypeAnalyzer<
535538
: operations.typeToSchema(declaredType);
536539
}
537540

541+
/// Analyzes a dot shorthand.
542+
/// Saves the [context] for when we resolve the dot shorthand head.
543+
SharedTypeView analyzeDotShorthand(
544+
Expression node, SharedTypeSchemaView context) {
545+
_dotShorthands.add(context);
546+
ExpressionTypeAnalysisResult analysisResult =
547+
dispatchExpression(node, context);
548+
_dotShorthands.removeLast();
549+
return analysisResult.type;
550+
}
551+
538552
/// Analyzes an expression. [node] is the expression to analyze, and
539553
/// [schema] is the type schema which should be used for type inference.
540554
///
@@ -2146,6 +2160,9 @@ mixin TypeAnalyzer<
21462160
required SharedTypeView type,
21472161
});
21482162

2163+
/// Returns the most recently cached dot shorthand context type.
2164+
SharedTypeSchemaView getDotShorthandContext() => _dotShorthands.last;
2165+
21492166
/// If the [element] is a map pattern entry, returns it.
21502167
MapPatternEntry<Expression, Pattern>? getMapPatternEntry(Node element);
21512168

pkg/front_end/lib/src/type_inference/inference_visitor.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12107,8 +12107,11 @@ class InferenceVisitorImpl extends InferenceVisitorBase
1210712107
// Coverage-ignore(suite): Not run.
1210812108
ExpressionInferenceResult visitDotShorthand(
1210912109
DotShorthand node, DartType typeContext) {
12110-
// TODO(kallentu): Implementation needed for dot shorthands.
12111-
return _unhandledExpression(node, typeContext);
12110+
DartType rewrittenType = analyzeDotShorthand(
12111+
node.innerExpression, new SharedTypeSchemaView(typeContext))
12112+
.unwrapTypeView();
12113+
Expression rewrittenExpr = popRewrite() as Expression;
12114+
return new ExpressionInferenceResult(rewrittenType, rewrittenExpr);
1211212115
}
1211312116

1211412117
// Coverage-ignore(suite): Not run.

0 commit comments

Comments
 (0)