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)
}
}
is ConeRawType -> {
mangleType(tBuilder, type.lowerBound)
}
is ConeFlexibleType -> {
// Need to reproduce type approximation done for flexible types in TypeTranslator.
// For now, we replicate the current behaviour of Fir2IrTypeConverter and just take the upper bound
mangleType(tBuilder, type.upperBound)
with(session.typeContext) {
// Need to reproduce type approximation done for flexible types in TypeTranslator.
// 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 -> {
// E.g. not-null type parameter in Java
@@ -163,13 +163,26 @@ class Fir2IrTypeConverter(
// (some reflection tests rely on this)
lowerBound.toIrType(
typeContext,
annotations,
hasFlexibleNullability = lowerBound.nullability != upperBound.nullability,
addRawTypeAnnotation = true
)
}
is ConeFlexibleType -> {
// TODO: yet we take more general type. Not quite sure it's Ok
upperBound.toIrType(typeContext, hasFlexibleNullability = lowerBound.nullability != upperBound.nullability)
is ConeFlexibleType -> with(session.typeContext) {
if (upperBound is ConeClassLikeType) {
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 -> {
val cached = capturedTypeCache[this]