Skip to content

Commit 33df2ed

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Switch flow analysis tests to performFlowAnalysis().
So, we can make visitors private. R=brianwilkerson@google.com, paulberry@google.com Change-Id: I8ff98e95b30f4b2d807a7b3d2a63e125d9420669 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107180 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Paul Berry <paulberry@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 7c63999 commit 33df2ed

3 files changed

Lines changed: 80 additions & 90 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,12 @@ class FlowAnalysis<Statement, Expression, Element, Type> {
375375
}
376376
}
377377

378-
/// Return `true` if the [variable] is known to be be nullable.
378+
/// Return `true` if the [variable] is known to be be non-nullable.
379379
bool isNonNullable(Element variable) {
380380
return !_current.notNonNullable.contains(variable);
381381
}
382382

383-
/// Return `true` if the [variable] is known to be be non-nullable.
383+
/// Return `true` if the [variable] is known to be be nullable.
384384
bool isNullable(Element variable) {
385385
return !_current.notNullable.contains(variable);
386386
}

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

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,77 @@ FlowAnalysisResult performFlowAnalysis(
1717
CompilationUnit unit,
1818
) {
1919
var assignedVariables = AssignedVariables<Statement, VariableElement>();
20-
unit.accept(AssignedVariablesVisitor(assignedVariables));
20+
unit.accept(_AssignedVariablesVisitor(assignedVariables));
2121

2222
var readBeforeWritten = <LocalVariableElement>[];
23+
var nullableNodes = <SimpleIdentifier>[];
24+
var nonNullableNodes = <SimpleIdentifier>[];
25+
var unreachableNodes = <AstNode>[];
26+
var functionBodiesThatDontComplete = <FunctionBody>[];
27+
var promotedTypes = <SimpleIdentifier, DartType>{};
2328

24-
unit.accept(FlowAnalysisVisitor(
29+
unit.accept(_FlowAnalysisVisitor(
2530
typeSystem,
2631
assignedVariables,
27-
{},
32+
promotedTypes,
2833
readBeforeWritten,
29-
[],
30-
[],
31-
[],
32-
[],
34+
nullableNodes,
35+
nonNullableNodes,
36+
unreachableNodes,
37+
functionBodiesThatDontComplete,
3338
));
3439

3540
return FlowAnalysisResult(
3641
readBeforeWritten,
42+
nullableNodes,
43+
nonNullableNodes,
44+
unreachableNodes,
45+
functionBodiesThatDontComplete,
46+
promotedTypes,
47+
);
48+
}
49+
50+
/// The result of performing flow analysis on a unit.
51+
class FlowAnalysisResult {
52+
final List<LocalVariableElement> readBeforeWritten;
53+
54+
/// The list of identifiers, resolved to a local variable or a parameter,
55+
/// where the variable is known to be nullable.
56+
final List<SimpleIdentifier> nullableNodes;
57+
58+
/// The list of identifiers, resolved to a local variable or a parameter,
59+
/// where the variable is known to be non-nullable.
60+
final List<SimpleIdentifier> nonNullableNodes;
61+
62+
/// The list of nodes, [Expression]s or [Statement]s, that cannot be reached,
63+
/// for example because a previous statement always exits.
64+
final List<AstNode> unreachableNodes;
65+
66+
/// The list of [FunctionBody]s that don't complete, for example because
67+
/// there is a `return` statement at the end of the function body block.
68+
final List<FunctionBody> functionBodiesThatDontComplete;
69+
70+
/// For each local variable or parameter, which type is promoted to a type
71+
/// specific than its declaration type, this map included references where
72+
/// the variable where it is read, and the type it has.
73+
final Map<SimpleIdentifier, DartType> promotedTypes;
74+
75+
FlowAnalysisResult(
76+
this.readBeforeWritten,
77+
this.nullableNodes,
78+
this.nonNullableNodes,
79+
this.unreachableNodes,
80+
this.functionBodiesThatDontComplete,
81+
this.promotedTypes,
3782
);
3883
}
3984

4085
/// The visitor that gathers local variables that are potentially assigned
4186
/// in corresponding statements, such as loops, `switch` and `try`.
42-
///
43-
/// TODO(scheglov) Change other tests to use [performFlowAnalysis],
44-
/// and make this class private.
45-
class AssignedVariablesVisitor extends RecursiveAstVisitor<void> {
87+
class _AssignedVariablesVisitor extends RecursiveAstVisitor<void> {
4688
final AssignedVariables assignedVariables;
4789

48-
AssignedVariablesVisitor(this.assignedVariables);
90+
_AssignedVariablesVisitor(this.assignedVariables);
4991

5092
@override
5193
void visitAssignmentExpression(AssignmentExpression node) {
@@ -135,34 +177,24 @@ class AssignedVariablesVisitor extends RecursiveAstVisitor<void> {
135177
}
136178
}
137179

138-
/// The result of performing flow analysis on a unit.
139-
class FlowAnalysisResult {
140-
final List<LocalVariableElement> readBeforeWritten;
141-
142-
FlowAnalysisResult(this.readBeforeWritten);
143-
}
144-
145180
/// [AstVisitor] that drives the [FlowAnalysis].
146-
///
147-
/// TODO(scheglov) Change other tests to use [performFlowAnalysis],
148-
/// and make this class private.
149-
class FlowAnalysisVisitor extends GeneralizingAstVisitor<void> {
181+
class _FlowAnalysisVisitor extends GeneralizingAstVisitor<void> {
150182
static final trueLiteral = astFactory.booleanLiteral(null, true);
151183

152184
final NodeOperations<Expression> nodeOperations;
153185
final TypeOperations<VariableElement, DartType> typeOperations;
154186
final AssignedVariables assignedVariables;
155187

156-
final Map<AstNode, DartType> promotedTypes;
188+
final Map<SimpleIdentifier, DartType> promotedTypes;
157189
final List<LocalVariableElement> readBeforeWritten;
158-
final List<AstNode> nullableNodes;
159-
final List<AstNode> nonNullableNodes;
190+
final List<SimpleIdentifier> nullableNodes;
191+
final List<SimpleIdentifier> nonNullableNodes;
160192
final List<AstNode> unreachableNodes;
161193
final List<FunctionBody> functionBodiesThatDontComplete;
162194

163195
FlowAnalysis<Statement, Expression, VariableElement, DartType> flow;
164196

165-
FlowAnalysisVisitor(
197+
_FlowAnalysisVisitor(
166198
TypeSystem typeSystem,
167199
this.assignedVariables,
168200
this.promotedTypes,

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

Lines changed: 19 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/ast/ast.dart';
6-
import 'package:analyzer/dart/element/element.dart';
7-
import 'package:analyzer/dart/element/type.dart';
8-
import 'package:analyzer/src/dart/resolver/flow_analysis.dart';
96
import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
107
import 'package:test/test.dart';
118
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -23,7 +20,7 @@ main() {
2320

2421
@reflectiveTest
2522
class DefiniteAssignmentFlowTest extends DriverResolutionTest {
26-
final List<LocalVariableElement> readBeforeWritten = [];
23+
FlowAnalysisResult flowResult;
2724

2825
/// Assert that only local variables with the given names are marked as read
2926
/// before being written. All the other local variables are implicitly
@@ -34,7 +31,7 @@ class DefiniteAssignmentFlowTest extends DriverResolutionTest {
3431
.where((i) => i != null)
3532
.map((name) => findElement.localVar(name))
3633
.toList();
37-
expect(readBeforeWritten, unorderedEquals(expected));
34+
expect(flowResult.readBeforeWritten, unorderedEquals(expected));
3835
}
3936

4037
test_assignment_leftExpression() async {
@@ -613,7 +610,7 @@ void f() {
613610
}
614611
''');
615612
var localV = findNode.simple('v; // 1').staticElement;
616-
expect(readBeforeWritten, unorderedEquals([localV]));
613+
expect(flowResult.readBeforeWritten, unorderedEquals([localV]));
617614
}
618615

619616
test_functionExpression_localFunction_local2() async {
@@ -1306,15 +1303,13 @@ void f() {
13061303
var unit = result.unit;
13071304
var typeSystem = result.typeSystem;
13081305

1309-
var flowAnalysisResult = performFlowAnalysis(typeSystem, unit);
1310-
readBeforeWritten.addAll(flowAnalysisResult.readBeforeWritten);
1306+
flowResult = performFlowAnalysis(typeSystem, unit);
13111307
}
13121308
}
13131309

13141310
@reflectiveTest
13151311
class NullableFlowTest extends DriverResolutionTest {
1316-
final List<AstNode> nullableNodes = [];
1317-
final List<AstNode> nonNullableNodes = [];
1312+
FlowAnalysisResult flowResult;
13181313

13191314
void assertNonNullable([
13201315
String search1,
@@ -1327,7 +1322,7 @@ class NullableFlowTest extends DriverResolutionTest {
13271322
.where((i) => i != null)
13281323
.map((search) => findNode.simple(search))
13291324
.toList();
1330-
expect(nonNullableNodes, unorderedEquals(expected));
1325+
expect(flowResult.nonNullableNodes, unorderedEquals(expected));
13311326
}
13321327

13331328
void assertNullable([
@@ -1341,7 +1336,7 @@ class NullableFlowTest extends DriverResolutionTest {
13411336
.where((i) => i != null)
13421337
.map((search) => findNode.simple(search))
13431338
.toList();
1344-
expect(nullableNodes, unorderedEquals(expected));
1339+
expect(flowResult.nullableNodes, unorderedEquals(expected));
13451340
}
13461341

13471342
test_assign_toNonNull() async {
@@ -1608,28 +1603,15 @@ void f(int x) {
16081603
await resolveTestFile();
16091604

16101605
var unit = result.unit;
1606+
var typeSystem = result.typeSystem;
16111607

1612-
var assignedVariables = AssignedVariables<Statement, VariableElement>();
1613-
unit.accept(AssignedVariablesVisitor(assignedVariables));
1614-
1615-
var typeSystem = unit.declaredElement.context.typeSystem;
1616-
unit.accept(FlowAnalysisVisitor(
1617-
typeSystem,
1618-
assignedVariables,
1619-
{},
1620-
[],
1621-
nullableNodes,
1622-
nonNullableNodes,
1623-
[],
1624-
[],
1625-
));
1608+
flowResult = performFlowAnalysis(typeSystem, unit);
16261609
}
16271610
}
16281611

16291612
@reflectiveTest
16301613
class ReachableFlowTest extends DriverResolutionTest {
1631-
final List<AstNode> unreachableNodes = [];
1632-
final List<FunctionBody> functionBodiesThatDontComplete = [];
1614+
FlowAnalysisResult flowResult;
16331615

16341616
test_conditional_false() async {
16351617
await trackCode(r'''
@@ -2055,21 +2037,9 @@ void f() { // f
20552037
await resolveTestFile();
20562038

20572039
var unit = result.unit;
2040+
var typeSystem = result.typeSystem;
20582041

2059-
var assignedVariables = AssignedVariables<Statement, VariableElement>();
2060-
unit.accept(AssignedVariablesVisitor(assignedVariables));
2061-
2062-
var typeSystem = unit.declaredElement.context.typeSystem;
2063-
unit.accept(FlowAnalysisVisitor(
2064-
typeSystem,
2065-
assignedVariables,
2066-
{},
2067-
[],
2068-
[],
2069-
[],
2070-
unreachableNodes,
2071-
functionBodiesThatDontComplete,
2072-
));
2042+
flowResult = performFlowAnalysis(typeSystem, unit);
20732043
}
20742044

20752045
void verify({
@@ -2086,11 +2056,11 @@ void f() { // f
20862056
);
20872057

20882058
expect(
2089-
this.unreachableNodes,
2059+
flowResult.unreachableNodes,
20902060
unorderedEquals(expectedUnreachableNodes),
20912061
);
20922062
expect(
2093-
this.functionBodiesThatDontComplete,
2063+
flowResult.functionBodiesThatDontComplete,
20942064
unorderedEquals(
20952065
functionBodiesThatDontComplete
20962066
.map((search) => findNode.functionBody(search))
@@ -2102,17 +2072,17 @@ void f() { // f
21022072

21032073
@reflectiveTest
21042074
class TypePromotionFlowTest extends DriverResolutionTest {
2105-
final Map<AstNode, DartType> promotedTypes = {};
2075+
FlowAnalysisResult flowResult;
21062076

21072077
void assertNotPromoted(String search) {
21082078
var node = findNode.simple(search);
2109-
var actualType = promotedTypes[node];
2079+
var actualType = flowResult.promotedTypes[node];
21102080
expect(actualType, isNull, reason: search);
21112081
}
21122082

21132083
void assertPromoted(String search, String expectedType) {
21142084
var node = findNode.simple(search);
2115-
var actualType = promotedTypes[node];
2085+
var actualType = flowResult.promotedTypes[node];
21162086
if (actualType == null) {
21172087
fail('$expectedType expected, but actually not promoted\n$search');
21182088
}
@@ -2888,20 +2858,8 @@ void f(bool b, Object x) {
28882858
await resolveTestFile();
28892859

28902860
var unit = result.unit;
2861+
var typeSystem = result.typeSystem;
28912862

2892-
var assignedVariables = AssignedVariables<Statement, VariableElement>();
2893-
unit.accept(AssignedVariablesVisitor(assignedVariables));
2894-
2895-
var typeSystem = unit.declaredElement.context.typeSystem;
2896-
unit.accept(FlowAnalysisVisitor(
2897-
typeSystem,
2898-
assignedVariables,
2899-
promotedTypes,
2900-
[],
2901-
[],
2902-
[],
2903-
[],
2904-
[],
2905-
));
2863+
flowResult = performFlowAnalysis(typeSystem, unit);
29062864
}
29072865
}

0 commit comments

Comments
 (0)