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) { if (annotatedField != null) {
boolean isInvisibleField = (modifiers & ACC_PRIVATE) != 0 || (modifiers & ACC_SYNTHETIC) != 0; // Don't emit nullability annotations for backing field if:
AnnotationCodegen.forField(fv, memberCodegen, state, isInvisibleField) // - 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()); .genAnnotations(annotatedField, type, propertyDescriptor.getType());
} }
} }
@@ -141,8 +141,8 @@ abstract class AnnotationCodegen(
is IrValueDeclaration -> declaration.type is IrValueDeclaration -> declaration.type
is IrField -> is IrField ->
if (declaration.correspondingPropertySymbol?.owner?.isLateinit == true) { if (declaration.correspondingPropertySymbol?.owner?.isLateinit == true) {
// Lateinit fields are nullable, but should be marked as NotNull to match the JVM backend. // Don't generate nullability annotations on lateinit fields
declaration.type.makeNotNull() return
} else { } else {
declaration.type declaration.type
} }
@@ -1,6 +1,6 @@
@kotlin.Metadata @kotlin.Metadata
public final class A { public final class A {
public @org.jetbrains.annotations.NotNull field x: A public field x: A
public method <init>(): void public method <init>(): void
public final @org.jetbrains.annotations.NotNull method getX(): A public final @org.jetbrains.annotations.NotNull method getX(): A
public final method setX(@org.jetbrains.annotations.NotNull p0: A): void public final method setX(@org.jetbrains.annotations.NotNull p0: A): void