Skip to content

Commit 6b59afd

Browse files
committedFeb 13, 2025·
KSFunction: return type aliases if possible
AA expands types by default. Luckily, the abbreviations are handily available. This change makes KSFunction behave the same as other parts that return KSType in the API.
1 parent 9284800 commit 6b59afd

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed
 

‎kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSFunctionImpl.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KaFunctionSymbol
2626
import org.jetbrains.kotlin.analysis.api.symbols.typeParameters
2727
import org.jetbrains.kotlin.analysis.api.types.KaSubstitutor
2828
import org.jetbrains.kotlin.analysis.api.types.KaType
29+
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
2930
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
3031
import java.util.*
3132

@@ -35,11 +36,11 @@ class KSFunctionImpl @OptIn(KaExperimentalApi::class) constructor(
3536
) : KSFunction {
3637

3738
override val returnType: KSType? by lazy {
38-
functionSignature.returnType.let { KSTypeImpl.getCached(it) }
39+
functionSignature.returnType.abbreviationOrSelf.let { KSTypeImpl.getCached(it) }
3940
}
4041

4142
override val parameterTypes: List<KSType?> by lazy {
42-
functionSignature.valueParameters.map { it.returnType.let { KSTypeImpl.getCached(it) } }
43+
functionSignature.valueParameters.map { it.returnType.abbreviationOrSelf.let { KSTypeImpl.getCached(it) } }
4344
}
4445

4546
@OptIn(KaExperimentalApi::class)
@@ -69,7 +70,7 @@ class KSFunctionImpl @OptIn(KaExperimentalApi::class) constructor(
6970
}
7071

7172
override val extensionReceiverType: KSType? by lazy {
72-
functionSignature.receiverType?.let { KSTypeImpl.getCached(it) }
73+
functionSignature.receiverType?.abbreviationOrSelf?.let { KSTypeImpl.getCached(it) }
7374
}
7475

7576
override val isError: Boolean = false

‎kotlin-analysis-api/testData/typeAlias.kt

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
// viewBinderProviders : Map<Class<BaseViewHolder>, @JvmSuppressWildcards Provider<BaseEmbedViewBinder>> = (expanded) Map<Class<BaseViewHolder>, Provider<ViewBinder<BaseViewHolder, SpaceshipEmbedModel>>>
3535
// nested1 : MyList<ListOfInt> = List<T> = (expanded) List<List<Int>>
3636
// nested2 : List<ListOfInt> = (expanded) List<List<Int>>
37+
// param w.o. asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
38+
// param with asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
3739
// END
3840

3941
// MODULE: module1
@@ -78,3 +80,9 @@ typealias BaseEmbedViewBinder = ViewBinder<out BaseViewHolder, out SpaceshipEmbe
7880
val viewBinderProviders: Map<Class<out BaseViewHolder>, @JvmSuppressWildcards Provider<BaseEmbedViewBinder>> = TODO()
7981
val nested1: MyList<ListOfInt>
8082
val nested2: List<ListOfInt>
83+
84+
class Subject(val param: MyAlias<String>)
85+
typealias MyAlias<T> = Foo<Bar<T>, Baz<T>>
86+
class Foo<T1, T2>
87+
class Bar<T>
88+
class Baz<T>

‎test-utils/src/main/kotlin/com/google/devtools/ksp/processor/TypeAliasProcessor.kt

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.google.devtools.ksp.processor
1919

20+
import com.google.devtools.ksp.getConstructors
2021
import com.google.devtools.ksp.processing.Resolver
2122
import com.google.devtools.ksp.symbol.*
2223

@@ -57,6 +58,19 @@ open class TypeAliasProcessor : AbstractTestProcessor() {
5758
}
5859
}
5960
}
61+
62+
val subjectName = resolver.getKSNameFromString("Subject")
63+
val subject = resolver.getClassDeclarationByName(subjectName)!!
64+
val constructor = subject.getConstructors().single()
65+
val type1 = constructor.parameters.single().type.resolve()
66+
val type2 = constructor.asMemberOf(subject.asType(emptyList())).parameterTypes.single()!!
67+
val type1Signatures = type1.typeAliasSignatures().joinToString(" = ")
68+
val type2Signatures = type2.typeAliasSignatures().joinToString(" = ")
69+
val type1Expanded = resolver.expandType(type1).toSignature()
70+
val type2Expanded = resolver.expandType(type2).toSignature()
71+
72+
results.add("param w.o. asMemberOf: $type1Signatures = (expanded) $type1Expanded")
73+
results.add("param with asMemberOf: $type2Signatures = (expanded) $type2Expanded")
6074
return emptyList()
6175
}
6276

‎test-utils/testData/api/typeAlias.kt

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
// viewBinderProviders : Map<Class<BaseViewHolder>, @JvmSuppressWildcards Provider<BaseEmbedViewBinder>> = (expanded) Map<Class<BaseViewHolder>, Provider<ViewBinder<BaseViewHolder, SpaceshipEmbedModel>>>
3535
// nested1 : MyList<ListOfInt> = List<T> = (expanded) List<List<Int>>
3636
// nested2 : List<ListOfInt> = (expanded) List<List<Int>>
37+
// param w.o. asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
38+
// param with asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
3739
// END
3840

3941
// MODULE: module1
@@ -79,3 +81,9 @@ typealias BaseEmbedViewBinder = ViewBinder<out BaseViewHolder, out SpaceshipEmbe
7981
val viewBinderProviders: Map<Class<out BaseViewHolder>, @JvmSuppressWildcards Provider<BaseEmbedViewBinder>> = TODO()
8082
val nested1: MyList<ListOfInt>
8183
val nested2: List<ListOfInt>
84+
85+
class Subject(val param: MyAlias<String>)
86+
typealias MyAlias<T> = Foo<Bar<T>, Baz<T>>
87+
class Foo<T1, T2>
88+
class Bar<T>
89+
class Baz<T>

0 commit comments

Comments
 (0)
Please sign in to comment.