IR KT-44233 support flexible nullability in IrTypeSystemContext

^KT-44233 Fixed Target versions 1.5-M1
This commit is contained in:
Dmitry Petrov
2021-01-11 13:43:01 +03:00
committed by TeamCityServer
parent 093f62caac
commit b02a9846d0
13 changed files with 163 additions and 16 deletions
@@ -5068,6 +5068,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/collections/kt41123.kt");
}
@TestMetadata("kt44233.kt")
public void testKt44233() throws Exception {
runTest("compiler/testData/codegen/box/collections/kt44233.kt");
}
@TestMetadata("mutableList.kt")
public void testMutableList() throws Exception {
runTest("compiler/testData/codegen/box/collections/mutableList.kt");
@@ -33,7 +33,16 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun KotlinTypeMarker.asSimpleType() = this as? SimpleTypeMarker
override fun KotlinTypeMarker.asFlexibleType() = this as? IrDynamicType
override fun KotlinTypeMarker.asFlexibleType(): FlexibleTypeMarker? {
if (this is FlexibleTypeMarker) return this
if (this is IrType) {
val jvmFlexibleType = this.asJvmFlexibleType()
if (jvmFlexibleType != null) return jvmFlexibleType
}
return null
}
override fun KotlinTypeMarker.isError() = this is IrErrorType
@@ -44,13 +53,19 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun FlexibleTypeMarker.asRawType(): RawTypeMarker? = null
override fun FlexibleTypeMarker.upperBound(): SimpleTypeMarker {
require(this is IrDynamicType)
return irBuiltIns.anyNType as IrSimpleType
return when (this) {
is IrDynamicType -> irBuiltIns.anyNType as IrSimpleType
is IrJvmFlexibleType -> this.upperBound
else -> error("Unexpected flexible type ${this::class.java.simpleName}: $this")
}
}
override fun FlexibleTypeMarker.lowerBound(): SimpleTypeMarker {
require(this is IrDynamicType)
return irBuiltIns.nothingType as IrSimpleType
return when (this) {
is IrDynamicType -> irBuiltIns.nothingType as IrSimpleType
is IrJvmFlexibleType -> this.lowerBound
else -> error("Unexpected flexible type ${this::class.java.simpleName}: $this")
}
}
override fun SimpleTypeMarker.asCapturedType(): CapturedTypeMarker? = null
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.isAnonymousObject
import org.jetbrains.kotlin.ir.util.isPropertyAccessor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
import org.jetbrains.kotlin.types.typeUtil.makeNullable
@@ -30,19 +29,21 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
fun IrType.withHasQuestionMark(newHasQuestionMark: Boolean): IrType =
when (this) {
is IrSimpleType ->
if (this.hasQuestionMark == newHasQuestionMark)
this
else
buildSimpleType {
hasQuestionMark = newHasQuestionMark
kotlinType = originalKotlinType?.run {
if (newHasQuestionMark) makeNullable() else makeNotNullable()
}
}
is IrSimpleType -> withHasQuestionMark(newHasQuestionMark)
else -> this
}
fun IrSimpleType.withHasQuestionMark(newHasQuestionMark: Boolean): IrSimpleType =
if (this.hasQuestionMark == newHasQuestionMark)
this
else
buildSimpleType {
hasQuestionMark = newHasQuestionMark
kotlinType = originalKotlinType?.run {
if (newHasQuestionMark) makeNullable() else makeNotNullable()
}
}
fun IrType.addAnnotations(newAnnotations: List<IrConstructorCall>): IrType =
if (newAnnotations.isEmpty())
this
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.types
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.model.FlexibleTypeMarker
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
internal interface IrJvmFlexibleType : FlexibleTypeMarker {
val lowerBound: SimpleTypeMarker
val upperBound: SimpleTypeMarker
}
internal class IrJvmFlexibleNullabilityType(val irType: IrSimpleType) : IrJvmFlexibleType {
override val lowerBound get() = irType.withHasQuestionMark(false)
override val upperBound get() = irType.withHasQuestionMark(true)
}
internal val FLEXIBLE_NULLABILITY_FQN = FqName("kotlin.internal.ir").child(Name.identifier("FlexibleNullability"))
internal fun IrType.isWithFlexibleNullability() =
hasAnnotation(FLEXIBLE_NULLABILITY_FQN)
internal fun IrType.asJvmFlexibleType() =
when {
this is IrSimpleType && isWithFlexibleNullability() ->
IrJvmFlexibleNullabilityType(
this.removeAnnotations { irCtorCall ->
irCtorCall.type.classFqName == FLEXIBLE_NULLABILITY_FQN
} as IrSimpleType
)
else ->
null
}
+18
View File
@@ -0,0 +1,18 @@
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// FULL_JDK
import java.util.concurrent.ConcurrentSkipListSet
class StringIterable : Iterable<String> {
private val strings = ConcurrentSkipListSet<String>()
override fun iterator() = strings.iterator()
}
fun box(): String {
val si = StringIterable()
return if (si.iterator().hasNext())
"Failed"
else
"OK"
}
@@ -0,0 +1,14 @@
// IGNORE_BACKEND_FIR: JVM_IR
// FULL_JDK
import java.util.concurrent.*
class Test1 : Iterable<String> {
private val received = ConcurrentSkipListSet<String>()
override fun iterator() = received.iterator()
}
class Test2 : Iterable<String> {
private val received = Array<String>(0) { "" }
override fun iterator() = received.iterator()
}
@@ -0,0 +1,15 @@
@kotlin.Metadata
public final class Test1 {
// source: 'kt44233.kt'
private final field received: java.util.concurrent.ConcurrentSkipListSet
public method <init>(): void
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
}
@kotlin.Metadata
public final class Test2 {
// source: 'kt44233.kt'
private final field received: java.lang.String[]
public method <init>(): void
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
}
@@ -0,0 +1,15 @@
@kotlin.Metadata
public final class Test1 {
// source: 'kt44233.kt'
private final @org.jetbrains.annotations.NotNull field received: java.util.concurrent.ConcurrentSkipListSet
public method <init>(): void
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
}
@kotlin.Metadata
public final class Test2 {
// source: 'kt44233.kt'
private final @org.jetbrains.annotations.NotNull field received: java.lang.String[]
public method <init>(): void
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
}
@@ -5068,6 +5068,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/collections/kt41123.kt");
}
@TestMetadata("kt44233.kt")
public void testKt44233() throws Exception {
runTest("compiler/testData/codegen/box/collections/kt44233.kt");
}
@TestMetadata("mutableList.kt")
public void testMutableList() throws Exception {
runTest("compiler/testData/codegen/box/collections/mutableList.kt");
@@ -410,6 +410,11 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.kt");
}
@TestMetadata("kt44233.kt")
public void testKt44233() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/kt44233.kt");
}
@TestMetadata("mapOfPrimitivesFullJdk.kt")
public void testMapOfPrimitivesFullJdk() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/mapOfPrimitivesFullJdk.kt");
@@ -5068,6 +5068,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/collections/kt41123.kt");
}
@TestMetadata("kt44233.kt")
public void testKt44233() throws Exception {
runTest("compiler/testData/codegen/box/collections/kt44233.kt");
}
@TestMetadata("mutableList.kt")
public void testMutableList() throws Exception {
runTest("compiler/testData/codegen/box/collections/mutableList.kt");
@@ -5068,6 +5068,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/collections/kt41123.kt");
}
@TestMetadata("kt44233.kt")
public void testKt44233() throws Exception {
runTest("compiler/testData/codegen/box/collections/kt44233.kt");
}
@TestMetadata("mutableList.kt")
public void testMutableList() throws Exception {
runTest("compiler/testData/codegen/box/collections/mutableList.kt");
@@ -410,6 +410,11 @@ public class IrBytecodeListingTestGenerated extends AbstractIrBytecodeListingTes
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/inheritingFromAbstractMutableList.kt");
}
@TestMetadata("kt44233.kt")
public void testKt44233() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/kt44233.kt");
}
@TestMetadata("mapOfPrimitivesFullJdk.kt")
public void testMapOfPrimitivesFullJdk() throws Exception {
runTest("compiler/testData/codegen/bytecodeListing/collectionStubs/mapOfPrimitivesFullJdk.kt");