Files
kotlin-fork/compiler/testData/codegen/box/classes/kt496.kt
T
Alexander Udalov 69059362b8 Fir2Ir: generate 'finally' block always with Unit type
This is important for IR lowerings like PolymorphicSignatureLowering
which are very sensitive about the correct types of expressions and
placement of coercions to Unit (KT-59218).

A boolean parameter to `insertImplicitCasts` is not the best solution to
ensure that coercion to Unit is added. The best solution would be to fix
the TODO and generate coercion to the block's type for the last
statement. But that will affect many other places and will need to be
done separately => KT-59781.

Code in IrInterpreter is uncommented to fix the FIR test
`compiler/testData/ir/interpreter/exceptions/tryFinally.kt`; otherwise
evaluation of the function `returnTryFinally` there crashes with
"NoSuchElementException: ArrayDeque is empty". No idea why this test
didn't fail for K1 though, since the created IR is exactly the same.

For some unknown reason this breaks WASM backend with K2, but not with
K1 => KT-59800.
2023-07-18 11:37:41 +00:00

87 lines
1.3 KiB
Kotlin
Vendored

// IGNORE_BACKEND_K2: WASM
// ^ KT-59800 K2 + Wasm: test failure after changing `finally` block generation
fun test1() : Boolean {
try {
return true
} finally {
if(true) // otherwise we wisely have unreachable code
return false
}
}
var x = true
fun test2() : Boolean {
try {
} finally {
x = false;
}
return x
}
fun test3() : Int {
var y = 0
try {
++y
} finally {
++y
}
return y
}
var z = 0
fun test4() : Int {
z = 0
return try {
try {
z++
}
finally {
z++
}
} finally {
++z
}
}
fun test5() : Int {
var x = 0
while(true) {
try {
if(x < 10)
x++
else
break
}
finally {
x++
}
}
return x
}
fun test6() : Int {
var x = 0
while(x < 10) {
try {
x++
continue
}
finally {
x++
}
}
return x
}
fun box() : String {
if(test1()) return "test1 failed"
if(test2()) return "test2 failed"
if(test3() != 2) return "test3 failed"
if(test4() != 0) return "test4 failed"
if(test5() != 11) return "test5 failed"
if(test6() != 10) return "test6 failed"
return "OK"
}