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
@@ -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
}