From 06b6477d044d10779f68189909ed76346e97f78c Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Wed, 5 Feb 2020 14:48:58 -0800 Subject: [PATCH] [JVM IR] Do not add nullability annotation for functions that return platform types. This behavior matches the non-IR backend, which was added in f16dcdd8a904c88eda90e9e5c620370dd1274298. --- .../kotlin/backend/jvm/codegen/AnnotationCodegen.kt | 12 ++++++++---- .../codegen/bytecodeListing/platformTypes.kt | 8 ++++++++ .../codegen/bytecodeListing/platformTypes.txt | 7 +++++++ .../kotlin/codegen/BytecodeListingTestGenerated.java | 5 +++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/bytecodeListing/platformTypes.kt create mode 100644 compiler/testData/codegen/bytecodeListing/platformTypes.txt 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 8f15a6c086b..4e4ee1fb36c 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 @@ -32,10 +32,8 @@ import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention 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.IrType -import org.jetbrains.kotlin.ir.types.classifierOrNull -import org.jetbrains.kotlin.ir.types.isMarkedNullable -import org.jetbrains.kotlin.ir.types.isNullable +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 @@ -45,6 +43,7 @@ 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 java.lang.annotation.RetentionPolicy @@ -153,6 +152,11 @@ abstract class AnnotationCodegen( return } + // A flexible type whose lower bound in not-null and upper bound is nullable, should not be annotated + if (type.toKotlinType().isNullabilityFlexible()) { + return + } + val annotationClass = if (type.isNullable()) Nullable::class.java else NotNull::class.java generateAnnotationIfNotPresent(annotationDescriptorsAlreadyPresent, annotationClass) diff --git a/compiler/testData/codegen/bytecodeListing/platformTypes.kt b/compiler/testData/codegen/bytecodeListing/platformTypes.kt new file mode 100644 index 00000000000..4fc6b8e00b7 --- /dev/null +++ b/compiler/testData/codegen/bytecodeListing/platformTypes.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME +import java.util.Collections + +class PlatformTypes { + fun simplyPlatform() = Collections.singletonList("")[0] + fun bothNullable() = Collections.emptyList() ?: null + fun bothNotNull() = Collections.emptyList()!! +} diff --git a/compiler/testData/codegen/bytecodeListing/platformTypes.txt b/compiler/testData/codegen/bytecodeListing/platformTypes.txt new file mode 100644 index 00000000000..b4308ab326d --- /dev/null +++ b/compiler/testData/codegen/bytecodeListing/platformTypes.txt @@ -0,0 +1,7 @@ +@kotlin.Metadata +public final class PlatformTypes { + public method (): void + public final @org.jetbrains.annotations.NotNull method bothNotNull(): java.util.List + public final @org.jetbrains.annotations.Nullable method bothNullable(): java.util.List + public final method simplyPlatform(): java.lang.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java index 1b980011637..983b2b85fad 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java @@ -94,6 +94,11 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest { runTest("compiler/testData/codegen/bytecodeListing/noToArrayInJava.kt"); } + @TestMetadata("platformTypes.kt") + public void testPlatformTypes() throws Exception { + runTest("compiler/testData/codegen/bytecodeListing/platformTypes.kt"); + } + @TestMetadata("privateDefaultSetter.kt") public void testPrivateDefaultSetter() throws Exception { runTest("compiler/testData/codegen/bytecodeListing/privateDefaultSetter.kt");