Skip to content

Commit 6f2ea1e

Browse files
ting-yuanKSP Auto Pick
authored and
KSP Auto Pick
committed
KSP2: return type aliases if possible
(cherry picked from commit e5503dd)
1 parent 1afb280 commit 6f2ea1e

File tree

10 files changed

+37
-10
lines changed

10 files changed

+37
-10
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolv
77
import com.google.devtools.ksp.symbol.*
88
import org.jetbrains.kotlin.analysis.api.types.KaFunctionType
99
import org.jetbrains.kotlin.analysis.api.types.KaType
10+
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
1011

1112
// TODO: implement a psi based version, rename this class to resolved Impl.
1213
class KSCallableReferenceImpl private constructor(
@@ -18,15 +19,15 @@ class KSCallableReferenceImpl private constructor(
1819
cache.getOrPut(IdKeyPair(ktFunctionalType, parent)) { KSCallableReferenceImpl(ktFunctionalType, parent) }
1920
}
2021
override val receiverType: KSTypeReference?
21-
get() = ktFunctionalType.receiverType?.let { KSTypeReferenceResolvedImpl.getCached(it) }
22+
get() = ktFunctionalType.receiverType?.abbreviationOrSelf?.let { KSTypeReferenceResolvedImpl.getCached(it) }
2223

2324
override val functionParameters: List<KSValueParameter>
2425
get() = ktFunctionalType.parameterTypes.map {
2526
KSValueParameterLiteImpl.getCached(it, this@KSCallableReferenceImpl)
2627
}
2728

2829
override val returnType: KSTypeReference
29-
get() = KSTypeReferenceResolvedImpl.getCached(ktFunctionalType.returnType)
30+
get() = KSTypeReferenceResolvedImpl.getCached(ktFunctionalType.returnType.abbreviationOrSelf)
3031

3132
override val typeArguments: List<KSTypeArgument>
3233
get() = ktFunctionalType.typeArguments().map { KSTypeArgumentResolvedImpl.getCached(it, this) }

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.analysis.api.KaExperimentalApi
3232
import org.jetbrains.kotlin.analysis.api.KaImplementationDetail
3333
import org.jetbrains.kotlin.analysis.api.impl.base.types.KaBaseStarTypeProjection
3434
import org.jetbrains.kotlin.analysis.api.symbols.*
35+
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
3536
import org.jetbrains.kotlin.psi.KtClassOrObject
3637

3738
class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSymbol: KaClassSymbol) :
@@ -84,7 +85,7 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym
8485
}
8586
} ?: analyze {
8687
val supers = ktClassOrObjectSymbol.superTypes.mapIndexed { index, type ->
87-
KSTypeReferenceResolvedImpl.getCached(type, this@KSClassDeclarationImpl, index)
88+
KSTypeReferenceResolvedImpl.getCached(type.abbreviationOrSelf, this@KSClassDeclarationImpl, index)
8889
}
8990
// AA is returning additional kotlin.Any for java classes, explicitly extending kotlin.Any will result in
9091
// compile error, therefore filtering by name should work.

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.google.devtools.ksp.symbol.*
2828
import com.intellij.psi.PsiClass
2929
import com.intellij.psi.PsiMethod
3030
import org.jetbrains.kotlin.analysis.api.symbols.*
31+
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
3132
import org.jetbrains.kotlin.psi.KtDeclaration
3233
import org.jetbrains.kotlin.psi.KtFunction
3334

@@ -82,7 +83,7 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo
8283
ktFunctionSymbol.receiverParameter?.annotations ?: emptyList()
8384
)
8485
}
85-
?: ktFunctionSymbol.receiverType?.let {
86+
?: ktFunctionSymbol.receiverType?.abbreviationOrSelf?.let {
8687
KSTypeReferenceResolvedImpl.getCached(
8788
it,
8889
this@KSFunctionDeclarationImpl,
@@ -101,7 +102,7 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo
101102
if (ktFunctionSymbol is KaConstructorSymbol) {
102103
((parentDeclaration as KSClassDeclaration).asStarProjectedType() as KSTypeImpl).type
103104
} else {
104-
ktFunctionSymbol.returnType
105+
ktFunctionSymbol.returnType.abbreviationOrSelf
105106
}.let { KSTypeReferenceResolvedImpl.getCached(it, this@KSFunctionDeclarationImpl) }
106107
}
107108
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.google.devtools.ksp.symbol.*
2525
import org.jetbrains.kotlin.analysis.api.symbols.KaPropertyAccessorSymbol
2626
import org.jetbrains.kotlin.analysis.api.symbols.KaPropertyGetterSymbol
2727
import org.jetbrains.kotlin.analysis.api.symbols.KaPropertySetterSymbol
28+
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
2829
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
2930
import org.jetbrains.kotlin.psi.KtDeclaration
3031
import org.jetbrains.kotlin.psi.KtModifierListOwner
@@ -139,7 +140,7 @@ class KSPropertyGetterImpl private constructor(
139140
override val returnType: KSTypeReference? by lazy {
140141
((owner as? KSPropertyDeclarationImpl)?.ktPropertySymbol?.psiIfSource() as? KtProperty)?.typeReference
141142
?.let { KSTypeReferenceImpl.getCached(it, this) }
142-
?: KSTypeReferenceResolvedImpl.getCached(getter.returnType, this@KSPropertyGetterImpl)
143+
?: KSTypeReferenceResolvedImpl.getCached(getter.returnType.abbreviationOrSelf, this@KSPropertyGetterImpl)
143144
}
144145

145146
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KaPropertySymbol
3939
import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolModality
4040
import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolVisibility
4141
import org.jetbrains.kotlin.analysis.api.symbols.receiverType
42+
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
4243
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
4344
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
4445
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
@@ -111,7 +112,7 @@ class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbo
111112
ktPropertySymbol.receiverParameter?.annotations ?: emptyList()
112113
)
113114
}
114-
?: ktPropertySymbol.receiverType?.let {
115+
?: ktPropertySymbol.receiverType?.abbreviationOrSelf?.let {
115116
KSTypeReferenceResolvedImpl.getCached(
116117
it,
117118
this@KSPropertyDeclarationImpl,
@@ -123,7 +124,10 @@ class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbo
123124

124125
override val type: KSTypeReference by lazy {
125126
(ktPropertySymbol.psiIfSource() as? KtProperty)?.typeReference?.let { KSTypeReferenceImpl.getCached(it, this) }
126-
?: KSTypeReferenceResolvedImpl.getCached(ktPropertySymbol.returnType, this@KSPropertyDeclarationImpl)
127+
?: KSTypeReferenceResolvedImpl.getCached(
128+
ktPropertySymbol.returnType.abbreviationOrSelf,
129+
this@KSPropertyDeclarationImpl
130+
)
127131
}
128132

129133
override val isMutable: Boolean by lazy {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.google.devtools.ksp.symbol.KSType
1111
import com.google.devtools.ksp.symbol.KSTypeReference
1212
import com.google.devtools.ksp.symbol.KSVisitor
1313
import org.jetbrains.kotlin.analysis.api.symbols.KaLocalVariableSymbol
14+
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
1415
import org.jetbrains.kotlin.psi.KtProperty
1516

1617
class KSPropertyDeclarationLocalVariableImpl private constructor(
@@ -32,7 +33,7 @@ class KSPropertyDeclarationLocalVariableImpl private constructor(
3233
override val type: KSTypeReference by lazy {
3334
(ktLocalVariableSymbol.psiIfSource() as? KtProperty)?.typeReference
3435
?.let { KSTypeReferenceImpl.getCached(it, this) }
35-
?: KSTypeReferenceResolvedImpl.getCached(ktLocalVariableSymbol.returnType, this)
36+
?: KSTypeReferenceResolvedImpl.getCached(ktLocalVariableSymbol.returnType.abbreviationOrSelf, this)
3637
}
3738

3839
override val isMutable: Boolean = !ktLocalVariableSymbol.isVal

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolv
2424
import com.google.devtools.ksp.symbol.*
2525
import org.jetbrains.kotlin.analysis.api.fir.symbols.KaFirValueParameterSymbol
2626
import org.jetbrains.kotlin.analysis.api.symbols.KaValueParameterSymbol
27+
import org.jetbrains.kotlin.analysis.api.types.abbreviationOrSelf
2728
import org.jetbrains.kotlin.fir.java.JavaTypeParameterStack
2829
import org.jetbrains.kotlin.fir.java.declarations.FirJavaValueParameter
2930
import org.jetbrains.kotlin.fir.java.resolveIfJavaType
@@ -59,7 +60,10 @@ class KSValueParameterImpl private constructor(
5960
}
6061
(ktValueParameterSymbol.psiIfSource() as? KtParameter)?.typeReference
6162
?.let { KSTypeReferenceImpl.getCached(it, this) }
62-
?: KSTypeReferenceResolvedImpl.getCached(ktValueParameterSymbol.returnType, this@KSValueParameterImpl)
63+
?: KSTypeReferenceResolvedImpl.getCached(
64+
ktValueParameterSymbol.returnType.abbreviationOrSelf,
65+
this@KSValueParameterImpl
66+
)
6367
}
6468

6569
override val isVararg: Boolean by lazy {

kotlin-analysis-api/testData/typeAlias.kt

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
// nested2 : List<ListOfInt> = (expanded) List<List<Int>>
3737
// param w.o. asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
3838
// param with asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
39+
// param: MyAlias: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
3940
// END
4041

4142
// MODULE: module1

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

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.google.devtools.ksp.processor
1919

2020
import com.google.devtools.ksp.getConstructors
21+
import com.google.devtools.ksp.getDeclaredProperties
2122
import com.google.devtools.ksp.processing.Resolver
2223
import com.google.devtools.ksp.symbol.*
2324

@@ -71,6 +72,17 @@ open class TypeAliasProcessor : AbstractTestProcessor() {
7172

7273
results.add("param w.o. asMemberOf: $type1Signatures = (expanded) $type1Expanded")
7374
results.add("param with asMemberOf: $type2Signatures = (expanded) $type2Expanded")
75+
76+
val property = subject.getDeclaredProperties().single()
77+
val propertyType = property.type.resolve()
78+
val propertyTypeSignatures = propertyType.typeAliasSignatures().joinToString(" = ")
79+
val propertyTypeExpanded = resolver.expandType(propertyType).toSignature()
80+
results.add(
81+
"${property.simpleName.asString()}: " +
82+
"${propertyType.declaration.qualifiedName?.asString()}: " +
83+
"$propertyTypeSignatures = (expanded) $propertyTypeExpanded"
84+
)
85+
7486
return emptyList()
7587
}
7688

test-utils/testData/api/typeAlias.kt

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
// nested2 : List<ListOfInt> = (expanded) List<List<Int>>
3737
// param w.o. asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
3838
// param with asMemberOf: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
39+
// param: MyAlias: MyAlias<String> = Foo<Bar<T>, Baz<T>> = (expanded) Foo<Bar<String>, Baz<String>>
3940
// END
4041

4142
// MODULE: module1

0 commit comments

Comments
 (0)