JVM_IR KT-43043 fix nullability annotations for inline class members

This commit is contained in:
Dmitry Petrov
2020-12-16 11:58:46 +03:00
parent 2b3fc330ad
commit 7ed3860c70
9 changed files with 77 additions and 126 deletions
@@ -57,7 +57,11 @@ abstract class AnnotationCodegen(
/**
* @param returnType can be null if not applicable (e.g. [annotated] is a class)
*/
fun genAnnotations(annotated: IrAnnotationContainer?, returnType: Type?, typeForTypeAnnotations: IrType?) {
fun genAnnotations(
annotated: IrAnnotationContainer?,
returnType: Type?,
typeForTypeAnnotations: IrType?
) {
if (annotated == null) return
val annotationDescriptorsAlreadyPresent = mutableSetOf<String>()
@@ -94,7 +98,10 @@ abstract class AnnotationCodegen(
}
}
generateAdditionalAnnotations(annotated, returnType, annotationDescriptorsAlreadyPresent)
if (!skipNullabilityAnnotations && annotated is IrDeclaration && returnType != null && !AsmUtil.isPrimitive(returnType)) {
generateNullabilityAnnotationForCallable(annotated, annotationDescriptorsAlreadyPresent)
}
generateTypeAnnotations(annotated, typeForTypeAnnotations)
}
@@ -109,16 +116,6 @@ abstract class AnnotationCodegen(
}
private fun generateAdditionalAnnotations(
annotated: IrAnnotationContainer,
returnType: Type?,
annotationDescriptorsAlreadyPresent: MutableSet<String>
) {
if (!skipNullabilityAnnotations && annotated is IrDeclaration && returnType != null && !AsmUtil.isPrimitive(returnType)) {
generateNullabilityAnnotationForCallable(annotated, annotationDescriptorsAlreadyPresent)
}
}
private fun generateNullabilityAnnotationForCallable(
declaration: IrDeclaration, // There is no superclass that encompasses IrFunction, IrField and nothing else.
annotationDescriptorsAlreadyPresent: MutableSet<String>
@@ -127,6 +124,7 @@ abstract class AnnotationCodegen(
if (declaration is IrValueParameter) {
val parent = declaration.parent as IrDeclaration
if (isInvisibleForNullabilityAnalysis(parent)) return
if (isMovedReceiverParameterOfStaticInlineClassReplacement(declaration, parent)) return
}
// No need to annotate annotation methods since they're always non-null
@@ -167,6 +165,10 @@ abstract class AnnotationCodegen(
generateAnnotationIfNotPresent(annotationDescriptorsAlreadyPresent, annotationClass)
}
private fun isMovedReceiverParameterOfStaticInlineClassReplacement(parameter: IrValueParameter, parent: IrDeclaration): Boolean =
parent.origin == JvmLoweredDeclarationOrigin.STATIC_INLINE_CLASS_REPLACEMENT &&
parameter.origin == IrDeclarationOrigin.MOVED_DISPATCH_RECEIVER
private fun generateAnnotationIfNotPresent(annotationDescriptorsAlreadyPresent: MutableSet<String>, annotationClass: Class<*>) {
val descriptor = Type.getType(annotationClass).descriptor
if (!annotationDescriptorsAlreadyPresent.contains(descriptor)) {