Skip to content

Commit 97abf9e

Browse files
committed
Merge pull request JetBrains#108 from udalov/kt2423
KT-2423 Jump after condition check in inner loop is to the wrong label
2 parents f9b0223 + 6eb4cf5 commit 97abf9e

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,16 @@ public StackValue visitWhileExpression(JetWhileExpression expression, StackValue
285285
Label condition = new Label();
286286
v.mark(condition);
287287

288-
Label end = continueLabel != null ? continueLabel : new Label();
288+
Label end = new Label();
289289
blockStackElements.push(new LoopBlockStackElement(end, condition, targetLabel(expression)));
290290

291-
Label savedContinueLabel = continueLabel;
292-
continueLabel = condition;
293-
294291
final StackValue conditionValue = gen(expression.getCondition());
295292
conditionValue.condJump(end, true, v);
296293

297294
gen(expression.getBody(), Type.VOID_TYPE);
298295
v.goTo(condition);
299296

300-
continueLabel = savedContinueLabel;
301-
if (end != continueLabel)
302-
v.mark(end);
297+
v.mark(end);
303298

304299
blockStackElements.pop();
305300

@@ -683,14 +678,13 @@ else if (stackElement instanceof LoopBlockStackElement) {
683678
}
684679

685680
private StackValue generateSingleBranchIf(StackValue condition, JetExpression expression, boolean inverse) {
686-
Label end = continueLabel != null ? continueLabel : new Label();
681+
Label end = new Label();
687682

688683
condition.condJump(end, inverse, v);
689684

690685
gen(expression, Type.VOID_TYPE);
691686

692-
if (continueLabel != end)
693-
v.mark(end);
687+
v.mark(end);
694688
return StackValue.none();
695689
}
696690

@@ -849,8 +843,6 @@ public StackValue visitObjectLiteralExpression(JetObjectLiteralExpression expres
849843
return StackValue.onStack(closure.getClassname().getAsmType());
850844
}
851845

852-
private Label continueLabel;
853-
854846
private StackValue generateBlock(List<JetElement> statements) {
855847
Label blockStart = new Label();
856848
v.mark(blockStart);
@@ -880,12 +872,9 @@ private StackValue generateBlock(List<JetElement> statements) {
880872
}
881873

882874
StackValue answer = StackValue.none();
883-
Label savedContinueLabel = continueLabel;
884-
continueLabel = null;
885875
for (int i = 0, statementsSize = statements.size(); i < statementsSize; i++) {
886876
JetElement statement = statements.get(i);
887877
if (i == statements.size() - 1 /*&& statement instanceof JetExpression && !bindingContext.get(BindingContext.STATEMENT, statement)*/) {
888-
continueLabel = savedContinueLabel;
889878
answer = gen(statement);
890879
}
891880
else {
@@ -2709,9 +2698,6 @@ public StackValue visitTryExpression(JetTryExpression expression, StackValue rec
27092698
The "returned" value of try expression with no finally is either the last expression in the try block or the last expression in the catch block
27102699
(or blocks).
27112700
*/
2712-
Label savedContinueLabel = continueLabel;
2713-
continueLabel = null;
2714-
27152701
JetFinallySection finallyBlock = expression.getFinallyBlock();
27162702
if (finallyBlock != null) {
27172703
blockStackElements.push(new FinallyBlockStackElement(expression));
@@ -2771,8 +2757,6 @@ public StackValue visitTryExpression(JetTryExpression expression, StackValue rec
27712757
blockStackElements.pop();
27722758
}
27732759

2774-
continueLabel = savedContinueLabel;
2775-
27762760
return StackValue.onStack(expectedAsmType);
27772761
}
27782762

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
fun ok1(): Boolean {
2+
val queue = linkedList(1, 2, 3)
3+
while (!queue.isEmpty()) {
4+
queue.poll()
5+
for (y in 1..3) {
6+
if (queue.contains(y)) {
7+
return true
8+
}
9+
}
10+
}
11+
return false
12+
}
13+
14+
fun ok2(): Boolean {
15+
val queue = linkedList(1, 2, 3)
16+
val array = array(1, 2, 3)
17+
while (!queue.isEmpty()) {
18+
queue.poll()
19+
for (y in array) {
20+
if (queue.contains(y)) {
21+
return true
22+
}
23+
}
24+
}
25+
return false
26+
}
27+
28+
fun ok3(): Boolean {
29+
val queue = linkedList(1, 2, 3)
30+
while (!queue.isEmpty()) {
31+
queue.poll()
32+
var x = 0
33+
do {
34+
x++
35+
if (x == 2) return true
36+
} while (x < 2)
37+
}
38+
return false
39+
}
40+
41+
fun box(): String {
42+
if (!ok1()) return "Fail #1"
43+
if (!ok2()) return "Fail #2"
44+
if (!ok3()) return "Fail #3"
45+
return "OK"
46+
}

compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,9 @@ public void testKt1688() {
361361
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
362362
blackBoxFile("regressions/kt1688.kt");
363363
}
364+
365+
public void testKt2423() {
366+
createEnvironmentWithFullJdk();
367+
blackBoxFile("regressions/kt2423.kt");
368+
}
364369
}

0 commit comments

Comments
 (0)