Skip to content

Commit 63d7e47

Browse files
committed
Fix invalid nullability annotation for suspend functions in LightClasses
1 parent d005dc6 commit 63d7e47

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaClassConstructorDescriptor
3636
import org.jetbrains.kotlin.psi.*
3737
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
3838
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
39+
import org.jetbrains.kotlin.psi.psiUtil.hasSuspendModifier
3940
import org.jetbrains.kotlin.resolve.BindingContext
4041
import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils
4142
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
@@ -83,10 +84,10 @@ abstract class KtLightAbstractAnnotation(parent: PsiElement, computeDelegate: ()
8384
}
8485

8586
class KtLightAnnotationForSourceEntry(
86-
private val qualifiedName: String,
87-
override val kotlinOrigin: KtCallElement,
88-
parent: PsiElement,
89-
computeDelegate: () -> PsiAnnotation
87+
private val qualifiedName: String,
88+
override val kotlinOrigin: KtCallElement,
89+
parent: PsiElement,
90+
computeDelegate: () -> PsiAnnotation
9091
) : KtLightAbstractAnnotation(parent, computeDelegate) {
9192

9293
override fun getQualifiedName() = qualifiedName
@@ -237,9 +238,9 @@ class KtLightEmptyAnnotationParameterList(parent: PsiElement) : KtLightElementBa
237238

238239
open class KtLightNullabilityAnnotation<D : KtLightElement<*, PsiModifierListOwner>>(val member: D, parent: PsiElement) :
239240
KtLightAbstractAnnotation(parent, {
240-
// searching for last because nullability annotations are generated after backend generates source annotations
241+
// searching for last because nullability annotations are generated after backend generates source annotations
241242
getClsNullabilityAnnotation(member) ?: KtLightNonExistentAnnotation(member)
242-
}) {
243+
}) {
243244
override fun fqNameMatches(fqName: String): Boolean {
244245
if (!isNullabilityAnnotation(fqName)) return false
245246

@@ -253,8 +254,8 @@ open class KtLightNullabilityAnnotation<D : KtLightElement<*, PsiModifierListOwn
253254

254255
override fun getQualifiedName(): String? {
255256
val annotatedElement = member.takeIf(::isFromSources)?.kotlinOrigin
256-
?: // it is out of our hands
257-
return getClsNullabilityAnnotation(member)?.qualifiedName
257+
?: // it is out of our hands
258+
return getClsNullabilityAnnotation(member)?.qualifiedName
258259

259260
// all data-class generated members are not-null
260261
if (annotatedElement is KtClass && annotatedElement.isData()) return NotNull::class.java.name
@@ -266,7 +267,12 @@ open class KtLightNullabilityAnnotation<D : KtLightElement<*, PsiModifierListOwn
266267
// don't annotate property setters
267268
if (annotatedElement is KtValVarKeywordOwner && member is KtLightMethod && member.returnType == PsiType.VOID) return null
268269

270+
if (annotatedElement is KtNamedFunction && annotatedElement.modifierList?.hasSuspendModifier() == true) {
271+
return Nullable::class.java.name
272+
}
273+
269274
val kotlinType = getTargetType(annotatedElement) ?: return null
275+
270276
if (KotlinBuiltIns.isPrimitiveType(kotlinType) && (annotatedElement as? KtParameter)?.isVarArg != true) {
271277
// no need to annotate them explicitly except the case when overriding reference-type makes it non-primitive for Jvm
272278
if (!(annotatedElement is KtCallableDeclaration && annotatedElement.hasModifier(KtTokens.OVERRIDE_KEYWORD))) return null

0 commit comments

Comments
 (0)