Skip to content

Commit

Permalink
compose-compiler: fix typeparam remapping
Browse files Browse the repository at this point in the history
fixes JetBrains/compose-multiplatform#3147

there is a bug on all platforms related to incorrect
`IrTypeParameterSymbol` remapping during function body transformation

because of `copyWithNewTypeParams` is called on function body directly,
it doesn't accumulate typeparam symbols mapping (located in signature),
which leads to preserving old symbols during body transformation

Test: tested against Compose Multiplatform testsuite

Change-Id: If813824a3387e78910670174bd6aabe7d13e2d43 ( https://android-review.googlesource.com/q/If813824a3387e78910670174bd6aabe7d13e2d43 )
Signed-off-by: Pavel Shishkin <pavel.shishkin@jetbrains.com>

Moved from: androidx/androidx@faae2fb
  • Loading branch information
shishkin-pavel authored and Space Cloud committed Apr 23, 2024
1 parent e572f72 commit beb970f
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeArgument
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.util.DeepCopySymbolRemapper
import org.jetbrains.kotlin.ir.util.DeepCopyTypeRemapper
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.SymbolRenamer
import org.jetbrains.kotlin.ir.util.TypeRemapper
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
import org.jetbrains.kotlin.ir.util.getAnnotation
import org.jetbrains.kotlin.ir.util.isTopLevel
import org.jetbrains.kotlin.ir.util.module
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
import org.jetbrains.kotlin.ir.util.remapTypeParameters
import org.jetbrains.kotlin.ir.util.toIrConst
import org.jetbrains.kotlin.ir.visitors.acceptVoid

@JvmDefaultWithCompatibility
internal interface DecoyTransformBase {
Expand Down Expand Up @@ -209,19 +211,28 @@ internal inline fun <reified T : IrElement> T.copyWithNewTypeParams(
source: IrFunction,
target: IrFunction
): T {
@Suppress("DEPRECATION")
return deepCopyWithSymbols(target) { symbolRemapper, typeRemapper ->
val typeParamRemapper = object : TypeRemapper by typeRemapper {
override fun remapType(type: IrType): IrType {
return typeRemapper.remapType(type.remapTypeParameters(source, target))
val typeParamsAwareSymbolRemapper = object : DeepCopySymbolRemapper() {
init {
for ((orig, new) in source.typeParameters.zip(target.typeParameters)) {
typeParameters[orig.symbol] = new.symbol
}
}
val deepCopy = DeepCopyPreservingMetadata(
symbolRemapper,
typeParamRemapper,
SymbolRenamer.DEFAULT
)
(typeRemapper as? DeepCopyTypeRemapper)?.deepCopy = deepCopy
deepCopy
}
val typeRemapper = DeepCopyTypeRemapper(typeParamsAwareSymbolRemapper)
val typeParamRemapper = object : TypeRemapper by typeRemapper {
override fun remapType(type: IrType): IrType {
return typeRemapper.remapType(type.remapTypeParameters(source, target))
}
}

@Suppress("DEPRECATION")
val deepCopy = DeepCopyPreservingMetadata(
typeParamsAwareSymbolRemapper,
typeParamRemapper,
SymbolRenamer.DEFAULT
)
typeRemapper.deepCopy = deepCopy

acceptVoid(typeParamsAwareSymbolRemapper)
return transform(deepCopy, null).patchDeclarationParents(target) as T
}

0 comments on commit beb970f

Please sign in to comment.