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 7b7ba883187..e5769228de0 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 @@ -322,18 +322,14 @@ class ExpressionCodegen( } } + private val IrVariable.isVisibleInLVT: Boolean + get() = origin != IrDeclarationOrigin.IR_TEMPORARY_VARIABLE && + origin != IrDeclarationOrigin.FOR_LOOP_ITERATOR + private fun writeLocalVariablesInTable(info: BlockInfo, endLabel: Label) { info.variables.forEach { - when (it.declaration.origin) { - IrDeclarationOrigin.IR_TEMPORARY_VARIABLE, - IrDeclarationOrigin.FOR_LOOP_ITERATOR -> { - // Ignore implicitly created variables - } - else -> { - mv.visitLocalVariable( - it.declaration.name.asString(), it.type.descriptor, null, it.startLabel, endLabel, it.index - ) - } + if (it.declaration.isVisibleInLVT) { + mv.visitLocalVariable(it.declaration.name.asString(), it.type.descriptor, null, it.startLabel, endLabel, it.index) } } @@ -485,9 +481,13 @@ class ExpressionCodegen( declaration.markLineNumber(startOffset = true) - declaration.initializer?.let { - it.accept(this, data).coerce(varType, declaration.type).materialize() - it.markLineNumber(startOffset = true) + val initializer = declaration.initializer + if (initializer != null) { + initializer.accept(this, data).coerce(varType, declaration.type).materialize() + initializer.markLineNumber(startOffset = true) + mv.store(index, varType) + } else if (declaration.isVisibleInLVT) { + pushDefaultValueOnStack(varType, mv) mv.store(index, varType) } diff --git a/compiler/testData/codegen/box/when/sealedWhenInitialization.kt b/compiler/testData/codegen/box/when/sealedWhenInitialization.kt index 59ef24d3984..5f9db0bde7d 100644 --- a/compiler/testData/codegen/box/when/sealedWhenInitialization.kt +++ b/compiler/testData/codegen/box/when/sealedWhenInitialization.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR sealed class A { object B : A() diff --git a/compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTableSameSort.kt b/compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTableSameSort.kt index 605f7a4e54b..6f3cd404d79 100644 --- a/compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTableSameSort.kt +++ b/compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTableSameSort.kt @@ -1,6 +1,3 @@ -// This test checks, that different variables occupy the same slot -// In JVM_IR, however, loop variable's lifetime goes beyond the loop itself, thus the test has no sense in JVM_IR -// IGNORE_BACKEND: JVM_IR // WITH_COROUTINES import helpers.* diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/boxing.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/boxing.kt index 0371c0b9117..5bc1fc26f92 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/boxing.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/boxing.kt @@ -1,6 +1,5 @@ // IGNORE_BACKEND: JVM_IR // TODO KT-36648 Captured variables not optimized in JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR fun test(): java.lang.Integer { val c: java.lang.Integer diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/contract.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/contract.kt index 87990ea3826..a4dda832612 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/contract.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/contract.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -22,4 +20,4 @@ fun doIt(block: () -> Unit) { } // 0 ISTORE 0 -// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; L1 L3 0 \ No newline at end of file +// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/contractVar.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/contractVar.kt index 6c6a7500409..c5f223540e3 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/contractVar.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/contractVar.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -22,8 +20,8 @@ fun doIt(block: () -> Unit) { } // 0 ISTORE 0 -// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; L1 L3 0 +// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; // JVM_IR_TEMPLATES // 0 ISTORE 0 -// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; L1 L4 0 \ No newline at end of file +// 1 LOCALVARIABLE c Lkotlin/jvm/internal/Ref\$CharRef; \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatement.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatement.kt index fb1b7c36f3b..8cded13bc65 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatement.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatement.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR - import kotlin.random.Random fun test(): Char { @@ -14,4 +11,4 @@ fun test(): Char { } // 3 ISTORE 0 -// 1 LOCALVARIABLE c C L1 L7 0 +// 1 LOCALVARIABLE c C diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementVar.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementVar.kt index bbc27b6e4e1..a56b3d27e24 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementVar.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementVar.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR - import kotlin.random.Random fun test(): Char { @@ -14,4 +11,4 @@ fun test(): Char { } // 3 ISTORE 0 -// 1 LOCALVARIABLE c C L1 L7 0 +// 1 LOCALVARIABLE c C diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementWithoutBlock.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementWithoutBlock.kt index 51957ea1223..dba5f713e37 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementWithoutBlock.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementWithoutBlock.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR - import kotlin.random.Random fun test(): Char { diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementWithoutBlockVar.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementWithoutBlockVar.kt index 743d70e8e25..1ed398bab91 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementWithoutBlockVar.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/ifStatementWithoutBlockVar.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR - import kotlin.random.Random fun test(): Char { diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/inlineClass.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/inlineClass.kt index 458db7b57ae..6243a6fff2a 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/inlineClass.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/inlineClass.kt @@ -1,6 +1,5 @@ // IGNORE_BACKEND: JVM_IR // TODO KT-36648 Captured variables not optimized in JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR fun test(): UInt { val c: UInt diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/inlineClassVar.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/inlineClassVar.kt index f7b3f124673..7d48d568811 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/inlineClassVar.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/inlineClassVar.kt @@ -1,6 +1,5 @@ // IGNORE_BACKEND: JVM_IR // TODO KT-36648 Captured variables not optimized in JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR fun test(): UInt { var c: UInt diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/lateinit.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/lateinit.kt index fa2f7761854..2be1fc776bc 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/lateinit.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/lateinit.kt @@ -1,6 +1,5 @@ // IGNORE_BACKEND: JVM_IR // TODO KT-36648 Captured variables not optimized in JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR fun test(): Char { lateinit var c: Any diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/run.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/run.kt index a2f326930d6..c2e121915d2 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/run.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/run.kt @@ -1,6 +1,5 @@ // IGNORE_BACKEND: JVM_IR // TODO KT-36648 Captured variables not optimized in JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR fun test(): Char { val c: Char diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/singleBlock.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/singleBlock.kt index 51d0a942ecd..6ce0f2ce9db 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/singleBlock.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/singleBlock.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR - fun test(): Char { val c: Char val l = Any() diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/singleBlockVar.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/singleBlockVar.kt index f8cb104d580..f5491372ee9 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/singleBlockVar.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/singleBlockVar.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR - fun test(): Char { var c: Char val l = Any() diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/whenStatement.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/whenStatement.kt index 0594dc127c9..38b38924310 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/whenStatement.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/whenStatement.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR - fun test(i: Int): Char { val c: Char when (i) { @@ -20,4 +17,4 @@ fun test(i: Int): Char { } // 12 ISTORE 1 -// 1 LOCALVARIABLE c C L1 L16 1 +// 1 LOCALVARIABLE c C diff --git a/compiler/testData/codegen/bytecodeText/localInitializationLVT/whenStatementVar.kt b/compiler/testData/codegen/bytecodeText/localInitializationLVT/whenStatementVar.kt index 60e002b106c..2a34e476424 100644 --- a/compiler/testData/codegen/bytecodeText/localInitializationLVT/whenStatementVar.kt +++ b/compiler/testData/codegen/bytecodeText/localInitializationLVT/whenStatementVar.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36812 Generate proper lifetime intervals for local variables in JVM_IR - fun test(i: Int): Char { var c: Char when (i) { @@ -20,4 +17,4 @@ fun test(i: Int): Char { } // 12 ISTORE 1 -// 1 LOCALVARIABLE c C L1 L16 1 +// 1 LOCALVARIABLE c C