Fix behavior of multiple methods in IrTypeSystemContext

Make sure implementations are the same as in ClassicTypeSystemContext
and ConeTypeSystemContext
This commit is contained in:
Alexander Udalov
2019-06-26 16:15:54 +02:00
parent 445c4ebe65
commit 3800d57691
3 changed files with 38 additions and 33 deletions
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
import org.jetbrains.kotlin.types.model.TypeArgumentMarker
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
class IrTypeCheckerContext(override val irBuiltIns: IrBuiltIns) : IrTypeSystemContext, AbstractTypeCheckerContext() {
@@ -60,21 +59,4 @@ class IrTypeCheckerContext(override val irBuiltIns: IrBuiltIns) : IrTypeSystemCo
override fun captureFromExpression(type: KotlinTypeMarker): KotlinTypeMarker? =
error("Captured type is unsupported in IR")
override fun SimpleTypeMarker.isPrimitiveType(): Boolean {
// TODO this is currently used in overload resolution only
return false
}
override fun KotlinTypeMarker.argumentsCount(): Int =
when (this) {
is IrSimpleType -> arguments.size
else -> 0
}
override fun KotlinTypeMarker.getArgument(index: Int): TypeArgumentMarker =
when (this) {
is IrSimpleType -> arguments[index]
else -> error("Type $this has no arguments")
}
}
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.ir.types
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
@@ -18,6 +19,7 @@ import org.jetbrains.kotlin.ir.types.impl.*
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.checker.convertVariance
import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.ir.types.isPrimitiveType as irTypePredicates_isPrimitiveType
interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesContext {
@@ -49,7 +51,7 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun SimpleTypeMarker.asDefinitelyNotNullType(): DefinitelyNotNullTypeMarker? = null
override fun SimpleTypeMarker.isMarkedNullable(): Boolean = (this as IrSimpleType).containsNull()
override fun SimpleTypeMarker.isMarkedNullable(): Boolean = (this as IrSimpleType).isMarkedNullable()
override fun SimpleTypeMarker.withNullability(nullable: Boolean): SimpleTypeMarker {
val simpleType = this as IrSimpleType
@@ -59,13 +61,26 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun SimpleTypeMarker.typeConstructor() = (this as IrSimpleType).classifier
override fun KotlinTypeMarker.argumentsCount(): Int =
when (this) {
is IrSimpleType -> arguments.size
else -> 0
}
override fun KotlinTypeMarker.getArgument(index: Int): TypeArgumentMarker =
when (this) {
is IrSimpleType -> arguments[index]
else -> error("Type $this has no arguments")
}
override fun KotlinTypeMarker.asTypeArgument() = this as IrTypeArgument
override fun CapturedTypeMarker.lowerType(): KotlinTypeMarker? = error("Captured Type is not valid for IrTypes")
override fun TypeArgumentMarker.isStarProjection() = this is IrStarProjection
override fun TypeArgumentMarker.getVariance() = (this as IrTypeProjection).variance.convertVariance()
override fun TypeArgumentMarker.getVariance(): TypeVariance =
(this as? IrTypeProjection)?.variance?.convertVariance() ?: TypeVariance.OUT
override fun TypeArgumentMarker.getType() = (this as IrTypeProjection).type
@@ -164,9 +179,11 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun SimpleTypeMarker.asArgumentList() = this as IrSimpleType
override fun TypeConstructorMarker.isAnyConstructor() = this === irBuiltIns.anyClass
override fun TypeConstructorMarker.isAnyConstructor(): Boolean =
this is IrClassSymbol && isClassWithFqName(KotlinBuiltIns.FQ_NAMES.any)
override fun TypeConstructorMarker.isNothingConstructor() = this === irBuiltIns.nothingClass
override fun TypeConstructorMarker.isNothingConstructor(): Boolean =
this is IrClassSymbol && isClassWithFqName(KotlinBuiltIns.FQ_NAMES.nothing)
override fun SimpleTypeMarker.isSingleClassifierType() = true
@@ -203,22 +220,31 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun KotlinTypeMarker.canHaveUndefinedNullability() = this is IrSimpleType && classifier is IrTypeParameterSymbol
override fun SimpleTypeMarker.typeDepth() = 2
override fun SimpleTypeMarker.typeDepth(): Int {
val maxInArguments = (this as IrSimpleType).arguments.asSequence().map {
if (it is IrStarProjection) 1 else it.getType().typeDepth()
}.max() ?: 0
return maxInArguments + 1
}
override fun findCommonIntegerLiteralTypesSuperType(explicitSupertypes: List<SimpleTypeMarker>): SimpleTypeMarker? =
irBuiltIns.intType as IrSimpleType
override fun KotlinTypeMarker.isNullableType() = this is IrDynamicType || this is IrSimpleType && hasQuestionMark
override fun KotlinTypeMarker.isNullableType(): Boolean =
this is IrType && isNullable()
@Suppress("UNCHECKED_CAST")
override fun intersectTypes(types: List<SimpleTypeMarker>): SimpleTypeMarker =
makeTypeIntersection(types as List<IrType>) as SimpleTypeMarker
@Suppress("UNCHECKED_CAST")
override fun intersectTypes(types: List<KotlinTypeMarker>): KotlinTypeMarker =
makeTypeIntersection(types as List<IrType>)
override fun SimpleTypeMarker.isPrimitiveType(): Boolean =
this is IrSimpleType && irTypePredicates_isPrimitiveType()
override fun createErrorTypeWithCustomConstructor(debugName: String, constructor: TypeConstructorMarker): KotlinTypeMarker =
TODO("IrTypeSystemContext doesn't support constraint system resolution")
}
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.resolve.DescriptorUtils.getFqName
@@ -19,10 +20,12 @@ private fun IrType.isNullableClassType(fqName: FqNameUnsafe) = isClassType(fqNam
private fun IrType.isClassType(fqName: FqNameUnsafe, hasQuestionMark: Boolean? = null): Boolean {
if (this !is IrSimpleType) return false
if (hasQuestionMark != null && this.hasQuestionMark != hasQuestionMark) return false
val classSymbol = this.classifier as? IrClassSymbol ?: return false
return classFqNameEquals(classSymbol, fqName)
return classifier.isClassWithFqName(fqName)
}
fun IrClassifierSymbol.isClassWithFqName(fqName: FqNameUnsafe): Boolean =
this is IrClassSymbol && classFqNameEquals(this, fqName)
private fun classFqNameEquals(symbol: IrClassSymbol, fqName: FqNameUnsafe): Boolean =
if (symbol.isBound) classFqNameEquals(symbol.owner, fqName) else classFqNameEquals(symbol.descriptor, fqName)
@@ -47,12 +50,6 @@ fun IrType.isPrimitiveType(): Boolean = KotlinBuiltIns.FQ_NAMES.fqNameToPrimitiv
fun IrType.isNullablePrimitiveType(): Boolean = KotlinBuiltIns.FQ_NAMES.fqNameToPrimitiveType.keys.any { isNullableClassType(it) }
fun IrType.isMarkedNullable() = (this as? IrSimpleType)?.hasQuestionMark ?: false
fun IrType.containsNull(): Boolean = when {
this is IrDynamicType -> true
this is IrStarProjection -> true
this is IrSimpleType -> hasQuestionMark || classifier.superTypes().any { it.containsNull() }
else -> false
}
fun IrType.isUnit() = isNotNullClassType(KotlinBuiltIns.FQ_NAMES.unit)
fun IrType.isNullableUnit() = isNullableClassType(KotlinBuiltIns.FQ_NAMES.unit)