Fix continue in for loops. Tests added
This commit is contained in:
@@ -439,6 +439,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
private void generateForLoop(AbstractForLoopGenerator generator) {
|
||||
Label loopExit = new Label();
|
||||
Label loopEntry = new Label();
|
||||
Label continueLabel = new Label();
|
||||
|
||||
generator.beforeLoop();
|
||||
|
||||
@@ -446,9 +447,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
generator.conditionAndJump(loopExit);
|
||||
|
||||
generator.beforeBody();
|
||||
blockStackElements.push(new LoopBlockStackElement(loopExit, loopEntry, null));
|
||||
blockStackElements.push(new LoopBlockStackElement(loopExit, continueLabel, targetLabel(generator.forExpression)));
|
||||
generator.body();
|
||||
blockStackElements.pop();
|
||||
v.mark(continueLabel);
|
||||
generator.afterBody();
|
||||
|
||||
v.goTo(loopEntry);
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
fun for_int_range(): Int {
|
||||
var c = 0
|
||||
@loop for (i in 1..10) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun for_byte_range(): Int {
|
||||
var c = 0
|
||||
val from: Byte = 1
|
||||
val to: Byte = 10
|
||||
@loop for (i in from..to) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun for_long_range(): Int {
|
||||
var c = 0
|
||||
val from: Long = 1
|
||||
val to: Long = 10
|
||||
@loop for (i in from..to) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun for_int_list(): Int {
|
||||
val a = java.util.ArrayList<Int>()
|
||||
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
|
||||
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
|
||||
var c = 0
|
||||
@loop for (i in a) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun for_byte_list(): Int {
|
||||
val a = java.util.ArrayList<Byte>()
|
||||
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
|
||||
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
|
||||
var c = 0
|
||||
@loop for (i in a) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun for_long_list(): Int {
|
||||
val a = java.util.ArrayList<Long>()
|
||||
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
|
||||
a.add(0); a.add(0); a.add(0); a.add(0); a.add(0)
|
||||
var c = 0
|
||||
@loop for (i in a) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun for_double_list(): Int {
|
||||
val a = java.util.ArrayList<Double>()
|
||||
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
|
||||
a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0); a.add(0.0)
|
||||
var c = 0
|
||||
@loop for (i in a) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun for_str_array(): Int {
|
||||
val a = Array<String>(10) {i -> "$i"}
|
||||
var c = 0
|
||||
@loop for (i in a) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun for_intarray(): Int {
|
||||
val a = IntArray(10)
|
||||
var c = 0
|
||||
@loop for (i in a) {
|
||||
if (c >= 5) continue @loop
|
||||
c++
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if (for_int_range() != 5) return "fail 1"
|
||||
if (for_byte_range() != 5) return "fail 2"
|
||||
if (for_long_range() != 5) return "fail 3"
|
||||
if (for_intarray() != 5) return "fail 4"
|
||||
if (for_str_array() != 5) return "fail 5"
|
||||
if (for_int_list() != 5) return "fail 6"
|
||||
if (for_byte_list() != 5) return "fail 7"
|
||||
if (for_long_list() != 5) return "fail 8"
|
||||
if (for_double_list() != 5) return "fail 9"
|
||||
return "OK"
|
||||
}
|
||||
@@ -344,6 +344,11 @@ public class ControlStructuresTest extends CodegenTestCase {
|
||||
blackBoxFile("regressions/kt998.kt");
|
||||
}
|
||||
|
||||
public void testContinueInFor() throws Exception {
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_AND_ANNOTATIONS);
|
||||
blackBoxFile("controlStructures/continueInFor.kt");
|
||||
}
|
||||
|
||||
public void testKt628() throws Exception {
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
blackBoxFile("regressions/kt628.kt");
|
||||
|
||||
Reference in New Issue
Block a user