Skip to content

Commit

Permalink
Handle unresolved types for types from Java symbols.
Browse files Browse the repository at this point in the history
KotlinType is making such error type into a flexible type, need to
check if the declaration of the bounds of this flexible type is an
instance of NotFoundClasses.MockClassDescriptor.
  • Loading branch information
neetopia committed Oct 9, 2020
1 parent fddfece commit 37e2d07
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
Expand Up @@ -18,6 +18,7 @@

package com.google.devtools.ksp.processor

import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.impl.ResolverImpl
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
Expand All @@ -34,34 +35,37 @@ class ErrorTypeProcessor : AbstractTestProcessor() {
val classC = resolver.getClassDeclarationByName(resolver.getKSNameFromString("C"))!!
val errorAtTop = classC.declarations.single { it.simpleName.asString() == "errorAtTop" } as KSPropertyDeclaration
val errorInComponent = classC.declarations.single { it.simpleName.asString() == "errorInComponent" } as KSPropertyDeclaration
result.add(errorAtTop.type?.resolve()?.print() ?: "")
result.add(errorInComponent.type?.resolve()?.print() ?: "")
errorInComponent.type!!.resolve()!!.arguments.map { result.add(it.type!!.resolve()!!.print()) }
result.add(errorAtTop.type.resolve().print() ?: "")
result.add(errorInComponent.type.resolve().print() ?: "")
errorInComponent.type.resolve().arguments.map { result.add(it.type!!.resolve().print()) }
result.add(
"errorInComponent is assignable from errorAtTop: ${
errorAtTop.type!!.resolve()!!.isAssignableFrom(errorAtTop.type!!.resolve()!!)
errorAtTop.type.resolve().isAssignableFrom(errorAtTop.type.resolve())
}"
)
result.add(
"errorInComponent is assignable from class C: ${
errorAtTop.type!!.resolve()!!.isAssignableFrom(classC.asStarProjectedType())
errorAtTop.type.resolve().isAssignableFrom(classC.asStarProjectedType())
}"
)
result.add(
"Any is assignable from errorInComponent: ${
ResolverImpl.instance.builtIns.anyType.isAssignableFrom(errorAtTop.type!!.resolve()!!)
ResolverImpl.instance.builtIns.anyType.isAssignableFrom(errorAtTop.type.resolve())
}"
)
result.add(
"class C is assignable from errorInComponent: ${
classC.asStarProjectedType().isAssignableFrom(errorAtTop.type!!.resolve()!!)
classC.asStarProjectedType().isAssignableFrom(errorAtTop.type.resolve())
}"
)
result.add(
"Any is assignable from class C: ${
ResolverImpl.instance.builtIns.anyType.isAssignableFrom(classC.asStarProjectedType())
}"
)
val Cls = resolver.getClassDeclarationByName("Cls")!!
val type = Cls.superTypes[0].resolve()
result.add("Cls's super type is Error type: ${type.isError}")
}

private fun KSType.print(): String {
Expand Down
Expand Up @@ -23,8 +23,11 @@ import com.intellij.psi.impl.source.PsiClassReferenceType
import com.google.devtools.ksp.processing.impl.ResolverImpl
import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.impl.KSObjectCache
import com.google.devtools.ksp.symbol.impl.binary.KSClassDeclarationDescriptorImpl
import com.google.devtools.ksp.symbol.impl.binary.KSClassifierReferenceDescriptorImpl
import com.google.devtools.ksp.symbol.impl.kotlin.KSErrorType
import com.google.devtools.ksp.symbol.impl.toLocation
import org.jetbrains.kotlin.descriptors.NotFoundClasses
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance

Expand Down Expand Up @@ -87,7 +90,10 @@ class KSTypeReferenceJavaImpl private constructor(val psi: PsiType) : KSTypeRefe
}

override fun resolve(): KSType {
return ResolverImpl.instance.resolveUserType(this)
val resolvedType = ResolverImpl.instance.resolveUserType(this)
return if ((resolvedType.declaration as? KSClassDeclarationDescriptorImpl)?.descriptor is NotFoundClasses.MockClassDescriptor) {
KSErrorType
} else resolvedType
}

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
Expand Down
6 changes: 4 additions & 2 deletions compiler-plugin/testData/api/allFunctions.kt
Expand Up @@ -45,8 +45,8 @@
// indexOf(kotlin.Number): kotlin.Int
// isEmpty(): kotlin.Boolean
// iterator(): kotlin.collections.Iterator
// javaListFun(): Collection
// javaListFun(): kotlin.collections.List
// javaListFun(): kotlin.collections.MutableCollection
// javaPrivateFun(): kotlin.Unit
// javaStrFun(): kotlin.String
// javaStrFun(): kotlin.String
Expand Down Expand Up @@ -84,6 +84,8 @@ data class Data(val a: String) {
}

// FILE: C.java
import java.util.Collection;

class C {
public int aFromC = 1;
private int bFromC = 2;
Expand All @@ -92,7 +94,7 @@ class C {

}

protected Collection<Int> javaListFun() {
protected Collection<Integer> javaListFun() {
return Arrays.asList(1,2,3)
}

Expand Down
7 changes: 7 additions & 0 deletions compiler-plugin/testData/api/errorTypes.kt
Expand Up @@ -27,9 +27,16 @@
// Any is assignable from errorInComponent: false
// class C is assignable from errorInComponent: false
// Any is assignable from class C: true
// Cls's super type is Error type: true
// END
// FILE: a.kt
class C {
val errorAtTop = mutableMapOf<String, NonExistType>()
val errorInComponent: Map<String, NonExistType>
}

// FILE: Cls.java

public class Cls extends NonExistType {

}

0 comments on commit 37e2d07

Please sign in to comment.