Skip to content

Commit d621ed9

Browse files
natebiggsCommit Queue
authored andcommitted
[ddc] Fix handling of continues in switch statements with labeled continues.
Tested with test case outlined in bug below. This should be tested via Lasse's tests when he lands them. For slightly more context we compile code like this: do { switch (y) { case 1: continue L1; L1: case 2: continue; } } while (x); into something like this: do { while (true) { var labelState = y; switch (labelState) { case 1: labelState = 2; case 2: continue; // <-- This now only continues the while (true). } } } while (x); Bug: #59593 Change-Id: I9b343fd918e48cb44b65e90eab39d70fca4758d4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397105 Reviewed-by: Nicholas Shahan <[email protected]> Commit-Queue: Nate Biggs <[email protected]>
1 parent 81a7acc commit d621ed9

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4622,11 +4622,18 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
46224622
var labelName = 'SL${_switchLabelStates.length}';
46234623
_switchLabelStates[node] = _SwitchLabelState(labelName, labelState);
46244624

4625+
// Since we wrap the switch in a 'while (true)' loop the continue targets
4626+
// within the switch will no longer target the correct loop so we need
4627+
// explicit breaks.
4628+
final savedCurrentContinueTargets = _currentContinueTargets;
4629+
_currentContinueTargets = [];
4630+
46254631
for (var c in node.cases) {
46264632
var subcases =
46274633
_visitSwitchCase(c, lastSwitchCase: c == node.cases.last);
46284634
if (subcases.isNotEmpty) cases.addAll(subcases);
46294635
}
4636+
_currentContinueTargets = savedCurrentContinueTargets;
46304637

46314638
var switchExpr = _visitExpression(node.expression);
46324639
var switchStmt = js_ast.Switch(labelState, cases);

pkg/dev_compiler/lib/src/kernel/compiler_new.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4919,11 +4919,18 @@ class LibraryCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
49194919
var labelName = 'SL${_switchLabelStates.length}';
49204920
_switchLabelStates[node] = _SwitchLabelState(labelName, labelState);
49214921

4922+
// Since we wrap the switch in a 'while (true)' loop the continue targets
4923+
// within the switch will no longer target the correct loop so we need
4924+
// explicit breaks.
4925+
final savedCurrentContinueTargets = _currentContinueTargets;
4926+
_currentContinueTargets = [];
4927+
49224928
for (var c in node.cases) {
49234929
var subcases =
49244930
_visitSwitchCase(c, lastSwitchCase: c == node.cases.last);
49254931
if (subcases.isNotEmpty) cases.addAll(subcases);
49264932
}
4933+
_currentContinueTargets = savedCurrentContinueTargets;
49274934

49284935
var switchExpr = _visitExpression(node.expression);
49294936
var switchStmt = js_ast.Switch(labelState, cases);

0 commit comments

Comments
 (0)