[FIR] Fix capturing of flexible types during resolution
Previously, because we didn't handle flexible types properly in prepareCapturedType, projections inside flexible types would only be captured during subtyping with captureStatus=FOR_SUBTYPING which would lead to the constraint type being wrongly approximated (see ConstraintInjector.TypeCheckerStateForConstraintInjector .addNewIncorporatedConstraint). Fixing the capturing produced two kinds of false positive diagnostics: 1. In ConstraintInjector.TypeCheckerStateForConstraintInjector .addNewIncorporatedConstraint we would get two instances of cone types that are structurally equal and containing the same captured type. However, because we only skipped subtyping if the types were referentially equal, we would get a contradiction here. The fix was to use structural equality instead, which should be okay as the captured type instances are the same. 2. Reified type variables were inferred to captured types because flexible arrays with captured upper bounds (Array<Foo>..Array<Captured(out Foo)>?) were not properly approximated. #KT-62609 Fixed
This commit is contained in:
committed by
Space Team
parent
ed4941d9f9
commit
560c1cacf3
+6
-1
@@ -466,7 +466,12 @@ class ConstraintInjector(
|
||||
isFromNullabilityConstraint: Boolean,
|
||||
isFromDeclaredUpperBound: Boolean
|
||||
) {
|
||||
if (lowerType === upperType) return
|
||||
// Avoid checking trivial incorporated constraints
|
||||
if (isK2) {
|
||||
if (lowerType == upperType) return
|
||||
} else {
|
||||
if (lowerType === upperType) return
|
||||
}
|
||||
if (c.isAllowedType(lowerType) && c.isAllowedType(upperType)) {
|
||||
fun runIsSubtypeOf() =
|
||||
runIsSubtypeOf(lowerType, upperType, shouldTryUseDifferentFlexibilityForUpperType, isFromNullabilityConstraint)
|
||||
|
||||
+24
-3
@@ -143,10 +143,12 @@ abstract class AbstractTypeApproximator(
|
||||
|
||||
val lowerResult = approximateTo(lowerBound, conf, depth)
|
||||
|
||||
val upperResult = if (!type.isRawType() && lowerBound.typeConstructor() == upperBound.typeConstructor())
|
||||
val upperResult = if (!type.isRawType() && !shouldApproximateUpperBoundSeparately(lowerBound, upperBound, conf)) {
|
||||
// We skip approximating the upper bound if the type constructors match as an optimization.
|
||||
lowerResult?.withNullability(upperBound.isMarkedNullable())
|
||||
else
|
||||
} else {
|
||||
approximateTo(upperBound, conf, depth)
|
||||
}
|
||||
if (lowerResult == null && upperResult == null) return null
|
||||
|
||||
/**
|
||||
@@ -168,11 +170,30 @@ abstract class AbstractTypeApproximator(
|
||||
}
|
||||
}
|
||||
|
||||
private fun shouldApproximateUpperBoundSeparately(
|
||||
lowerBound: SimpleTypeMarker,
|
||||
upperBound: SimpleTypeMarker,
|
||||
conf: TypeApproximatorConfiguration,
|
||||
): Boolean {
|
||||
val upperBoundConstructor = upperBound.typeConstructor()
|
||||
if (lowerBound.typeConstructor() != upperBoundConstructor) return true
|
||||
|
||||
// Flexible arrays have the shape `Array<X>..Array<out X>?`.
|
||||
// When such a type is captured, it results in `Array<X>..Array<Captured(out X)>?`, therefore it's necessary to approximate the
|
||||
// upper bound separately.
|
||||
// As an important performance optimization, we explicitly check if the type in question is an array with a captured type argument
|
||||
// that needs to be approximated.
|
||||
// This saves us from doing twice the work unnecessarily in many cases.
|
||||
return isK2 &&
|
||||
upperBoundConstructor.isArrayConstructor() &&
|
||||
upperBound.getArgumentOrNull(0).let { it is CapturedTypeMarker && conf.capturedType(ctx, it) }
|
||||
}
|
||||
|
||||
private fun approximateLocalTypes(
|
||||
type: SimpleTypeMarker,
|
||||
conf: TypeApproximatorConfiguration,
|
||||
toSuper: Boolean,
|
||||
depth: Int
|
||||
depth: Int,
|
||||
): SimpleTypeMarker? {
|
||||
if (!toSuper) return null
|
||||
if (!conf.localTypes && !conf.anonymous) return null
|
||||
|
||||
Reference in New Issue
Block a user