From bb0cd92da0f79a523339e1756bdc0cb82a4ea483 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Mon, 28 Jan 2019 13:34:51 +0100 Subject: [PATCH] JVM_IR: Fix codegeneration for missing branches. This used to generate an ACONST_NULL instruction leading to incorrect stack height for code such as: ``` if (condition) else 32 ``` --- .../jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt | 4 ++++ .../codegen/bytecodeText/boxingOptimization/maxMinBy.kt | 1 - compiler/testData/lineNumber/if.kt | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index 2ec7a4bff1c..9301bef8889 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -33,6 +33,7 @@ import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.symbols.IrSymbol +import org.jetbrains.kotlin.ir.types.isNothing import org.jetbrains.kotlin.ir.types.toKotlinType import org.jetbrains.kotlin.ir.util.dump import org.jetbrains.kotlin.ir.util.isNullConst @@ -240,6 +241,9 @@ class ExpressionCodegen( } override fun visitContainerExpression(expression: IrContainerExpression, data: BlockInfo): StackValue { + // Empty blocks with nothing type should not generate a value on the stack. They + // arise for if statements with missing branches such as: if (expr) else 42 + if (expression.statements.size == 0 && expression.type.isNothing()) return none() val result = expression.statements.fold(none()) { _, exp -> //coerceNotToUnit(r.type, Type.VOID_TYPE) exp.accept(this, data) diff --git a/compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt b/compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt index 3340e899fb4..bf55f7f6d84 100644 --- a/compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt +++ b/compiler/testData/codegen/bytecodeText/boxingOptimization/maxMinBy.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // FILE: list.kt val intList = listOf(1, 2, 3) diff --git a/compiler/testData/lineNumber/if.kt b/compiler/testData/lineNumber/if.kt index 67105f6fb7f..ffa49a338fd 100644 --- a/compiler/testData/lineNumber/if.kt +++ b/compiler/testData/lineNumber/if.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR fun foo() { if (test.lineNumber() > 0) { test.lineNumber()