Don't report missing reflection diagnostic for KType/KTypeProjection/KVariance

This commit is contained in:
Alexander Udalov
2019-10-24 22:00:31 +02:00
parent 896512f7cd
commit 59959c52ad
4 changed files with 27 additions and 15 deletions
@@ -21,16 +21,25 @@ import org.jetbrains.kotlin.builtins.KOTLIN_REFLECT_FQ_NAME
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.util.OperatorNameConventions
private val ANY_MEMBER_NAMES = setOf("equals", "hashCode", "toString")
private val ALLOWED_CLASSES = setOf(
FqName("kotlin.reflect.KType"),
FqName("kotlin.reflect.KTypeProjection"),
FqName("kotlin.reflect.KTypeProjection.Companion"),
FqName("kotlin.reflect.KVariance")
)
/**
* Checks that there are no usages of reflection API which will fail at runtime.
*/
@@ -71,7 +80,8 @@ abstract class AbstractReflectionApiCallChecker(
name == OperatorNameConventions.INVOKE ||
name.asString() == "name" ||
DescriptorUtils.isSubclass(containingClass, kClass) && isAllowedKClassMember(descriptor.name) ||
(name.asString() == "get" || name.asString() == "set") && containingClass.isKPropertyClass()
(name.asString() == "get" || name.asString() == "set") && containingClass.isKPropertyClass() ||
containingClass.fqNameSafe in ALLOWED_CLASSES
}
private fun ClassDescriptor.isKPropertyClass() = kPropertyClasses.any { kProperty -> DescriptorUtils.isSubclass(this, kProperty) }
@@ -45,3 +45,18 @@ fun <T : Any> kclass(k: KClass<*>, kt: KClass<T>) {
k.hashCode()
k.toString()
}
fun ktype(t: KType, t2: KType) {
t.classifier
t.arguments
t.isMarkedNullable
t.annotations
t == t2
t.hashCode()
t.toString()
KTypeProjection.Companion.covariant(t)
KTypeProjection.STAR
KTypeProjection(KVariance.IN, t)
}
@@ -1,6 +1,7 @@
package
public fun </*0*/ T : kotlin.Any> kclass(/*0*/ k: kotlin.reflect.KClass<*>, /*1*/ kt: kotlin.reflect.KClass<T>): kotlin.Unit
public fun ktype(/*0*/ t: kotlin.reflect.KType, /*1*/ t2: kotlin.reflect.KType): kotlin.Unit
public fun n01(): kotlin.reflect.KProperty1<Foo, kotlin.Any>
public fun n02(): kotlin.reflect.KFunction1<Foo, kotlin.Unit>
public fun n03(): kotlin.reflect.KClass<Foo>
@@ -18,22 +18,11 @@ package org.jetbrains.kotlin.js.resolve.diagnostics
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.diagnostics.Errors.UNSUPPORTED
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.calls.checkers.AbstractReflectionApiCallChecker
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.storage.StorageManager
private val ALLOWED_CLASSES = setOf(
FqName("kotlin.reflect.KType"),
FqName("kotlin.reflect.KTypeProjection"),
FqName("kotlin.reflect.KTypeProjection.Companion"),
FqName("kotlin.reflect.KVariance")
)
class JsReflectionAPICallChecker(
reflectionTypes: ReflectionTypes,
storageManager: StorageManager
@@ -44,7 +33,4 @@ class JsReflectionAPICallChecker(
override fun report(element: PsiElement, context: CallCheckerContext) {
context.trace.report(UNSUPPORTED.on(element, "This reflection API is not supported yet in JavaScript"))
}
override fun isAllowedReflectionApi(descriptor: CallableDescriptor, containingClass: ClassDescriptor): Boolean =
super.isAllowedReflectionApi(descriptor, containingClass) || containingClass.fqNameSafe in ALLOWED_CLASSES
}