JVM, JVM_IR: no nullability annotations on lateinit backing fields

This commit is contained in:
Dmitry Petrov
2020-06-26 10:49:51 +03:00
parent e822e871f5
commit 64e47265e1
3 changed files with 10 additions and 5 deletions
@@ -438,8 +438,13 @@ public class PropertyCodegen {
);
if (annotatedField != null) {
boolean isInvisibleField = (modifiers & ACC_PRIVATE) != 0 || (modifiers & ACC_SYNTHETIC) != 0;
AnnotationCodegen.forField(fv, memberCodegen, state, isInvisibleField)
// Don't emit nullability annotations for backing field if:
// - backing field is invisible from Java (private or synthetic);
// - property is lateinit (since corresponding field is actually nullable).
boolean skipNullabilityAnnotations =
(modifiers & ACC_PRIVATE) != 0 || (modifiers & ACC_SYNTHETIC) != 0 ||
propertyDescriptor.isLateInit();
AnnotationCodegen.forField(fv, memberCodegen, state, skipNullabilityAnnotations)
.genAnnotations(annotatedField, type, propertyDescriptor.getType());
}
}
@@ -141,8 +141,8 @@ abstract class AnnotationCodegen(
is IrValueDeclaration -> declaration.type
is IrField ->
if (declaration.correspondingPropertySymbol?.owner?.isLateinit == true) {
// Lateinit fields are nullable, but should be marked as NotNull to match the JVM backend.
declaration.type.makeNotNull()
// Don't generate nullability annotations on lateinit fields
return
} else {
declaration.type
}
@@ -1,6 +1,6 @@
@kotlin.Metadata
public final class A {
public @org.jetbrains.annotations.NotNull field x: A
public field x: A
public method <init>(): void
public final @org.jetbrains.annotations.NotNull method getX(): A
public final method setX(@org.jetbrains.annotations.NotNull p0: A): void