Fix invalid nullability annotation for suspend functions in LightClasses
This commit is contained in:
+14
-8
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaClassConstructorDescriptor
|
|||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.hasSuspendModifier
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils
|
import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||||
@@ -83,10 +84,10 @@ abstract class KtLightAbstractAnnotation(parent: PsiElement, computeDelegate: ()
|
|||||||
}
|
}
|
||||||
|
|
||||||
class KtLightAnnotationForSourceEntry(
|
class KtLightAnnotationForSourceEntry(
|
||||||
private val qualifiedName: String,
|
private val qualifiedName: String,
|
||||||
override val kotlinOrigin: KtCallElement,
|
override val kotlinOrigin: KtCallElement,
|
||||||
parent: PsiElement,
|
parent: PsiElement,
|
||||||
computeDelegate: () -> PsiAnnotation
|
computeDelegate: () -> PsiAnnotation
|
||||||
) : KtLightAbstractAnnotation(parent, computeDelegate) {
|
) : KtLightAbstractAnnotation(parent, computeDelegate) {
|
||||||
|
|
||||||
override fun getQualifiedName() = qualifiedName
|
override fun getQualifiedName() = qualifiedName
|
||||||
@@ -237,9 +238,9 @@ class KtLightEmptyAnnotationParameterList(parent: PsiElement) : KtLightElementBa
|
|||||||
|
|
||||||
open class KtLightNullabilityAnnotation<D : KtLightElement<*, PsiModifierListOwner>>(val member: D, parent: PsiElement) :
|
open class KtLightNullabilityAnnotation<D : KtLightElement<*, PsiModifierListOwner>>(val member: D, parent: PsiElement) :
|
||||||
KtLightAbstractAnnotation(parent, {
|
KtLightAbstractAnnotation(parent, {
|
||||||
// searching for last because nullability annotations are generated after backend generates source annotations
|
// searching for last because nullability annotations are generated after backend generates source annotations
|
||||||
getClsNullabilityAnnotation(member) ?: KtLightNonExistentAnnotation(member)
|
getClsNullabilityAnnotation(member) ?: KtLightNonExistentAnnotation(member)
|
||||||
}) {
|
}) {
|
||||||
override fun fqNameMatches(fqName: String): Boolean {
|
override fun fqNameMatches(fqName: String): Boolean {
|
||||||
if (!isNullabilityAnnotation(fqName)) return false
|
if (!isNullabilityAnnotation(fqName)) return false
|
||||||
|
|
||||||
@@ -253,8 +254,8 @@ open class KtLightNullabilityAnnotation<D : KtLightElement<*, PsiModifierListOwn
|
|||||||
|
|
||||||
override fun getQualifiedName(): String? {
|
override fun getQualifiedName(): String? {
|
||||||
val annotatedElement = member.takeIf(::isFromSources)?.kotlinOrigin
|
val annotatedElement = member.takeIf(::isFromSources)?.kotlinOrigin
|
||||||
?: // it is out of our hands
|
?: // it is out of our hands
|
||||||
return getClsNullabilityAnnotation(member)?.qualifiedName
|
return getClsNullabilityAnnotation(member)?.qualifiedName
|
||||||
|
|
||||||
// all data-class generated members are not-null
|
// all data-class generated members are not-null
|
||||||
if (annotatedElement is KtClass && annotatedElement.isData()) return NotNull::class.java.name
|
if (annotatedElement is KtClass && annotatedElement.isData()) return NotNull::class.java.name
|
||||||
@@ -266,7 +267,12 @@ open class KtLightNullabilityAnnotation<D : KtLightElement<*, PsiModifierListOwn
|
|||||||
// don't annotate property setters
|
// don't annotate property setters
|
||||||
if (annotatedElement is KtValVarKeywordOwner && member is KtLightMethod && member.returnType == PsiType.VOID) return null
|
if (annotatedElement is KtValVarKeywordOwner && member is KtLightMethod && member.returnType == PsiType.VOID) return null
|
||||||
|
|
||||||
|
if (annotatedElement is KtNamedFunction && annotatedElement.modifierList?.hasSuspendModifier() == true) {
|
||||||
|
return Nullable::class.java.name
|
||||||
|
}
|
||||||
|
|
||||||
val kotlinType = getTargetType(annotatedElement) ?: return null
|
val kotlinType = getTargetType(annotatedElement) ?: return null
|
||||||
|
|
||||||
if (KotlinBuiltIns.isPrimitiveType(kotlinType) && (annotatedElement as? KtParameter)?.isVarArg != true) {
|
if (KotlinBuiltIns.isPrimitiveType(kotlinType) && (annotatedElement as? KtParameter)?.isVarArg != true) {
|
||||||
// no need to annotate them explicitly except the case when overriding reference-type makes it non-primitive for Jvm
|
// no need to annotate them explicitly except the case when overriding reference-type makes it non-primitive for Jvm
|
||||||
if (!(annotatedElement is KtCallableDeclaration && annotatedElement.hasModifier(KtTokens.OVERRIDE_KEYWORD))) return null
|
if (!(annotatedElement is KtCallableDeclaration && annotatedElement.hasModifier(KtTokens.OVERRIDE_KEYWORD))) return null
|
||||||
|
|||||||
Reference in New Issue
Block a user