diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt index 4bf2ef88328..77a4cbe2087 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/IrUtils.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.backend.common.ir import org.jetbrains.kotlin.backend.common.CommonBackendContext import org.jetbrains.kotlin.backend.common.DumpIrTreeWithDescriptorsVisitor import org.jetbrains.kotlin.backend.common.deepCopyWithVariables -import org.jetbrains.kotlin.backend.common.descriptors.* import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Visibilities @@ -166,12 +165,12 @@ fun IrValueParameter.copyTo( fun IrTypeParameter.copyToWithoutSuperTypes( target: IrTypeParametersContainer, - shift: Int = 0, + index: Int = this.index, origin: IrDeclarationOrigin = this.origin ): IrTypeParameter { val descriptor = WrappedTypeParameterDescriptor(symbol.descriptor.annotations, symbol.descriptor.source) val symbol = IrTypeParameterSymbolImpl(descriptor) - return IrTypeParameterImpl(startOffset, endOffset, origin, symbol, name, shift + index, isReified, variance).also { copied -> + return IrTypeParameterImpl(startOffset, endOffset, origin, symbol, name, index, isReified, variance).also { copied -> descriptor.bind(copied) copied.parent = target } @@ -199,17 +198,18 @@ fun IrFunction.copyParameterDeclarationsFrom(from: IrFunction) { fun IrTypeParametersContainer.copyTypeParameters( srcTypeParameters: List, origin: IrDeclarationOrigin? = null -) { +): List { val shift = typeParameters.size // Any type parameter can figure in a boundary type for any other parameter. // Therefore, we first copy the parameters themselves, then set up their supertypes. - srcTypeParameters.forEachIndexed { i, sourceParameter -> - assert(sourceParameter.index == i) - typeParameters.add(sourceParameter.copyToWithoutSuperTypes(this, shift = shift, origin = origin ?: sourceParameter.origin)) + val newTypeParameters = srcTypeParameters.mapIndexed { i, sourceParameter -> + sourceParameter.copyToWithoutSuperTypes(this, index = i + shift, origin = origin ?: sourceParameter.origin) } - srcTypeParameters.zip(typeParameters.drop(shift)).forEach { (srcParameter, dstParameter) -> + typeParameters.addAll(newTypeParameters) + srcTypeParameters.zip(newTypeParameters).forEach { (srcParameter, dstParameter) -> dstParameter.copySuperTypesFrom(srcParameter) } + return newTypeParameters } fun IrTypeParametersContainer.copyTypeParametersFrom( diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt index 8d78ecdf4a8..a9339f691bd 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AbstractSuspendFunctionsLowering.kt @@ -498,7 +498,7 @@ abstract class AbstractSuspendFunctionsLowering(val co coroutineClass.declarations += this irFunction.typeParameters.mapTo(typeParameters) { parameter -> - parameter.copyToWithoutSuperTypes(this, 0, DECLARATION_ORIGIN_COROUTINE_IMPL) + parameter.copyToWithoutSuperTypes(this, origin = DECLARATION_ORIGIN_COROUTINE_IMPL) .apply { superTypes += parameter.superTypes } } @@ -566,7 +566,7 @@ abstract class AbstractSuspendFunctionsLowering(val co coroutineClass.declarations += this irFunction.typeParameters.mapTo(typeParameters) { parameter -> - parameter.copyToWithoutSuperTypes(this, 0, DECLARATION_ORIGIN_COROUTINE_IMPL) + parameter.copyToWithoutSuperTypes(this, origin = DECLARATION_ORIGIN_COROUTINE_IMPL) .apply { superTypes += parameter.superTypes } } @@ -626,7 +626,7 @@ abstract class AbstractSuspendFunctionsLowering(val co coroutineClass.declarations += this stateMachineFunction.typeParameters.mapTo(typeParameters) { parameter -> - parameter.copyToWithoutSuperTypes(this, 0, DECLARATION_ORIGIN_COROUTINE_IMPL) + parameter.copyToWithoutSuperTypes(this, origin = DECLARATION_ORIGIN_COROUTINE_IMPL) .apply { superTypes += parameter.superTypes } }