diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineCodegen.kt index 7fd615c4af0..a4ccd405c02 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineCodegen.kt @@ -62,7 +62,7 @@ abstract class AbstractCoroutineCodegen( if (languageVersionSettings.isReleaseCoroutines()) createImplMethod( INVOKE_SUSPEND_METHOD_NAME, - "\$result" to classDescriptor.module.getResult(classDescriptor.builtIns.anyType) + SUSPEND_CALL_RESULT_NAME to classDescriptor.module.getResult(classDescriptor.builtIns.anyType) ) else createImplMethod( 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 a8de636f806..7f77c2a85db 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformerMethodVisitor.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformerMethodVisitor.kt @@ -47,6 +47,7 @@ private const val COROUTINES_METADATA_CLASS_NAME_JVM_NAME = "c" private const val COROUTINES_METADATA_VERSION_JVM_NAME = "v" const val SUSPEND_FUNCTION_CONTINUATION_PARAMETER = "\$completion" +const val SUSPEND_CALL_RESULT_NAME = "\$result" class CoroutineTransformerMethodVisitor( delegate: MethodVisitor, @@ -315,18 +316,28 @@ class CoroutineTransformerMethodVisitor( // Warning! This is _continuation_, not _completion_, it can be allocated inside the method, thus, it is incorrect to treat it // as a parameter - private fun addContinuationToLvt(methodNode: MethodNode, startLabel: LabelNode) { - val endLabel = LabelNode() - methodNode.instructions.insert(methodNode.instructions.last, endLabel) - methodNode.localVariables.add( - LocalVariableNode( - CONTINUATION_VARIABLE_NAME, - languageVersionSettings.continuationAsmType().descriptor, - null, - startLabel, - endLabel, - continuationIndex - ) + private fun addContinuationAndResultToLvt( + methodNode: MethodNode, + startLabel: Label, + resultStartLabel: Label + ) { + val endLabel = Label() + methodNode.instructions.add(withInstructionAdapter { mark(endLabel) }) + methodNode.visitLocalVariable( + CONTINUATION_VARIABLE_NAME, + languageVersionSettings.continuationAsmType().descriptor, + null, + startLabel, + endLabel, + continuationIndex + ) + methodNode.visitLocalVariable( + SUSPEND_CALL_RESULT_NAME, + AsmTypes.OBJECT_TYPE.descriptor, + null, + resultStartLabel, + endLabel, + dataIndex ) } @@ -454,12 +465,15 @@ class CoroutineTransformerMethodVisitor( visitLabel(afterCoroutineStateCreated) - addContinuationToLvt(methodNode, LabelNode(afterCoroutineStateCreated)) - visitVarInsn(Opcodes.ALOAD, continuationIndex) getfield(classBuilderForCoroutineState.thisName, languageVersionSettings.dataFieldName(), AsmTypes.OBJECT_TYPE.descriptor) visitVarInsn(Opcodes.ASTORE, dataIndex) + val resultStartLabel = Label() + visitLabel(resultStartLabel) + + addContinuationAndResultToLvt(methodNode, afterCoroutineStateCreated, resultStartLabel) + if (!languageVersionSettings.isReleaseCoroutines()) { visitVarInsn(Opcodes.ALOAD, continuationIndex) getfield(classBuilderForCoroutineState.thisName, EXCEPTION_FIELD_NAME, AsmTypes.JAVA_THROWABLE_TYPE.descriptor) diff --git a/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/nonStaticStateMachine.kt b/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/nonStaticStateMachine.kt index 9c860ba7074..7052d54e540 100644 --- a/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/nonStaticStateMachine.kt +++ b/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/nonStaticStateMachine.kt @@ -13,4 +13,5 @@ class A { // VARIABLE : NAME=this TYPE=LA; INDEX=0 // VARIABLE : NAME=l TYPE=J INDEX=1 // VARIABLE : NAME=$completion TYPE=Lkotlin/coroutines/Continuation; INDEX=3 -// VARIABLE : NAME=$continuation TYPE=Lkotlin/coroutines/Continuation; INDEX=5 \ No newline at end of file +// VARIABLE : NAME=$continuation TYPE=Lkotlin/coroutines/Continuation; INDEX=5 +// VARIABLE : NAME=$result TYPE=Ljava/lang/Object; INDEX=4 \ No newline at end of file diff --git a/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/staticStateMachine.kt b/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/staticStateMachine.kt index 464cd2c779b..c078723ddad 100644 --- a/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/staticStateMachine.kt +++ b/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/staticStateMachine.kt @@ -10,4 +10,5 @@ suspend fun foo1(l: Long) { // METHOD : StaticStateMachineKt.foo1(JLkotlin/coroutines/Continuation;)Ljava/lang/Object; // VARIABLE : NAME=l TYPE=J INDEX=0 // VARIABLE : NAME=$completion TYPE=Lkotlin/coroutines/Continuation; INDEX=2 -// VARIABLE : NAME=$continuation TYPE=Lkotlin/coroutines/Continuation; INDEX=4 \ No newline at end of file +// VARIABLE : NAME=$continuation TYPE=Lkotlin/coroutines/Continuation; INDEX=4 +// VARIABLE : NAME=$result TYPE=Ljava/lang/Object; INDEX=3 \ No newline at end of file diff --git a/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/staticStateMachineReceiver.kt b/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/staticStateMachineReceiver.kt index 27005028ea3..b58207f157b 100644 --- a/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/staticStateMachineReceiver.kt +++ b/compiler/testData/checkLocalVariablesTable/completionInSuspendFunction/staticStateMachineReceiver.kt @@ -13,4 +13,5 @@ suspend fun A.foo1(l: Long) { // VARIABLE : NAME=$this$foo1 TYPE=LA; INDEX=0 // VARIABLE : NAME=l TYPE=J INDEX=1 // VARIABLE : NAME=$completion TYPE=Lkotlin/coroutines/Continuation; INDEX=3 -// VARIABLE : NAME=$continuation TYPE=Lkotlin/coroutines/Continuation; INDEX=5 \ No newline at end of file +// VARIABLE : NAME=$continuation TYPE=Lkotlin/coroutines/Continuation; INDEX=5 +// VARIABLE : NAME=$result TYPE=Ljava/lang/Object; INDEX=4 \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/coroutines/debug/thisAndResultInLvt.kt b/compiler/testData/codegen/bytecodeText/coroutines/debug/thisAndResultInLvt.kt index 35241dc87d1..bcd0e2626c0 100644 --- a/compiler/testData/codegen/bytecodeText/coroutines/debug/thisAndResultInLvt.kt +++ b/compiler/testData/codegen/bytecodeText/coroutines/debug/thisAndResultInLvt.kt @@ -22,4 +22,4 @@ class A { // 1 LOCALVARIABLE a LA; L0 L.* 1 // 1 LOCALVARIABLE s Ljava/lang/String; L0 L.* 2 // 1 LOCALVARIABLE block Lkotlin/jvm/functions/Function2; L0 L.* 3 -// 1 LOCALVARIABLE \$continuation Lkotlin/coroutines/Continuation; L2 L7 6 +// 1 LOCALVARIABLE \$continuation Lkotlin/coroutines/Continuation; L2 L.* 6