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 e8ac2eeb5fa..f07468ede65 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformerMethodVisitor.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/CoroutineTransformerMethodVisitor.kt @@ -828,23 +828,20 @@ internal fun InstructionAdapter.generateContinuationConstructorCall( private fun InstructionAdapter.generateResumeWithExceptionCheck(isReleaseCoroutines: Boolean, dataIndex: Int, exceptionIndex: Int) { // Check if resumeWithException has been called - load(if (isReleaseCoroutines) dataIndex else exceptionIndex, AsmTypes.OBJECT_TYPE) - dup() - val noExceptionLabel = Label() if (isReleaseCoroutines) { - instanceOf(AsmTypes.RESULT_FAILURE) - ifeq(noExceptionLabel) - // TODO: do we need this checkcast? - checkcast(AsmTypes.RESULT_FAILURE) - getfield(AsmTypes.RESULT_FAILURE.internalName, "exception", AsmTypes.JAVA_THROWABLE_TYPE.descriptor) + load(dataIndex, AsmTypes.OBJECT_TYPE) + invokestatic("kotlin/ResultKt", "throwOnFailure", "(Ljava/lang/Object;)V", false) } else { + load(exceptionIndex, AsmTypes.OBJECT_TYPE) + dup() + val noExceptionLabel = Label() ifnull(noExceptionLabel) - } - athrow() + athrow() - mark(noExceptionLabel) - pop() + mark(noExceptionLabel) + pop() + } } private fun Type.fieldNameForVar(index: Int) = descriptor.first() + "$" + index diff --git a/compiler/testData/codegen/bytecodeText/coroutines/throwOnFailure.kt b/compiler/testData/codegen/bytecodeText/coroutines/throwOnFailure.kt new file mode 100644 index 00000000000..bf3a9a45006 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/coroutines/throwOnFailure.kt @@ -0,0 +1,12 @@ +// IGNORE_BACKEND: JVM_IR +// WITH_COROUTINES +// TREAT_AS_ONE_FILE + +suspend fun foo() {} + +suspend fun bar(): Int { + foo() + return 42 +} + +// 2 INVOKESTATIC kotlin/ResultKt.throwOnFailure \(Ljava/lang/Object;\)V diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 36e6b270a1c..267f8fa4754 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -1125,6 +1125,11 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { runTestWithPackageReplacement("compiler/testData/codegen/bytecodeText/coroutines/returnUnitInLambda.kt", "kotlin.coroutines"); } + @TestMetadata("throwOnFailure.kt") + public void testThrowOnFailure() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/coroutines/throwOnFailure.kt"); + } + @TestMetadata("varValueConflictsWithTable.kt") public void testVarValueConflictsWithTable() throws Exception { runTest("compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTable.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java index 3e1586ba59f..e32a022c7aa 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java @@ -1125,6 +1125,11 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest { runTestWithPackageReplacement("compiler/testData/codegen/bytecodeText/coroutines/returnUnitInLambda.kt", "kotlin.coroutines"); } + @TestMetadata("throwOnFailure.kt") + public void testThrowOnFailure() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/coroutines/throwOnFailure.kt"); + } + @TestMetadata("varValueConflictsWithTable.kt") public void testVarValueConflictsWithTable() throws Exception { runTest("compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTable.kt");