Skip to content

Commit

Permalink
fix type parameter bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed May 9, 2024
1 parent 90fd8c7 commit 6b244f6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.symbol.*
import org.jetbrains.kotlin.analysis.api.symbols.KtTypeParameterSymbol
import org.jetbrains.kotlin.psi.KtTypeParameter
import org.jetbrains.kotlin.psi.KtTypeParameterListOwner

class KSTypeParameterImpl private constructor(internal val ktTypeParameterSymbol: KtTypeParameterSymbol) :
KSTypeParameter,
Expand All @@ -51,27 +49,8 @@ class KSTypeParameterImpl private constructor(internal val ktTypeParameterSymbol
override val isReified: Boolean = ktTypeParameterSymbol.isReified

override val bounds: Sequence<KSTypeReference> by lazy {
when (val psi = ktTypeParameterSymbol.psi) {
is KtTypeParameter -> {
val owner = (parentDeclaration as? AbstractKSDeclarationImpl)
?.ktDeclarationSymbol?.psi as? KtTypeParameterListOwner
val list = sequenceOf(psi.extendsBound)
if (owner != null) {
list.plus(
owner.typeConstraints
.filter {
it.subjectTypeParameterName!!.getReferencedName() == psi.nameAsSafeName.asString()
}
.map { it.boundTypeReference }
)
}
list.filterNotNull().map { KSTypeReferenceImpl.getCached(it, this) }
}
else -> {
ktTypeParameterSymbol.upperBounds.asSequence().mapIndexed { index, type ->
KSTypeReferenceResolvedImpl.getCached(type, this@KSTypeParameterImpl, index)
}
}
ktTypeParameterSymbol.upperBounds.asSequence().mapIndexed { index, type ->
KSTypeReferenceResolvedImpl.getCached(type, this@KSTypeParameterImpl, index)
}.ifEmpty {
sequenceOf(
KSTypeReferenceSyntheticImpl.getCached(ResolverAAImpl.instance.builtIns.anyType.makeNullable(), this)
Expand Down
8 changes: 5 additions & 3 deletions kotlin-analysis-api/testData/typeParameterVariance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

// TEST PROCESSOR: TypeParameterVarianceProcessor
// EXPECTED:
// Bar INVARIANT T
// BarIn CONTRAVARIANT T
// BarOut COVARIANT T
// Bar INVARIANT T Any?
// BarIn CONTRAVARIANT T Any?
// BarOut COVARIANT T Any?
// BarBounds INVARIANT T Bar<T>, BarIn<T>
// END

// FILE: main.kt
Expand All @@ -28,3 +29,4 @@ interface Bar<T>
interface BarIn<in T>
interface BarOut<out T>

interface BarBounds<T> where T: Bar<T>, T: BarIn<T>
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ class TypeParameterVarianceProcessor : AbstractTestProcessor() {
override fun process(resolver: Resolver): List<KSAnnotated> {
fun KSDeclaration.printTypeParams(): String {
val params = typeParameters.joinToString {
"${it.variance} ${it.name.asString()}"
"${it.variance} ${it.name.asString()} ${it.bounds.joinToString { it.resolve().toString() }}"
}
return "${simpleName.asString()} $params"
}

results.add(resolver.getClassDeclarationByName("Bar")!!.printTypeParams())
results.add(resolver.getClassDeclarationByName("BarIn")!!.printTypeParams())
results.add(resolver.getClassDeclarationByName("BarOut")!!.printTypeParams())
results.add(resolver.getClassDeclarationByName("BarBounds")!!.printTypeParams())

return emptyList()
}
Expand Down

0 comments on commit 6b244f6

Please sign in to comment.