Skip to content

Commit 2ea7e55

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Perform type promotion when NNBD, using flow analysis.
Mostly just implementing existing interface LocalVariableTypeProvider, and moving type promotion tests into a separate file. R=brianwilkerson@google.com, paulberry@google.com Change-Id: If864cb2c300f01a152f40c51cffa7f17028ad200 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108080 Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Paul Berry <paulberry@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 766e542 commit 2ea7e55

25 files changed

+1164
-808
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/element.dart';
99
import 'package:analyzer/dart/element/type.dart';
1010
import 'package:analyzer/dart/element/type_system.dart';
1111
import 'package:analyzer/src/dart/resolver/flow_analysis.dart';
12+
import 'package:analyzer/src/generated/variable_type_provider.dart';
1213

1314
/// The helper for performing flow analysis during resolution.
1415
///
@@ -52,6 +53,10 @@ class FlowAnalysisHelper {
5253
this.assignedVariables,
5354
);
5455

56+
LocalVariableTypeProvider get localVariableTypeProvider {
57+
return _LocalVariableTypeProvider(this);
58+
}
59+
5560
VariableElement assignmentExpression(AssignmentExpression node) {
5661
if (flow == null) return null;
5762

@@ -253,11 +258,6 @@ class FlowAnalysisHelper {
253258
if (flow.isNonNullable(element)) {
254259
result.nonNullableNodes.add(node);
255260
}
256-
257-
var promotedType = flow.promotedType(element);
258-
if (promotedType != null) {
259-
result.promotedTypes[node] = promotedType;
260-
}
261261
}
262262
}
263263
}
@@ -350,11 +350,6 @@ class FlowAnalysisResult {
350350
/// there is a `return` statement at the end of the function body block.
351351
final List<FunctionBody> functionBodiesThatDontComplete = [];
352352

353-
/// For each local variable or parameter, which type is promoted to a type
354-
/// specific than its declaration type, this map included references where
355-
/// the variable where it is read, and the type it has.
356-
final Map<SimpleIdentifier, DartType> promotedTypes = {};
357-
358353
void putIntoNode(AstNode node) {
359354
node.setProperty(_astKey, this);
360355
}
@@ -475,6 +470,20 @@ class _FunctionBodyAccess implements FunctionBodyAccess<VariableElement> {
475470
}
476471
}
477472

473+
/// The flow analysis based implementation of [LocalVariableTypeProvider].
474+
class _LocalVariableTypeProvider implements LocalVariableTypeProvider {
475+
final FlowAnalysisHelper _manager;
476+
477+
_LocalVariableTypeProvider(this._manager);
478+
479+
@override
480+
DartType getType(SimpleIdentifier node) {
481+
var variable = node.staticElement as VariableElement;
482+
var promotedType = _manager.flow?.promotedType(variable);
483+
return promotedType ?? variable.type;
484+
}
485+
}
486+
478487
class _NodeOperations implements NodeOperations<Expression> {
479488
@override
480489
Expression unwrapParenthesized(Expression node) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3811,7 +3811,11 @@ class ResolverVisitor extends ScopedVisitor {
38113811

38123812
/// Return the object providing promoted or declared types of variables.
38133813
LocalVariableTypeProvider get localVariableTypeProvider {
3814-
return _promoteManager.localVariableTypeProvider;
3814+
if (_flowAnalysis != null) {
3815+
return _flowAnalysis.localVariableTypeProvider;
3816+
} else {
3817+
return _promoteManager.localVariableTypeProvider;
3818+
}
38153819
}
38163820

38173821
/// Return the static element associated with the given expression whose type

0 commit comments

Comments
 (0)