Fix range-based 'for' loop with 'continue' in range bounds

1. Search for increment function in range element type, not in inferred
induction variable type
(which can be inappropriate, e.g., 'Nothing' in case of 'continue').

2. Handle nested loops with shared exit labels
(generated by JVM_IR for KT-37370 case).

KT-37370 KT-37373
This commit is contained in:
Dmitry Petrov
2020-03-16 11:40:35 +03:00
parent d5b65abc5d
commit 6809f4439c
14 changed files with 188 additions and 19 deletions
+65
View File
@@ -0,0 +1,65 @@
// WITH_RUNTIME
fun testContinue1() {
for (i in 0..1) {
for (j in continue downTo 1) {}
}
}
fun testContinue2() {
for (i in 0..1) {
for (j in 1 downTo continue) {}
}
}
fun falseCond() = false
fun testContinue3() {
for (i in 0..1) {
for (j in (if (falseCond()) 10 else continue) downTo 1) {}
}
}
fun testContinue4() {
for (i in 0..1) {
for (j in 10 downTo (if (falseCond()) 1 else continue)) {}
}
}
fun testContinue5() {
for (i in 0..1) {
for (j in (
if (try {
if (falseCond()) true else return
} finally {
if (!falseCond()) continue
}
)
10
else
continue
)
downTo 1) {
//...
}
}
}
fun start(i1: Int, i2: Int, i3: Int) = 10
fun testContinue6() {
for (i in 0..1) {
for (j in start(1, 2, continue) downTo 0) {
}
}
}
fun box(): String {
testContinue1()
testContinue2()
testContinue3()
testContinue4()
testContinue5()
testContinue6()
return "OK"
}
+28
View File
@@ -0,0 +1,28 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
import kotlin.test.*
fun testContinue7() {
var x = 0
fun inc() = ++x
for (i in 0..1) {
for (j in inc() downTo continue) {}
}
assertEquals(2, x)
}
fun testContinue8() {
var x = 0
fun inc() = ++x
for (i in 0..1) {
for (j in continue downTo inc()) {}
}
assertEquals(0, x)
}
fun box(): String {
testContinue7()
testContinue8()
return "OK"
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
@@ -1,8 +1,5 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
// IGNORE_BACKEND: JVM_IR
// ^ TODO KT-37373
fun testContinue() {
for (i in 0..1) {