IR: support buildSimpleType for IrCapturedType

To make it possible to change nullability of IrCapturedType and change
its annotations, which is happening when determining overridability of
functions via IR.

 #KT-63437 Fixed
This commit is contained in:
Alexander Udalov
2023-11-14 15:00:01 +01:00
committed by Space Team
parent 8379e48e33
commit 471f25abfc
13 changed files with 135 additions and 16 deletions
@@ -262,7 +262,9 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
argument.type
} else null
IrCapturedType(status, lowerType, argument, typeParameters[index])
IrCapturedType(
status, lowerType, argument, typeParameters[index], SimpleTypeNullability.DEFINITELY_NOT_NULL, emptyList(), null
)
}
}
@@ -10,8 +10,10 @@ import org.jetbrains.kotlin.ir.symbols.FqNameEqualityChecker
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.model.CaptureStatus
import org.jetbrains.kotlin.utils.compactIfPossible
abstract class IrAbstractSimpleType(kotlinType: KotlinType?) : IrSimpleType(kotlinType) {
@@ -89,27 +91,60 @@ class IrSimpleTypeBuilder {
var arguments: List<IrTypeArgument> = emptyList()
var annotations: List<IrConstructorCall> = emptyList()
var abbreviation: IrTypeAbbreviation? = null
var captureStatus: CaptureStatus? = null
var capturedLowerType: IrType? = null
var capturedTypeConstructor: IrCapturedType.Constructor? = null
}
fun IrSimpleType.toBuilder() =
fun IrSimpleType.toBuilder(): IrSimpleTypeBuilder =
IrSimpleTypeBuilder().also { b ->
b.kotlinType = originalKotlinType
b.classifier = classifier
if (this is IrCapturedType) {
b.captureStatus = captureStatus
b.capturedLowerType = lowerType
b.capturedTypeConstructor = constructor
} else {
b.classifier = classifier
}
b.nullability = nullability
b.arguments = arguments
b.annotations = annotations
b.abbreviation = abbreviation
}
fun IrSimpleTypeBuilder.buildSimpleType() =
IrSimpleTypeImpl(
kotlinType,
classifier ?: throw AssertionError("Classifier not provided"),
nullability,
arguments.compactIfPossible(),
annotations.compactIfPossible(),
abbreviation
)
fun IrSimpleTypeBuilder.buildSimpleType(): IrSimpleType =
if (classifier == null) {
check(captureStatus != null && capturedTypeConstructor != null) {
"Neither classifier nor captured type constructor is provided"
}
check(arguments.isEmpty()) {
"Arguments should be empty when creating a captured type: ${capturedTypeConstructor?.argument?.render()}"
}
IrCapturedType(
captureStatus!!,
capturedLowerType,
capturedTypeConstructor!!.argument,
capturedTypeConstructor!!.typeParameter,
nullability,
annotations.compactIfPossible(),
abbreviation,
).apply {
constructor.initSuperTypes(capturedTypeConstructor!!.superTypes)
}
} else {
check(captureStatus == null && capturedTypeConstructor == null) {
"Both classifier and captured type constructor are provided"
}
IrSimpleTypeImpl(
kotlinType,
classifier ?: throw AssertionError("Classifier not provided"),
nullability,
arguments.compactIfPossible(),
annotations.compactIfPossible(),
abbreviation
)
}
fun IrSimpleTypeBuilder.buildTypeProjection(variance: Variance): IrTypeProjection =
if (variance == Variance.INVARIANT)
@@ -71,7 +71,10 @@ class IrCapturedType(
val captureStatus: CaptureStatus,
val lowerType: IrType?,
projection: IrTypeArgument,
typeParameter: IrTypeParameter
typeParameter: IrTypeParameter,
override val nullability: SimpleTypeNullability,
override val annotations: List<IrConstructorCall>,
override val abbreviation: IrTypeAbbreviation?,
) : IrSimpleType(null), CapturedTypeMarker {
class Constructor(val argument: IrTypeArgument, val typeParameter: IrTypeParameter) : CapturedTypeConstructorMarker {
var superTypes: List<IrType> = emptyList()
@@ -85,10 +88,8 @@ class IrCapturedType(
val constructor: Constructor = Constructor(projection, typeParameter)
override val classifier: IrClassifierSymbol get() = error("Captured Type does not have a classifier")
override val arguments: List<IrTypeArgument> get() = emptyList()
override val abbreviation: IrTypeAbbreviation? get() = null
override val nullability: SimpleTypeNullability get() = SimpleTypeNullability.DEFINITELY_NOT_NULL
override val annotations: List<IrConstructorCall> get() = emptyList()
override fun equals(other: Any?): Boolean = this === other