Skip to content

Commit a1fc2ea

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Make variables assigned in a loop not definitely unassigned anywhere in the loop.
Bug: #42301 Change-Id: I75962b976427cef7f40025f1dcd9f27400306825 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151047 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 28231a7 commit a1fc2ea

File tree

5 files changed

+90
-18
lines changed

5 files changed

+90
-18
lines changed

pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,7 @@ class FlowModel<Variable, Type> {
12701270
Iterable<Variable> writtenVariables,
12711271
Iterable<Variable> capturedVariables) {
12721272
Map<Variable, VariableModel<Variable, Type>> newVariableInfo;
1273+
12731274
for (Variable variable in writtenVariables) {
12741275
VariableModel<Variable, Type> info = infoFor(variable);
12751276
if (info.promotedTypes != null) {
@@ -1278,6 +1279,7 @@ class FlowModel<Variable, Type> {
12781279
variableInfo))[variable] = info.discardPromotions();
12791280
}
12801281
}
1282+
12811283
for (Variable variable in capturedVariables) {
12821284
VariableModel<Variable, Type> info = variableInfo[variable];
12831285
if (info == null) {
@@ -1291,8 +1293,14 @@ class FlowModel<Variable, Type> {
12911293
variableInfo))[variable] = info.writeCapture();
12921294
}
12931295
}
1294-
if (newVariableInfo == null) return this;
1295-
return new FlowModel<Variable, Type>._(reachable, newVariableInfo);
1296+
1297+
FlowModel<Variable, Type> result = newVariableInfo == null
1298+
? this
1299+
: new FlowModel<Variable, Type>._(reachable, newVariableInfo);
1300+
1301+
result = result.joinUnassigned(written: writtenVariables);
1302+
1303+
return result;
12961304
}
12971305

12981306
/// Updates the state to reflect a control path that is known to have
@@ -2433,16 +2441,13 @@ class _FlowAnalysisImpl<Node, Statement extends Node, Expression, Variable,
24332441

24342442
@override
24352443
void for_end() {
2436-
FlowModel<Variable, Type> afterUpdate = _current;
2437-
24382444
_WhileContext<Variable, Type> context =
24392445
_stack.removeLast() as _WhileContext<Variable, Type>;
24402446
// Tail of the stack: falseCondition, break
24412447
FlowModel<Variable, Type> breakState = context._breakModel;
24422448
FlowModel<Variable, Type> falseCondition = context._conditionInfo.ifFalse;
24432449

24442450
_current = _join(falseCondition, breakState);
2445-
_current = _current.joinUnassigned(other: afterUpdate);
24462451
}
24472452

24482453
@override
@@ -2481,9 +2486,6 @@ class _FlowAnalysisImpl<Node, Statement extends Node, Expression, Variable,
24812486
_stack.add(new _SimpleContext(_current));
24822487
_current = _current.removePromotedAll(_assignedVariables._anywhere._written,
24832488
_assignedVariables._anywhere._captured);
2484-
_current = _current.joinUnassigned(
2485-
written: _assignedVariables._anywhere._written,
2486-
);
24872489
}
24882490

24892491
@override
@@ -2752,9 +2754,8 @@ class _FlowAnalysisImpl<Node, Statement extends Node, Expression, Variable,
27522754

27532755
AssignedVariablesNodeInfo<Variable> info =
27542756
_assignedVariables._getInfoForNode(body);
2755-
FlowModel<Variable, Type> beforeCatch = beforeBody
2756-
.removePromotedAll(info._written, info._captured)
2757-
.joinUnassigned(other: afterBody);
2757+
FlowModel<Variable, Type> beforeCatch =
2758+
beforeBody.removePromotedAll(info._written, info._captured);
27582759

27592760
context._beforeCatch = beforeCatch;
27602761
context._afterBodyAndCatches = afterBody;
@@ -2843,9 +2844,7 @@ class _FlowAnalysisImpl<Node, Statement extends Node, Expression, Variable,
28432844
void whileStatement_end() {
28442845
_WhileContext<Variable, Type> context =
28452846
_stack.removeLast() as _WhileContext<Variable, Type>;
2846-
FlowModel<Variable, Type> afterBody = _current;
28472847
_current = _join(context._conditionInfo.ifFalse, context._breakModel);
2848-
_current = _current.joinUnassigned(other: afterBody);
28492848
}
28502849

