diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/types/AbstractTypeApproximator.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/types/AbstractTypeApproximator.kt index 89a322553a3..09f54c4c144 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/types/AbstractTypeApproximator.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/types/AbstractTypeApproximator.kt @@ -375,7 +375,7 @@ abstract class AbstractTypeApproximator( } if (typeConstructor is TypeVariableTypeConstructorMarker) { - return if (shouldHandleAsTypeVariable(conf, typeConstructor)) null else type.defaultResult(toSuper) + return if (conf.shouldKeepTypeVariableBasedType(typeConstructor, isK2)) null else type.defaultResult(toSuper) } if (typeConstructor.isIntegerLiteralConstantTypeConstructor()) { @@ -393,15 +393,6 @@ abstract class AbstractTypeApproximator( return approximateLocalTypes(type, conf, toSuper, depth) // simple classifier type } - private fun shouldHandleAsTypeVariable( - conf: TypeApproximatorConfiguration, - typeConstructorMarker: TypeVariableTypeConstructorMarker, - ): Boolean = when (conf.typeVariable(typeConstructorMarker)) { - TypeApproximatorConfiguration.HandleAsTypeVariable.ALWAYS -> true - TypeApproximatorConfiguration.HandleAsTypeVariable.K2_ONLY -> isK2 - TypeApproximatorConfiguration.HandleAsTypeVariable.NEVER -> false - } - private fun approximateDefinitelyNotNullType( type: DefinitelyNotNullTypeMarker, conf: TypeApproximatorConfiguration, diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/types/TypeApproximatorConfiguration.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/types/TypeApproximatorConfiguration.kt index 61e2043d3c4..91f8ebda366 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/types/TypeApproximatorConfiguration.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/types/TypeApproximatorConfiguration.kt @@ -38,13 +38,15 @@ open class TypeApproximatorConfiguration { */ open val anonymous = false - internal enum class HandleAsTypeVariable { - ALWAYS, - K2_ONLY, - NEVER - } + /** + * This function determines the approximator behavior if a type variable based type is encountered. + * + * @param marker type variable encountered + * @param isK2 true for K2 compiler, false for K1 compiler + * @return true if the type variable based type should be kept, false if it should be approximated + */ + internal open fun shouldKeepTypeVariableBasedType(marker: TypeVariableTypeConstructorMarker, isK2: Boolean): Boolean = false - internal open val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable = { HandleAsTypeVariable.NEVER } open fun capturedType(ctx: TypeSystemInferenceExtensionContext, type: CapturedTypeMarker): Boolean = true // false means that this type we can leave as is @@ -63,7 +65,7 @@ open class TypeApproximatorConfiguration { override val integerLiteralConstantType: Boolean get() = true override val intersectionTypesInContravariantPositions: Boolean get() = true - override val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable get() = { HandleAsTypeVariable.K2_ONLY } + override fun shouldKeepTypeVariableBasedType(marker: TypeVariableTypeConstructorMarker, isK2: Boolean): Boolean = isK2 } open class PublicDeclaration(override val localTypes: Boolean, override val anonymous: Boolean) : AllFlexibleSameValue() { @@ -73,7 +75,7 @@ open class TypeApproximatorConfiguration { override val integerLiteralConstantType: Boolean get() = true override val intersectionTypesInContravariantPositions: Boolean get() = true - override val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable get() = { HandleAsTypeVariable.K2_ONLY } + override fun shouldKeepTypeVariableBasedType(marker: TypeVariableTypeConstructorMarker, isK2: Boolean): Boolean = isK2 object SaveAnonymousTypes : PublicDeclaration(localTypes = false, anonymous = false) object ApproximateAnonymousTypes : PublicDeclaration(localTypes = false, anonymous = true) @@ -89,7 +91,7 @@ open class TypeApproximatorConfiguration { approximatedCapturedStatus != null && type.captureStatus(ctx) == approximatedCapturedStatus override val intersection: IntersectionStrategy get() = IntersectionStrategy.ALLOWED - override val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable get() = { HandleAsTypeVariable.ALWAYS } + override fun shouldKeepTypeVariableBasedType(marker: TypeVariableTypeConstructorMarker, isK2: Boolean): Boolean = true } object IncorporationConfiguration : AbstractCapturedTypesApproximation(CaptureStatus.FOR_INCORPORATION) @@ -118,7 +120,7 @@ open class TypeApproximatorConfiguration { override val integerLiteralConstantType: Boolean get() = true override val allFlexible: Boolean get() = true override val intersection: IntersectionStrategy get() = IntersectionStrategy.ALLOWED - override val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable get() = { HandleAsTypeVariable.ALWAYS } + override fun shouldKeepTypeVariableBasedType(marker: TypeVariableTypeConstructorMarker, isK2: Boolean): Boolean = true override val errorType: Boolean get() = true override fun capturedType(ctx: TypeSystemInferenceExtensionContext, type: CapturedTypeMarker): Boolean = false