Skip to content

Commit

Permalink
Fix type remapping of lambda value classes
Browse files Browse the repository at this point in the history
The valueClassRepresentation type should also be updated by the remapper

Test: ComposeBytecodeCodegenTest
  • Loading branch information
ShikaSD authored and Space Cloud committed Sep 27, 2024
1 parent 586d337 commit 9f97f04
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -712,4 +712,17 @@ class ComposeBytecodeCodegenTest(useFir: Boolean) : AbstractCodegenTest(useFir)
}
"""
)

@Test
fun inlineClassWithComposableLambda() {
testCompile(
"""
import androidx.compose.runtime.*
import kotlin.jvm.JvmInline
@JvmInline
value class ComposableContent(val content: @Composable () -> Unit)
"""
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,33 @@ class ComposeCrossModuleTests(useFir: Boolean) : AbstractCodegenTest(useFir) {
)
}

@Test
fun inlineClassWithComposableLambda() {
compile(
mapOf(
"Base" to mapOf(
"Base.kt" to """
import androidx.compose.runtime.*
import kotlin.jvm.JvmInline
@JvmInline
value class ComposableContent(val content: @Composable () -> Unit)
"""
),
"Main" to mapOf(
"Main.kt" to """
import androidx.compose.runtime.*
@Composable fun Test(content: ComposableContent) {
content.content.invoke()
}
"""
)
),
validate = {}
)
}

private fun compile(
modules: Map<String, Map<String, String>>,
dumpClasses: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal fun IrType?.containsComposableAnnotation(): Boolean {

internal class ComposableTypeTransformer(
private val context: IrPluginContext,
private val typeRemapper: TypeRemapper,
private val typeRemapper: ComposableTypeRemapper,
) : IrElementTransformerVoid() {
override fun visitSimpleFunction(declaration: IrSimpleFunction): IrStatement {
declaration.returnType = declaration.returnType.remapType()
Expand Down Expand Up @@ -231,6 +231,9 @@ internal class ComposableTypeTransformer(

override fun visitClass(declaration: IrClass): IrStatement {
declaration.superTypes = declaration.superTypes.memoryOptimizedMap { it.remapType() }
declaration.valueClassRepresentation?.run {
declaration.valueClassRepresentation = mapUnderlyingType { it.remapType() as IrSimpleType }
}
return super.visitClass(declaration)
}

Expand Down Expand Up @@ -297,7 +300,7 @@ internal class ComposableTypeTransformer(
private fun IrType.remapType() = typeRemapper.remapType(this)
}

class ComposerTypeRemapper(
class ComposableTypeRemapper(
private val context: IrPluginContext,
private val composerType: IrType,
) : TypeRemapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.name.JvmStandardClassIds
Expand Down Expand Up @@ -70,7 +69,7 @@ class ComposerParamTransformer(

module.transformChildrenVoid(this)

val typeRemapper = ComposerTypeRemapper(
val typeRemapper = ComposableTypeRemapper(
context,
composerType
)
Expand Down

0 comments on commit 9f97f04

Please sign in to comment.