From 901ff06e90ecf242e2022c474f658a0f881355ba Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Wed, 30 Nov 2022 19:29:55 +0100 Subject: [PATCH] JVM IR: Copy type parameters from class to suspendImpl These type parameters where used in function parameters, but since suspendImpl is static function, it has no access to class type parameters. Solution is to copy them to the function itself. #KT-55125 Fixed --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../jvm/lower/AddContinuationLowering.kt | 3 ++- .../coroutines/suspendImplTypeParameters.kt | 20 +++++++++++++++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 49a62b57034..3ee90b681db 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -10765,6 +10765,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/coroutines/suspendImplBridge.kt"); } + @Test + @TestMetadata("suspendImplTypeParameters.kt") + public void testSuspendImplTypeParameters() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt"); + } + @Test @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle() throws Exception { diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt index 021ec6af9d5..3dacf726a0f 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt @@ -225,7 +225,8 @@ private class AddContinuationLowering(context: JvmBackendContext) : SuspendLower else JavaDescriptorVisibilities.PACKAGE_VISIBILITY, isFakeOverride = false, - copyMetadata = false + copyMetadata = false, + typeParametersFromContext = irFunction.parentAsClass.typeParameters, ) static.body = irFunction.moveBodyTo(static) // Fixup dispatch parameter to outer class diff --git a/compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt b/compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt new file mode 100644 index 00000000000..78a3396a9ba --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt @@ -0,0 +1,20 @@ +// TARGET_BACKEND: JVM_IR +// WITH_STDLIB +// FULL_JDK + +abstract class AbstractPersistence { + open suspend fun fetch(identifier: U): T? = null +} + +fun box(): String { + val genericString = + AbstractPersistence::class.java.declaredMethods + .single { it.name.contains("\$suspendImpl") } + .toGenericString() + + if (!genericString.startsWith("static ")) { + return genericString + } + + return "OK" +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index f8e9a83367f..26ab8d2d74d 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -10765,6 +10765,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/coroutines/suspendImplBridge.kt"); } + @Test + @TestMetadata("suspendImplTypeParameters.kt") + public void testSuspendImplTypeParameters() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt"); + } + @Test @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle() throws Exception {