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
This commit is contained in:
Ilmir Usmanov
2023-02-07 16:01:10 +01:00
committed by Space Team
parent 5cd817955b
commit b262070922
5 changed files with 38 additions and 2 deletions
@@ -10807,6 +10807,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt"); 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 @Test
@TestMetadata("suspendInCycle.kt") @TestMetadata("suspendInCycle.kt")
public void testSuspendInCycle() throws Exception { public void testSuspendInCycle() throws Exception {
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType 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.types.typeWith
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
@@ -226,7 +227,7 @@ private class AddContinuationLowering(context: JvmBackendContext) : SuspendLower
JavaDescriptorVisibilities.PACKAGE_VISIBILITY, JavaDescriptorVisibilities.PACKAGE_VISIBILITY,
isFakeOverride = false, isFakeOverride = false,
copyMetadata = false, copyMetadata = false,
typeParametersFromContext = irFunction.parentAsClass.typeParameters, typeParametersFromContext = extractTypeParameters(irFunction.parentAsClass)
) )
static.body = irFunction.moveBodyTo(static) static.body = irFunction.moveBodyTo(static)
// Fixup dispatch parameter to outer class // Fixup dispatch parameter to outer class
@@ -1281,7 +1281,7 @@ private fun IrSimpleFunction.copyAndRenameConflictingTypeParametersFrom(
val existingNames = val existingNames =
(contextParameters.map { it.name.asString() } + existingParameters.map { it.name.asString() }).toMutableSet() (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 newName = if (existingParameters.any { it.name.asString() == contextType.name.asString() }) {
val newNamePrefix = contextType.name.asString() + "_I" val newNamePrefix = contextType.name.asString() + "_I"
val newName = newNamePrefix + generateSequence(1) { x -> x + 1 }.first { n -> val newName = newNamePrefix + generateSequence(1) { x -> x + 1 }.first { n ->
@@ -1295,6 +1295,7 @@ private fun IrSimpleFunction.copyAndRenameConflictingTypeParametersFrom(
newParameters.add(buildTypeParameter(this) { newParameters.add(buildTypeParameter(this) {
updateFrom(contextType) updateFrom(contextType)
index = i
name = Name.identifier(newName) name = Name.identifier(newName)
}) })
} }
@@ -0,0 +1,22 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// FULL_JDK
class Outer<U> {
inner abstract class AbstractPersistence<T> {
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<Unit>().foo()
if (!genericString.startsWith("static <T,U>")) {
return genericString
}
return "OK"
}
@@ -10807,6 +10807,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/suspendImplTypeParameters.kt"); 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 @Test
@TestMetadata("suspendInCycle.kt") @TestMetadata("suspendInCycle.kt")
public void testSuspendInCycle() throws Exception { public void testSuspendInCycle() throws Exception {