[FIR] Make captureFromTypeParameterUpperBoundIfNeeded smarter

Before this change `ARGUMENT_TYPE_MISMATCH` would complain that
`Y` "is not a subtype of" `Inv<Y>`, because the function would only
check immediate bounds of the type parameter `Y`. `chosenSupertype`
would be `X`, not `Inv<out kotlin/String>`.

^KT-60056
This commit is contained in:
Nikolay Lunyak
2023-11-23 13:01:39 +02:00
committed by Space Team
parent 5e1e8e8f61
commit 69fba8d33b
10 changed files with 56 additions and 59 deletions
@@ -668,11 +668,9 @@ internal fun captureFromTypeParameterUpperBoundIfNeeded(
): ConeKotlinType {
val expectedTypeClassId = expectedType.upperBoundIfFlexible().classId ?: return argumentType
val simplifiedArgumentType = argumentType.lowerBoundIfFlexible() as? ConeTypeParameterType ?: return argumentType
val typeParameter = simplifiedArgumentType.lookupTag.typeParameterSymbol.fir
val context = session.typeContext
val chosenSupertype = typeParameter.symbol.resolvedBounds.map { it.coneType }
val chosenSupertype = simplifiedArgumentType.collectUpperBounds()
.singleOrNull { it.hasSupertypeWithGivenClassId(expectedTypeClassId, context) } ?: return argumentType
val capturedType = context.captureFromExpression(chosenSupertype) ?: return argumentType