28512850
@override

pkg/_fe_analyzer_shared/test/flow_analysis/definite_unassignment/data/do.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ breakOuterFromInner(bool c) {
4040
condition() {
4141
late int v1, v2;
4242
do {
43-
/*unassigned*/ v1; // assigned in the condition, but not yet
43+
v1;
4444
} while ((v1 = 0) + (v2 = 0) >= 0);
4545
v2;
4646
}

pkg/_fe_analyzer_shared/test/flow_analysis/definite_unassignment/data/for.dart

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,56 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
for_assignedInBody_body(bool b) {
6+
late int v;
7+
for (;;) {
8+
if (b) {
9+
v = 0;
10+
} else {
11+
v;
12+
}
13+
v;
14+
}
15+
v;
16+
}
17+
18+
for_assignedInBody_condition() {
19+
bool firstTime = true;
20+
late int v;
21+
for (; firstTime || v > 0;) {
22+
firstTime = false;
23+
v = 5;
24+
}
25+
v;
26+
}
27+
28+
for_assignedInBody_initializer() {
29+
bool firstTime = true;
30+
late int v;
31+
for (var x = /*unassigned*/ v;;) {
32+
v = 5;
33+
}
34+
v;
35+
}
36+
37+
for_assignedInCondition() {
38+
bool firstTime = true;
39+
late int v;
40+
for (var x = /*unassigned*/ v; (v = 0) > 0;) {
41+
v;
42+
}
43+
v;
44+
}
45+
46+
for_assignedInUpdater() {
47+
bool firstTime = true;
48+
late int v;
49+
for (var x = /*unassigned*/ v;; v = 0) {
50+
v;
51+
}
52+
v;
53+
}
54+
555
for_body(bool c) {
656
late int v;
757
for (; c;) {
@@ -77,7 +127,7 @@ for_initializer_variable() {
77127
for_updaters(bool c) {
78128
late int v1, v2, v3, v4;
79129
for (; c; v1 = 0, v2 = 0, v3 = 0, /*unassigned*/ v4) {
80-
/*unassigned*/ v1;
130+
v1;
81131
}
82132
v2;
83133
}
@@ -115,7 +165,7 @@ collection_for_initializer_variable() {
115165

116166
collection_for_updaters(bool c) {
117167
late int v1, v2, v3, v4;
118-
[for (; c; v1 = 0, v2 = 0, v3 = 0, /*unassigned*/ v4) /*unassigned*/ v1 ];
168+
[for (; c; v1 = 0, v2 = 0, v3 = 0, /*unassigned*/ v4) v1];
119169
v2;
120170
}
121171

pkg/_fe_analyzer_shared/test/flow_analysis/definite_unassignment/data/issue41284.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void method1() {
88
local = 0;
99
return;
1010
} finally {
11-
print(/*unassigned*/ local);
11+
print(local);
1212
}
1313
local;
1414
}
@@ -22,7 +22,7 @@ void method2() {
2222
local = 42;
2323
rethrow;
2424
} finally {
25-
print(/*unassigned*/ local);
25+
print(local);
2626
}
2727
local;
2828
}

pkg/_fe_analyzer_shared/test/flow_analysis/definite_unassignment/data/while.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
assignedInBody_body(bool b) {
6+
late int v;
7+
while (true) {
8+
if (b) {
9+
v = 0;
10+
} else {
11+
v;
12+
}
13+
v;
14+
}
15+
v;
16+
}
17+
18+
assignedInBody_condition() {
19+
bool firstTime = true;
20+
late int v;
21+
while (firstTime || v > 0) {
22+
firstTime = false;
23+
v = 5;
24+
}
25+
v;
26+
}
27+
528
condition() {
629
late int v;
730
while ((v = 0) >= 0) {

0 commit comments

Comments
 (0)