From b2620709226dd1e31f1354cac10c4b5d192d1ae4 Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Tue, 7 Feb 2023 16:01:10 +0100 Subject: [PATCH] JVM IR: Copy type parameters from outer class to suspendImpl Previously we copied only type parameters from containing class, but we need to do that for outer classes as well. #KT-55125 --- .../FirBlackBoxCodegenTestGenerated.java | 6 +++++ .../jvm/lower/AddContinuationLowering.kt | 3 ++- .../org/jetbrains/kotlin/ir/util/IrUtils.kt | 3 ++- .../suspendImplTypeParametersOuterClass.kt | 22 +++++++++++++++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/coroutines/suspendImplTypeParametersOuterClass.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 156291e7870..725b714d9ff 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 @@ -10807,6 +10807,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt"); } + @Test + @TestMetadata("suspendImplTypeParametersOuterClass.kt") + public void testSuspendImplTypeParametersOuterClass() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendImplTypeParametersOuterClass.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 3dacf726a0f..5c17507e5ea 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 @@ -33,6 +33,7 @@ import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.defaultType +import org.jetbrains.kotlin.ir.types.extractTypeParameters import org.jetbrains.kotlin.ir.types.typeWith import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.IrElementTransformer @@ -226,7 +227,7 @@ private class AddContinuationLowering(context: JvmBackendContext) : SuspendLower JavaDescriptorVisibilities.PACKAGE_VISIBILITY, isFakeOverride = false, copyMetadata = false, - typeParametersFromContext = irFunction.parentAsClass.typeParameters, + typeParametersFromContext = extractTypeParameters(irFunction.parentAsClass) ) static.body = irFunction.moveBodyTo(static) // Fixup dispatch parameter to outer class diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt index 33011b622b3..915948a1434 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/IrUtils.kt @@ -1281,7 +1281,7 @@ private fun IrSimpleFunction.copyAndRenameConflictingTypeParametersFrom( val existingNames = (contextParameters.map { it.name.asString() } + existingParameters.map { it.name.asString() }).toMutableSet() - contextParameters.forEach { contextType -> + contextParameters.forEachIndexed { i, contextType -> val newName = if (existingParameters.any { it.name.asString() == contextType.name.asString() }) { val newNamePrefix = contextType.name.asString() + "_I" val newName = newNamePrefix + generateSequence(1) { x -> x + 1 }.first { n -> @@ -1295,6 +1295,7 @@ private fun IrSimpleFunction.copyAndRenameConflictingTypeParametersFrom( newParameters.add(buildTypeParameter(this) { updateFrom(contextType) + index = i name = Name.identifier(newName) }) } diff --git a/compiler/testData/codegen/box/coroutines/suspendImplTypeParametersOuterClass.kt b/compiler/testData/codegen/box/coroutines/suspendImplTypeParametersOuterClass.kt new file mode 100644 index 00000000000..d1cd658de24 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/suspendImplTypeParametersOuterClass.kt @@ -0,0 +1,22 @@ +// TARGET_BACKEND: JVM_IR +// WITH_STDLIB +// FULL_JDK + +class Outer { + inner abstract class AbstractPersistence { + open suspend fun fetch(identifier: U): T? = null + } + + fun foo(): String = AbstractPersistence::class.java.declaredMethods + .single { it.name.contains("\$suspendImpl") } + .toGenericString() +} + +fun box(): String { + val genericString = Outer().foo() + if (!genericString.startsWith("static ")) { + return genericString + } + + return "OK" +} \ No newline at end of file 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 5ec4837cb89..150aaba89b1 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 @@ -10807,6 +10807,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt"); } + @Test + @TestMetadata("suspendImplTypeParametersOuterClass.kt") + public void testSuspendImplTypeParametersOuterClass() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendImplTypeParametersOuterClass.kt"); + } + @Test @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle() throws Exception {