[IR] Introduce IrDefinitelyNotNullType in IR

- support it in render
 - support in IR copy
 - KEEP-268
This commit is contained in:
Roman Artemev
2021-10-25 20:29:15 +03:00
committed by TeamCityServer
parent b48870c11f
commit 101afded69
6 changed files with 34 additions and 7 deletions
@@ -30,6 +30,10 @@ interface IrErrorType : IrType
interface IrDynamicType : IrType, DynamicTypeMarker interface IrDynamicType : IrType, DynamicTypeMarker
interface IrDefinitelyNotNullType : IrType, DefinitelyNotNullTypeMarker {
val original: IrType
}
interface IrSimpleType : IrType, SimpleTypeMarker, TypeArgumentListMarker { interface IrSimpleType : IrType, SimpleTypeMarker, TypeArgumentListMarker {
val classifier: IrClassifierSymbol val classifier: IrClassifierSymbol
val hasQuestionMark: Boolean val hasQuestionMark: Boolean
@@ -34,6 +34,7 @@ fun IrType.isSubtypeOf(superType: IrType, typeSystem: IrTypeSystemContext): Bool
fun IrType.isNullable(): Boolean = fun IrType.isNullable(): Boolean =
when (this) { when (this) {
is IrDefinitelyNotNullType -> false
is IrSimpleType -> when (val classifier = classifier) { is IrSimpleType -> when (val classifier = classifier) {
is IrClassSymbol -> hasQuestionMark is IrClassSymbol -> hasQuestionMark
is IrTypeParameterSymbol -> hasQuestionMark || classifier.owner.superTypes.any(IrType::isNullable) is IrTypeParameterSymbol -> hasQuestionMark || classifier.owner.superTypes.any(IrType::isNullable)
@@ -35,7 +35,7 @@ abstract class IrAbstractSimpleType(kotlinType: KotlinType?) : IrTypeBase(kotlin
arguments.hashCode() arguments.hashCode()
} }
abstract class IrDelegatedSimpleType : IrAbstractSimpleType(null) { abstract class IrDelegatedSimpleType(kotlinType: KotlinType? = null) : IrAbstractSimpleType(kotlinType) {
protected abstract val delegate: IrSimpleType protected abstract val delegate: IrSimpleType
@@ -51,6 +51,14 @@ abstract class IrDelegatedSimpleType : IrAbstractSimpleType(null) {
get() = delegate.annotations get() = delegate.annotations
} }
// TODO: This implementation is aligned with FE representation of DefinitelyNotNull (DNN) type but
// in fact DNN is special case of more general `IntersectionType`
// so someday it should be reconsidered
class IrDefinitelyNotNullTypeImpl(kotlinType: KotlinType?, original: IrType) : IrDelegatedSimpleType(kotlinType), IrDefinitelyNotNullType {
override val delegate: IrSimpleType = original as IrSimpleType
override val original: IrType
get() = delegate
}
class IrSimpleTypeImpl( class IrSimpleTypeImpl(
kotlinType: KotlinType?, kotlinType: KotlinType?,
@@ -123,6 +131,7 @@ class IrTypeProjectionImpl internal constructor(
fun makeTypeProjection(type: IrType, variance: Variance): IrTypeProjection = fun makeTypeProjection(type: IrType, variance: Variance): IrTypeProjection =
when { when {
type is IrCapturedType -> IrTypeProjectionImpl(type, variance) type is IrCapturedType -> IrTypeProjectionImpl(type, variance)
type is IrDefinitelyNotNullType -> IrTypeProjectionImpl(type, variance)
type is IrTypeProjection && type.variance == variance -> type type is IrTypeProjection && type.variance == variance -> type
type is IrSimpleType -> type.toBuilder().apply { this.variance = variance }.buildTypeProjection() type is IrSimpleType -> type.toBuilder().apply { this.variance = variance }.buildTypeProjection()
type is IrDynamicType -> IrDynamicTypeImpl(null, type.annotations, variance) type is IrDynamicType -> IrDynamicTypeImpl(null, type.annotations, variance)
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.impl.IrDefinitelyNotNullTypeImpl
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.types.impl.IrTypeAbbreviationImpl import org.jetbrains.kotlin.ir.types.impl.IrTypeAbbreviationImpl
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
@@ -26,11 +27,10 @@ class DeepCopyTypeRemapper(
// TODO // TODO
} }
override fun remapType(type: IrType): IrType = override fun remapType(type: IrType): IrType {
if (type !is IrSimpleType) return when (type) {
type is IrDefinitelyNotNullType -> IrDefinitelyNotNullTypeImpl(null, remapType(type.original))
else is IrSimpleType -> IrSimpleTypeImpl(
IrSimpleTypeImpl(
null, null,
symbolRemapper.getReferencedClassifier(type.classifier), symbolRemapper.getReferencedClassifier(type.classifier),
type.hasQuestionMark, type.hasQuestionMark,
@@ -38,6 +38,9 @@ class DeepCopyTypeRemapper(
type.annotations.map { it.transform(deepCopy, null) as IrConstructorCall }, type.annotations.map { it.transform(deepCopy, null) as IrConstructorCall },
type.abbreviation?.remapTypeAbbreviation() type.abbreviation?.remapTypeAbbreviation()
) )
else -> type
}
}
private fun remapTypeArgument(typeArgument: IrTypeArgument): IrTypeArgument = private fun remapTypeArgument(typeArgument: IrTypeArgument): IrTypeArgument =
if (typeArgument is IrTypeProjection) if (typeArgument is IrTypeProjection)
@@ -113,6 +113,8 @@ class RenderIrElementVisitor(private val normalizeNames: Boolean = false, privat
is IrErrorType -> "IrErrorType(${if (verboseErrorTypes) originalKotlinType else null})" is IrErrorType -> "IrErrorType(${if (verboseErrorTypes) originalKotlinType else null})"
is IrDefinitelyNotNullType -> "{${original.render()} & Any}"
is IrSimpleType -> buildTrimEnd { is IrSimpleType -> buildTrimEnd {
append(classifier.renderClassifierFqn()) append(classifier.renderClassifierFqn())
if (arguments.isNotEmpty()) { if (arguments.isNotEmpty()) {
@@ -6,7 +6,10 @@
package org.jetbrains.kotlin.ir.util package org.jetbrains.kotlin.ir.util
import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
@@ -415,6 +418,11 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
// TODO don't print `Any?` as upper bound? // TODO don't print `Any?` as upper bound?
printAnnotationsWithNoIndent() printAnnotationsWithNoIndent()
when (this) { when (this) {
is IrDefinitelyNotNullType -> {
p.printWithNoIndent("(")
original.printTypeWithNoIndent()
p.printWithNoIndent(" & Any)")
}
is IrSimpleType -> { is IrSimpleType -> {
// TODO abbreviation // TODO abbreviation