Skip to content

Commit df3e562

Browse files
authored
Fix complex alias handling in KsTypes (#1929)
1 parent fe3abfa commit df3e562

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change Log
55

66
* **Fix**: Don't expand typealiases of function types to `LambdaTypeName`s in `KSTypeReference.toTypeName()`.
77
* **Fix**: Small double and float values were set to 0.0 in %L translation (#1919)
8+
* **Fix**: Fix typealias type argument resolution in KSP2.
89
* **Enhancement**: Make enum entry references in `KSAnnotation.toAnnotationSpec()` and `KSClassDeclaration.toClassName()` more robust.
910
* Migrate `kotlinpoet-metadata` to stable `org.jetbrains.kotlin:kotlin-metadata-jvm` artifact for Metadata parsing.
1011
* Promote `kotlinpoet-metadata` out of preview to stable.

interop/ksp/src/main/kotlin/com/squareup/kotlinpoet/ksp/KsTypes.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,6 @@ public fun KSTypeReference.toTypeName(
199199
returnType = elem.returnType.toTypeName(typeParamResolver),
200200
).copy(nullable = type.isMarkedNullable, suspending = type.isSuspendFunctionType)
201201
} else {
202-
type.toTypeName(typeParamResolver, element?.typeArguments.orEmpty())
202+
type.toTypeName(typeParamResolver, type.arguments)
203203
}
204204
}

interop/ksp/test-processor/src/test/kotlin/com/squareup/kotlinpoet/ksp/test/processor/TestProcessorTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,49 @@ class TestProcessorTest(private val useKsp2: Boolean) {
911911
)
912912
}
913913

914+
@Test
915+
fun complexAliasing() {
916+
val compilation = prepareCompilation(
917+
kotlin(
918+
"Example.kt",
919+
"""
920+
package test
921+
922+
import javax.inject.Provider
923+
import com.squareup.kotlinpoet.ksp.test.processor.ExampleAnnotation
924+
925+
typealias DaggerProvider<T> = @JvmSuppressWildcards Provider<T>
926+
interface SelectOptions
927+
interface SelectHandler<T>
928+
929+
@ExampleAnnotation
930+
class Example(
931+
private val handlers: Map<Class<out SelectOptions>, DaggerProvider<SelectHandler<*>>>,
932+
)
933+
""",
934+
),
935+
)
936+
937+
val result = compilation.compile()
938+
assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
939+
val generatedFileText = File(compilation.kspSourcesDir, "kotlin/test/TestExample.kt")
940+
.readText()
941+
942+
assertThat(generatedFileText).isEqualTo(
943+
"""
944+
package test
945+
946+
import java.lang.Class
947+
import kotlin.collections.Map
948+
949+
public class TestExample {
950+
private val handlers: Map<Class<out SelectOptions>, DaggerProvider<SelectHandler<*>>> = TODO()
951+
}
952+
953+
""".trimIndent(),
954+
)
955+
}
956+
914957
private fun prepareCompilation(vararg sourceFiles: SourceFile): KotlinCompilation {
915958
return KotlinCompilation()
916959
.apply {

0 commit comments

Comments
 (0)