[FIR2IR] Add Unit coercion for non-last expressions in try/catch blocks

^KT-61088 Fixed
This commit is contained in:
Sergej Jaskiewicz
2024-02-06 18:21:19 +01:00
committed by Space Team
parent 3d66511ca7
commit 88b80e0ed2
6 changed files with 27 additions and 3 deletions
@@ -175,6 +175,15 @@ class Fir2IrImplicitCastInserter(
override fun visitTryExpression(tryExpression: FirTryExpression, data: IrElement): IrElement {
val irTry = data as IrTry
(irTry.tryResult as? IrContainerExpression)?.let {
irTry.tryResult = it.insertImplicitCasts()
}
for (catch in irTry.catches) {
(catch.result as? IrContainerExpression)?.let {
catch.result = it.insertImplicitCasts()
}
}
(irTry.finallyExpression as? IrContainerExpression)?.let {
irTry.finallyExpression = it.insertImplicitCasts(coerceLastExpressionToUnit = true)
}
@@ -6,13 +6,18 @@ import java.lang.invoke.MethodHandles
import java.lang.invoke.MethodType
object O {
fun main() {}
var counter = 0
fun main() {
counter += 1
}
}
fun box(): String {
try {
val mh = MethodHandles.lookup().findVirtual(O::class.java, "main", MethodType.methodType(Void.TYPE))
mh.invokeExact(O)
mh.invokeExact(O)
} finally {}
if (O.counter != 2) return "Fail: counter == ${O.counter}"
return "OK"
}
@@ -1,6 +1,4 @@
// !LANGUAGE: +PolymorphicSignature
// IGNORE_BACKEND_K2: JVM_IR, JS_IR
// FIR status: Fail 9
// TARGET_BACKEND: JVM
// FULL_JDK
// SKIP_JDK6
@@ -18,13 +18,19 @@ FILE fqName:<root> fileName:/tryCatch.kt
TRY type=kotlin.Int
try: BLOCK type=kotlin.Int origin=null
CALL 'public final fun println (): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
CONST Int type=kotlin.Int value=100
CONST Int type=kotlin.Int value=42
CATCH parameter=val e: kotlin.Throwable declared in <root>.test2
VAR CATCH_PARAMETER name:e type:kotlin.Throwable [val]
BLOCK type=kotlin.Int origin=null
CALL 'public final fun println (): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
CONST Int type=kotlin.Int value=101
CONST Int type=kotlin.Int value=24
finally: BLOCK type=kotlin.Unit origin=null
CALL 'public final fun println (): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
CONST Int type=kotlin.Int value=102
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
CONST Int type=kotlin.Int value=555
+3
View File
@@ -17,14 +17,17 @@ fun test1() {
fun test2(): Int {
return try {
println()
100
42
}
catch (e: Throwable) {
println()
101
24
}
finally {
println()
102
555
}
}
@@ -16,14 +16,17 @@ fun test1() {
fun test2(): Int {
return try { // BLOCK
println()
100 /*~> Unit */
42
}
catch (e: Throwable){ // BLOCK
println()
101 /*~> Unit */
24
}
finally { // BLOCK
println()
102 /*~> Unit */
555 /*~> Unit */
}
}