IR: take care of supertypes when copying IrTypeParameters
This commit is contained in:
+1
-1
@@ -21,6 +21,6 @@ class IrTypeParameterBuilder : IrDeclarationBuilder() {
|
||||
index = from.index
|
||||
variance = from.variance
|
||||
isReified = from.isReified
|
||||
superTypes.addAll(from.superTypes)
|
||||
// Do not copy superTypes. You typically want a remapping for a group of type parameters at a time, see IrTypeParameterRemapper.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.ir.util
|
||||
|
||||
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
|
||||
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
|
||||
import org.jetbrains.kotlin.ir.types.*
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrTypeAbbreviationImpl
|
||||
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
|
||||
|
||||
/* After moving an IrElement, some type parameter references within it may become out of scope.
|
||||
This remapper restores validity by redirecting those references to new type parameters.
|
||||
*/
|
||||
class IrTypeParameterRemapper(
|
||||
val typeParameterMap: Map<IrTypeParameter, IrTypeParameter>
|
||||
) : TypeRemapper {
|
||||
override fun enterScope(irTypeParametersContainer: IrTypeParametersContainer) {}
|
||||
override fun leaveScope() {}
|
||||
|
||||
override fun remapType(type: IrType): IrType =
|
||||
if (type !is IrSimpleType)
|
||||
type
|
||||
else
|
||||
IrSimpleTypeImpl(
|
||||
null,
|
||||
type.classifier.remap(),
|
||||
type.hasQuestionMark,
|
||||
type.arguments.map { it.remap() },
|
||||
type.annotations,
|
||||
type.abbreviation?.remap()
|
||||
).apply {
|
||||
annotations.forEach { it.remapTypes(this@IrTypeParameterRemapper) }
|
||||
}
|
||||
|
||||
private fun IrClassifierSymbol.remap() =
|
||||
(owner as? IrTypeParameter)?.let { typeParameterMap[it]?.symbol }
|
||||
?: this
|
||||
|
||||
private fun IrTypeArgument.remap() =
|
||||
if (this is IrTypeProjection)
|
||||
makeTypeProjection(remapType(type), variance)
|
||||
else
|
||||
this
|
||||
|
||||
private fun IrTypeAbbreviation.remap() =
|
||||
IrTypeAbbreviationImpl(
|
||||
typeAlias,
|
||||
hasQuestionMark,
|
||||
arguments.map { it.remap() },
|
||||
annotations
|
||||
).apply {
|
||||
annotations.forEach { it.remapTypes(this@IrTypeParameterRemapper) }
|
||||
}
|
||||
}
|
||||
@@ -24,9 +24,9 @@ private class RemapTypesHelper(private val typeRemapper: TypeRemapper) : IrEleme
|
||||
element.acceptChildrenVoid(this)
|
||||
}
|
||||
|
||||
override fun visitExpression(expression: IrExpression) {
|
||||
expression.type = typeRemapper.remapType(expression.type)
|
||||
super.visitExpression(expression)
|
||||
override fun visitClass(declaration: IrClass) {
|
||||
declaration.superTypes = declaration.superTypes.map { typeRemapper.remapType(it) }
|
||||
super.visitClass(declaration)
|
||||
}
|
||||
|
||||
override fun visitValueParameter(declaration: IrValueParameter) {
|
||||
@@ -34,6 +34,11 @@ private class RemapTypesHelper(private val typeRemapper: TypeRemapper) : IrEleme
|
||||
super.visitValueParameter(declaration)
|
||||
}
|
||||
|
||||
override fun visitTypeParameter(declaration: IrTypeParameter) {
|
||||
declaration.superTypes = declaration.superTypes.map { typeRemapper.remapType(it) }
|
||||
super.visitTypeParameter(declaration)
|
||||
}
|
||||
|
||||
override fun visitVariable(declaration: IrVariable) {
|
||||
declaration.type = typeRemapper.remapType(declaration.type)
|
||||
super.visitVariable(declaration)
|
||||
@@ -59,6 +64,11 @@ private class RemapTypesHelper(private val typeRemapper: TypeRemapper) : IrEleme
|
||||
super.visitTypeAlias(declaration)
|
||||
}
|
||||
|
||||
override fun visitExpression(expression: IrExpression) {
|
||||
expression.type = typeRemapper.remapType(expression.type)
|
||||
super.visitExpression(expression)
|
||||
}
|
||||
|
||||
override fun visitTypeOperator(expression: IrTypeOperatorCall) {
|
||||
expression.typeOperand = typeRemapper.remapType(expression.typeOperand)
|
||||
super.visitTypeOperator(expression)
|
||||
|
||||
Reference in New Issue
Block a user