Don't introduce @NotNull on Kotlin methods that already have that annotation

This usually can't happen, but delegates pointing at Java appear to be synthesised with this normally-hidden annotation
This commit is contained in:
Chris Smowton
2022-11-30 17:40:41 +00:00
parent c8e2ae8563
commit f5dc5155f9
7 changed files with 40 additions and 6 deletions

View File

@@ -1366,14 +1366,19 @@ open class KotlinFileExtractor(
if (t !is IrSimpleType)
return null
fun hasExistingAnnotation(name: FqName) =
existingAnnotations.any { existing -> existing.type.classFqName == name }
return if (declOrigin == IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB) {
// Java declaration: restore a NotNull or Nullable annotation if the original Java member had one but the Kotlin compiler removed it.
javaAnnotations?.mapNotNull { it.classId?.asSingleFqName() }
?.singleOrNull { NOT_NULL_ANNOTATIONS.contains(it) || NULLABLE_ANNOTATIONS.contains(it) }
?.takeUnless { existingAnnotations.any { existing -> existing.type.classFqName == it } }
?.takeUnless { hasExistingAnnotation(it) }
} else {
// Kotlin declaration: add a NotNull annotation to a non-nullable non-primitive type.
JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION.takeUnless { t.isNullable() || primitiveTypeMapping.getPrimitiveInfo(t) != null }
// Kotlin declaration: add a NotNull annotation to a non-nullable non-primitive type, unless one is already present.
// Usually Kotlin declarations can't have a manual `@NotNull`, but this happens at least when delegating members are
// synthesised and inherit the annotation from the delegate (which given it has @NotNull, is likely written in Java)
JvmAnnotationNames.JETBRAINS_NOT_NULL_ANNOTATION.takeUnless { t.isNullable() || primitiveTypeMapping.getPrimitiveInfo(t) != null || hasExistingAnnotation(it) }
}
}