K2: Imitate K1 behavior for case of captured types with a raw supertype

See the comments in the code, but mostly the motivation is that once
it was decided to stick with such a legacy thing as raw types,
we are ok with some corner-cases hacks for them
(if there are not too many of them) and they don't break anything
when there are no raw types in the code.

^KT-56616 Fixed
This commit is contained in:
Denis.Zharkov
2023-02-22 13:45:53 +01:00
committed by Space Team
parent eb09a25239
commit 608cb01935
14 changed files with 172 additions and 44 deletions
@@ -297,6 +297,18 @@ abstract class AbstractTypeApproximator(
type.isMarkedNullable() -> baseResult.withNullability(true)
type.isProjectionNotNull() -> baseResult.withNullability(false)
else -> baseResult
}.let {
when {
// This is just a hack that is necessary to preserve compatibility with K1 where return type of the calls
// if they contain a captured types with RAW supertype would be approximated to a regular non-raw flexible type
// See CapturedTypeApproximationKt.approximateCapturedTypes and especially the comment
// "// tod*: dynamic & raw type?" before it :)
// If we don't repeat that behavior, we would stumble upon KT-56616 with hardly having any workarounds.
isK2 && conf.convertToNonRawVersionAfterApproximationInK2 && it.isRawType() -> {
it.convertToNonRaw()
}
else -> it
}
}
}
@@ -26,6 +26,12 @@ open class TypeApproximatorConfiguration {
open val intersectionTypesInContravariantPositions = false
open val localTypes = false
/**
* Is only expected to be true for FinalApproximationAfterResolutionAndInference
* But it's only used for K2 to reproduce K1 behavior for the approximation of resolved calls
*/
open val convertToNonRawVersionAfterApproximationInK2 get() = false
/**
* Whether to approximate anonymous type. This flag does not have any effect if `localTypes` is true because all anonymous types are
* local.
@@ -88,6 +94,8 @@ open class TypeApproximatorConfiguration {
AbstractCapturedTypesApproximation(CaptureStatus.FROM_EXPRESSION) {
override val integerLiteralConstantType: Boolean get() = true
override val intersectionTypesInContravariantPositions: Boolean get() = true
override val convertToNonRawVersionAfterApproximationInK2: Boolean get() = true
}
object TypeArgumentApproximation : AbstractCapturedTypesApproximation(null) {