Fir2Ir: deal with flexible and raw types in FirJvmMangleComputer and type converter

This commit is contained in:
Georgy Bronnikov
2022-02-24 22:57:12 +03:00
committed by Alexander Udalov
parent ad625a4631
commit e7b1cc03de
2 changed files with 32 additions and 6 deletions
@@ -255,10 +255,23 @@ open class FirJvmMangleComputer(
tBuilder.appendSignature(MangleConstant.ENHANCED_NULLABILITY_MARK) tBuilder.appendSignature(MangleConstant.ENHANCED_NULLABILITY_MARK)
} }
} }
is ConeRawType -> {
mangleType(tBuilder, type.lowerBound)
}
is ConeFlexibleType -> { is ConeFlexibleType -> {
// Need to reproduce type approximation done for flexible types in TypeTranslator. with(session.typeContext) {
// For now, we replicate the current behaviour of Fir2IrTypeConverter and just take the upper bound // Need to reproduce type approximation done for flexible types in TypeTranslator.
mangleType(tBuilder, type.upperBound) // For now, we replicate the current behaviour of Fir2IrTypeConverter and just take the upper bound
val upper = type.upperBound
if (upper is ConeClassLikeType) {
val lower = type.lowerBound as? ConeClassLikeType ?: error("Expecting class-like type, got ${type.lowerBound}")
val intermediate = if (lower.lookupTag == upper.lookupTag) {
lower.replaceArguments(upper.getArguments())
} else lower
val mixed = if (upper.isNullable) intermediate.makeNullable() else intermediate.makeDefinitelyNotNullOrNotNull()
mangleType(tBuilder, mixed as ConeKotlinType)
} else mangleType(tBuilder, upper)
}
} }
is ConeDefinitelyNotNullType -> { is ConeDefinitelyNotNullType -> {
// E.g. not-null type parameter in Java // E.g. not-null type parameter in Java
@@ -163,13 +163,26 @@ class Fir2IrTypeConverter(
// (some reflection tests rely on this) // (some reflection tests rely on this)
lowerBound.toIrType( lowerBound.toIrType(
typeContext, typeContext,
annotations,
hasFlexibleNullability = lowerBound.nullability != upperBound.nullability, hasFlexibleNullability = lowerBound.nullability != upperBound.nullability,
addRawTypeAnnotation = true addRawTypeAnnotation = true
) )
} }
is ConeFlexibleType -> { is ConeFlexibleType -> with(session.typeContext) {
// TODO: yet we take more general type. Not quite sure it's Ok if (upperBound is ConeClassLikeType) {
upperBound.toIrType(typeContext, hasFlexibleNullability = lowerBound.nullability != upperBound.nullability) val upper = upperBound as ConeClassLikeType
val lower = lowerBound as? ConeClassLikeType ?: error("Expecting class-like type, got $lowerBound")
val intermediate = if (lower.lookupTag == upper.lookupTag) {
lower.replaceArguments(upper.getArguments())
} else lower
(intermediate.withNullability(upper.isNullable) as ConeKotlinType)
.withAttributes(lower.attributes)
.toIrType(
typeContext, annotations, hasFlexibleNullability = lower.nullability != upper.nullability
)
} else {
upperBound.toIrType(typeContext, annotations, hasFlexibleNullability = lowerBound.nullability != upperBound.nullability)
}
} }
is ConeCapturedType -> { is ConeCapturedType -> {
val cached = capturedTypeCache[this] val cached = capturedTypeCache[this]