diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformerMethodVisitor.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformerMethodVisitor.kt index 64e81092a28..e1c8428bab8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformerMethodVisitor.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformerMethodVisitor.kt @@ -201,20 +201,38 @@ class CoroutineTransformerMethodVisitor( // When suspension point is inlined, it is in range of fake inliner variables. // Path from TABLESWITCH into unspilling goes to latter part of the range. - // In this case the variables are uninitialized, initialize them + // In this case the variables are uninitialized, initialize them, and split the local variable + // range so that the local variable is only defined when initialized. private fun initializeFakeInlinerVariables(methodNode: MethodNode, stateLabels: List) { for (stateLabel in stateLabels) { + val newRecords = mutableListOf() for (record in methodNode.localVariables) { if (isFakeLocalVariableForInline(record.name) && methodNode.instructions.indexOf(record.start) < methodNode.instructions.indexOf(stateLabel) && methodNode.instructions.indexOf(stateLabel) < methodNode.instructions.indexOf(record.end) ) { + val newEnd = record.end + val newStart = LabelNode() + record.end = stateLabel methodNode.instructions.insert(stateLabel, withInstructionAdapter { iconst(0) store(record.index, Type.INT_TYPE) + }.also { + it.add(newStart) }) + newRecords.add( + LocalVariableNode( + record.name, + record.desc, + record.signature, + newStart, + newEnd, + record.index + ) + ) } } + methodNode.localVariables.addAll(newRecords) } }