Skip to content

Commit

Permalink
KSFunction: return type aliases if possible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ting-yuan committed Feb 13, 2025
1 parent 9284800 commit 6b59afd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KaFunctionSymbol
import org.jetbrains.kotlin.analysis.api.symbols.typeParameters
import org.jetbrains.kotlin.analysis.api.types.KaSubstitutor
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import java.util.*

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

override val returnType: KSType? by lazy {
functionSignature.returnType.let { KSTypeImpl.getCached(it) }
functionSignature.returnType.abbreviationOrSelf.let { KSTypeImpl.getCached(it) }
}

override val parameterTypes: List<KSType?> by lazy {
functionSignature.valueParameters.map { it.returnType.let { KSTypeImpl.getCached(it) } }
functionSignature.valueParameters.map { it.returnType.abbreviationOrSelf.let { KSTypeImpl.getCached(it) } }
}

@OptIn(KaExperimentalApi::class)
Expand Down Expand Up @@ -69,7 +70,7 @@ class KSFunctionImpl @OptIn(KaExperimentalApi::class) constructor(
}

override val extensionReceiverType: KSType? by lazy {
functionSignature.receiverType?.let { KSTypeImpl.getCached(it) }
functionSignature.receiverType?.abbreviationOrSelf?.let { KSTypeImpl.getCached(it) }
}

override val isError: Boolean = false
Expand Down
8 changes: 8 additions & 0 deletions kotlin-analysis-api/testData/typeAlias.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
// viewBinderProviders : Map<Class<BaseViewHolder>, @JvmSuppressWildcards Provider<BaseEmbedViewBinder>> = (expanded) Map<Class<BaseViewHolder>, Provider<ViewBinder<BaseViewHolder, SpaceshipEmbedModel>>>
// nested1 : MyList<ListOfInt> = List<T> = (expanded) List<List<Int>>
// nested2 : List<ListOfInt> = (expanded) List<List<Int>>
// param w.o. asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
// param with asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
// END

// MODULE: module1
Expand Down Expand Up @@ -78,3 +80,9 @@ typealias BaseEmbedViewBinder = ViewBinder<out BaseViewHolder, out SpaceshipEmbe
val viewBinderProviders: Map<Class<out BaseViewHolder>, @JvmSuppressWildcards Provider<BaseEmbedViewBinder>> = TODO()
val nested1: MyList<ListOfInt>
val nested2: List<ListOfInt>

class Subject(val param: MyAlias<String>)
typealias MyAlias<T> = Foo<Bar<T>, Baz<T>>
class Foo<T1, T2>
class Bar<T>
class Baz<T>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.google.devtools.ksp.processor

import com.google.devtools.ksp.getConstructors
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.*

Expand Down Expand Up @@ -57,6 +58,19 @@ open class TypeAliasProcessor : AbstractTestProcessor() {
}
}
}

val subjectName = resolver.getKSNameFromString("Subject")
val subject = resolver.getClassDeclarationByName(subjectName)!!
val constructor = subject.getConstructors().single()
val type1 = constructor.parameters.single().type.resolve()
val type2 = constructor.asMemberOf(subject.asType(emptyList())).parameterTypes.single()!!
val type1Signatures = type1.typeAliasSignatures().joinToString(" = ")
val type2Signatures = type2.typeAliasSignatures().joinToString(" = ")
val type1Expanded = resolver.expandType(type1).toSignature()
val type2Expanded = resolver.expandType(type2).toSignature()

results.add("param w.o. asMemberOf: $type1Signatures = (expanded) $type1Expanded")
results.add("param with asMemberOf: $type2Signatures = (expanded) $type2Expanded")
return emptyList()
}

Expand Down
8 changes: 8 additions & 0 deletions test-utils/testData/api/typeAlias.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
// viewBinderProviders : Map<Class<BaseViewHolder>, @JvmSuppressWildcards Provider<BaseEmbedViewBinder>> = (expanded) Map<Class<BaseViewHolder>, Provider<ViewBinder<BaseViewHolder, SpaceshipEmbedModel>>>
// nested1 : MyList<ListOfInt> = List<T> = (expanded) List<List<Int>>
// nested2 : List<ListOfInt> = (expanded) List<List<Int>>
// param w.o. asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
// param with asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
// END

// MODULE: module1
Expand Down Expand Up @@ -79,3 +81,9 @@ typealias BaseEmbedViewBinder = ViewBinder<out BaseViewHolder, out SpaceshipEmbe
val viewBinderProviders: Map<Class<out BaseViewHolder>, @JvmSuppressWildcards Provider<BaseEmbedViewBinder>> = TODO()
val nested1: MyList<ListOfInt>
val nested2: List<ListOfInt>

class Subject(val param: MyAlias<String>)
typealias MyAlias<T> = Foo<Bar<T>, Baz<T>>
class Foo<T1, T2>
class Bar<T>
class Baz<T>

0 comments on commit 6b59afd

Please sign in to comment.