From df3e562962ba5e58ed598f94afaa9bab3e02b86b Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Tue, 11 Jun 2024 17:05:26 -0400 Subject: [PATCH] Fix complex alias handling in KsTypes (#1929) --- docs/changelog.md | 1 + .../com/squareup/kotlinpoet/ksp/KsTypes.kt | 2 +- .../ksp/test/processor/TestProcessorTest.kt | 43 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 34ec2231dc..c0c33f0b7d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -5,6 +5,7 @@ Change Log * **Fix**: Don't expand typealiases of function types to `LambdaTypeName`s in `KSTypeReference.toTypeName()`. * **Fix**: Small double and float values were set to 0.0 in %L translation (#1919) +* **Fix**: Fix typealias type argument resolution in KSP2. * **Enhancement**: Make enum entry references in `KSAnnotation.toAnnotationSpec()` and `KSClassDeclaration.toClassName()` more robust. * Migrate `kotlinpoet-metadata` to stable `org.jetbrains.kotlin:kotlin-metadata-jvm` artifact for Metadata parsing. * Promote `kotlinpoet-metadata` out of preview to stable. diff --git a/interop/ksp/src/main/kotlin/com/squareup/kotlinpoet/ksp/KsTypes.kt b/interop/ksp/src/main/kotlin/com/squareup/kotlinpoet/ksp/KsTypes.kt index 19e48c01a3..500cb62aac 100644 --- a/interop/ksp/src/main/kotlin/com/squareup/kotlinpoet/ksp/KsTypes.kt +++ b/interop/ksp/src/main/kotlin/com/squareup/kotlinpoet/ksp/KsTypes.kt @@ -199,6 +199,6 @@ public fun KSTypeReference.toTypeName( returnType = elem.returnType.toTypeName(typeParamResolver), ).copy(nullable = type.isMarkedNullable, suspending = type.isSuspendFunctionType) } else { - type.toTypeName(typeParamResolver, element?.typeArguments.orEmpty()) + type.toTypeName(typeParamResolver, type.arguments) } } diff --git a/interop/ksp/test-processor/src/test/kotlin/com/squareup/kotlinpoet/ksp/test/processor/TestProcessorTest.kt b/interop/ksp/test-processor/src/test/kotlin/com/squareup/kotlinpoet/ksp/test/processor/TestProcessorTest.kt index 640012a3c1..966aca095a 100644 --- a/interop/ksp/test-processor/src/test/kotlin/com/squareup/kotlinpoet/ksp/test/processor/TestProcessorTest.kt +++ b/interop/ksp/test-processor/src/test/kotlin/com/squareup/kotlinpoet/ksp/test/processor/TestProcessorTest.kt @@ -911,6 +911,49 @@ class TestProcessorTest(private val useKsp2: Boolean) { ) } + @Test + fun complexAliasing() { + val compilation = prepareCompilation( + kotlin( + "Example.kt", + """ + package test + + import javax.inject.Provider + import com.squareup.kotlinpoet.ksp.test.processor.ExampleAnnotation + + typealias DaggerProvider = @JvmSuppressWildcards Provider + interface SelectOptions + interface SelectHandler + + @ExampleAnnotation + class Example( + private val handlers: Map, DaggerProvider>>, + ) + """, + ), + ) + + val result = compilation.compile() + assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK) + val generatedFileText = File(compilation.kspSourcesDir, "kotlin/test/TestExample.kt") + .readText() + + assertThat(generatedFileText).isEqualTo( + """ + package test + + import java.lang.Class + import kotlin.collections.Map + + public class TestExample { + private val handlers: Map, DaggerProvider>> = TODO() + } + + """.trimIndent(), + ) + } + private fun prepareCompilation(vararg sourceFiles: SourceFile): KotlinCompilation { return KotlinCompilation() .apply {