Adjust flow analysis of `for each` statements for consistency with `for`.
The recent changes to `for` statements were necessary to ensure that
if a `for` statement causes type promotion due to an unreachable
branch without type promotion joining a reachable one with type
promotion, the type promotion would still happen properly even if the
top of the `for` statement was itself unreachable. There is no need
for a similar change to `for each` statements, because they are simple
enough that this situation cannot arise. But it's still worth making
the corresponding change for consistency.
Bug: https://github.com/dart-lang/sdk/issues/40009
Change-Id: Idabf7745f04a99152e688bf050a8d80e07b004e2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166520
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
diff --git a/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart b/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart
index 057d308..de9e6cc 100644
--- a/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart
+++ b/pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart
@@ -2908,7 +2908,7 @@
void forEach_bodyBegin(Node node, Variable loopVariable, Type writtenType) {
AssignedVariablesNodeInfo<Variable> info =
_assignedVariables._getInfoForNode(node);
- _current = _current.conservativeJoin(info._written, info._captured);
+ _current = _current.conservativeJoin(info._written, info._captured).split();
_SimpleStatementContext<Variable, Type> context =
new _SimpleStatementContext<Variable, Type>(
_current.reachable.parent, _current);
@@ -2922,7 +2922,7 @@
void forEach_end() {
_SimpleStatementContext<Variable, Type> context =
_stack.removeLast() as _SimpleStatementContext<Variable, Type>;
- _current = _join(_current, context._previous);
+ _current = _merge(_current, context._previous);
}
@override