diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java index 1378d0f82a8..9e2bc1ce15e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/PropertyCodegen.java @@ -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()); } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/AnnotationCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/AnnotationCodegen.kt index a1a53bc87fa..51673d9c595 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/AnnotationCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/AnnotationCodegen.kt @@ -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 } diff --git a/compiler/testData/codegen/bytecodeListing/lateInitNotNull.txt b/compiler/testData/codegen/bytecodeListing/lateInitNotNull.txt index 630374a13a9..3eef66d87a7 100644 --- a/compiler/testData/codegen/bytecodeListing/lateInitNotNull.txt +++ b/compiler/testData/codegen/bytecodeListing/lateInitNotNull.txt @@ -1,6 +1,6 @@ @kotlin.Metadata public final class A { - public @org.jetbrains.annotations.NotNull field x: A + public field x: A public method (): void public final @org.jetbrains.annotations.NotNull method getX(): A public final method setX(@org.jetbrains.annotations.NotNull p0: A): void