JVM IR: Mark lateinit fields as NotNull

This is needed for compatibility with the JVM backend.
This commit is contained in:
Steven Schäfer
2020-06-02 12:02:29 +02:00
committed by Alexander Udalov
parent 52b29b53bc
commit 3291cf7a6e
5 changed files with 32 additions and 6 deletions
@@ -33,19 +33,17 @@ import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.load.kotlin.FileBasedKotlinClass
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
import org.jetbrains.kotlin.synthetic.isVisibleOutside
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.isNullabilityFlexible
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.org.objectweb.asm.*
import org.jetbrains.org.objectweb.asm.AnnotationVisitor
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.TypePath
import java.lang.annotation.RetentionPolicy
abstract class AnnotationCodegen(
@@ -139,8 +137,14 @@ abstract class AnnotationCodegen(
val type = when (declaration) {
is IrFunction -> declaration.returnType
is IrField -> declaration.type
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()
} else {
declaration.type
}
else -> return
}
@@ -0,0 +1,5 @@
// Test to ensure that we mark the backing field of a lateinit property
// as NotNull, even though the field is nullable in the JVM IR backend.
class A {
lateinit var x: A
}
@@ -0,0 +1,7 @@
@kotlin.Metadata
public final class A {
public @org.jetbrains.annotations.NotNull 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
}
@@ -74,6 +74,11 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest {
runTest("compiler/testData/codegen/bytecodeListing/jvmStaticWithDefaultParameters.kt");
}
@TestMetadata("lateInitNotNull.kt")
public void testLateInitNotNull() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/lateInitNotNull.kt");
}
@TestMetadata("noCollectionStubMethodsInInterface.kt")
public void testNoCollectionStubMethodsInInterface() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/noCollectionStubMethodsInInterface.kt");
@@ -74,6 +74,11 @@ public class IrBytecodeListingTestGenerated extends AbstractIrBytecodeListingTes
runTest("compiler/testData/codegen/bytecodeListing/jvmStaticWithDefaultParameters.kt");
}
@TestMetadata("lateInitNotNull.kt")
public void testLateInitNotNull() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/lateInitNotNull.kt");
}
@TestMetadata("noCollectionStubMethodsInInterface.kt")
public void testNoCollectionStubMethodsInInterface() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/noCollectionStubMethodsInInterface.kt");