[FIR] Fix inference based on recursive upper bound

#KT-59012 Fixed
This commit is contained in:
Kirill Rakhman
2023-12-06 14:11:29 +01:00
committed by Space Team
parent 251827c9aa
commit 9d91eb2510
14 changed files with 98 additions and 40 deletions
@@ -47,8 +47,9 @@ fun ConstraintStorage.buildNotFixedVariablesToNonSubtypableTypesSubstitutor(
)
}
fun TypeSystemInferenceExtensionContext.hasRecursiveTypeParametersWithGivenSelfType(selfTypeConstructor: TypeConstructorMarker) =
selfTypeConstructor.getParameters().any { it.hasRecursiveBounds(selfTypeConstructor) }
fun TypeSystemInferenceExtensionContext.hasRecursiveTypeParametersWithGivenSelfType(selfTypeConstructor: TypeConstructorMarker): Boolean =
selfTypeConstructor.getParameters().any { it.hasRecursiveBounds(selfTypeConstructor) } ||
isK2 && selfTypeConstructor is CapturedTypeConstructorMarker && selfTypeConstructor.supertypes().any { hasRecursiveTypeParametersWithGivenSelfType(it.typeConstructor()) }
fun TypeSystemInferenceExtensionContext.isRecursiveTypeParameter(typeConstructor: TypeConstructorMarker) =
typeConstructor.getTypeParameterClassifier()?.hasRecursiveBounds() == true
@@ -79,4 +80,4 @@ fun NewConstraintSystemImpl.registerTypeVariableIfNotPresent(
if (typeVariable.freshTypeConstructor(this) !in builder.currentStorage().allTypeVariables.keys) {
builder.registerVariable(typeVariable)
}
}
}
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.resolve.calls.NewCommonSuperTypeCalculator
import org.jetbrains.kotlin.resolve.calls.inference.components.TypeVariableDirectionCalculator.ResolveDirection
import org.jetbrains.kotlin.resolve.calls.inference.extractTypeForGivenRecursiveTypeParameter
import org.jetbrains.kotlin.resolve.calls.inference.hasRecursiveTypeParametersWithGivenSelfType
import org.jetbrains.kotlin.resolve.calls.inference.model.*
import org.jetbrains.kotlin.types.AbstractTypeApproximator
import org.jetbrains.kotlin.types.AbstractTypeChecker
@@ -349,6 +350,10 @@ class ResultTypeResolver(
if (upperConstraints.isNotEmpty()) {
val upperType = computeUpperType(upperConstraints)
if (isK2 && hasRecursiveTypeParametersWithGivenSelfType(upperType.typeConstructor())) {
return upperType
}
return typeApproximator.approximateToSubType(
upperType,
TypeApproximatorConfiguration.InternalTypesApproximation
@@ -476,6 +476,15 @@ abstract class AbstractTypeApproximator(
val argumentType = newArguments[index]?.getType() ?: argument.getType()
val capturedType = argumentType.lowerBoundIfFlexible().originalIfDefinitelyNotNullable().asCapturedType()
// When capturing recursive types with self upper bounds, their super types can contain captured types.
// In approximateCapturedType, we check if the super/subtypes of captured types need approximation even if captured types
// themselves don't need approximation, and will land here.
// To support this case, we also don't want to approximate captured types here if the configuration says so.
if (capturedType != null && isK2 && !conf.capturedType(ctx, capturedType)) {
continue@loop
}
val capturedStarProjectionOrNull =
capturedType?.typeConstructorProjection()?.takeIf { it.isStarProjection() }