Put the type inference on calls with self types under the compiler flag

This commit is contained in:
Victor Petukhov
2021-07-06 13:56:28 +03:00
parent 51c5a54e31
commit 3787099a38
30 changed files with 365 additions and 50 deletions
@@ -21,7 +21,7 @@ import kotlin.math.max
class ConstraintInjector(
val constraintIncorporator: ConstraintIncorporator,
val typeApproximator: AbstractTypeApproximator,
private val languageVersionSettings: LanguageVersionSettings,
val languageVersionSettings: LanguageVersionSettings,
) {
private val ALLOWED_DEPTH_DELTA_FOR_INCORPORATION = 1
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.config.LanguageFeature
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.hasDeclaredUpperBoundSelfTypes
@@ -16,7 +18,8 @@ import org.jetbrains.kotlin.types.model.*
class ResultTypeResolver(
val typeApproximator: AbstractTypeApproximator,
val trivialConstraintTypeInferenceOracle: TrivialConstraintTypeInferenceOracle
val trivialConstraintTypeInferenceOracle: TrivialConstraintTypeInferenceOracle,
private val languageVersionSettings: LanguageVersionSettings
) {
interface Context : TypeSystemInferenceExtensionContext {
fun isProperType(type: KotlinTypeMarker): Boolean
@@ -211,8 +214,11 @@ class ResultTypeResolver(
}
private fun Context.findSuperType(variableWithConstraints: VariableWithConstraints): KotlinTypeMarker? {
val isTypeInferenceForSelfTypesSupported =
languageVersionSettings.supportsFeature(LanguageFeature.TypeInferenceOnCallsWithSelfTypes)
val upperConstraints = variableWithConstraints.constraints.filter {
it.kind == ConstraintKind.UPPER && (hasDeclaredUpperBoundSelfTypes(it) || isProperTypeForFixation(it.type))
it.kind == ConstraintKind.UPPER
&& ((isTypeInferenceForSelfTypesSupported && hasDeclaredUpperBoundSelfTypes(it)) || isProperTypeForFixation(it.type))
}
if (upperConstraints.isNotEmpty()) {
val intersectionUpperType = intersectTypes(upperConstraints.map { it.type })
@@ -57,13 +57,17 @@ class VariableFixationFinder(
private val inferenceCompatibilityModeEnabled: Boolean
get() = languageVersionSettings.supportsFeature(LanguageFeature.InferenceCompatibility)
private val isTypeInferenceForSelfTypesSupported: Boolean
get() = languageVersionSettings.supportsFeature(LanguageFeature.TypeInferenceOnCallsWithSelfTypes)
private fun Context.getTypeVariableReadiness(
variable: TypeConstructorMarker,
dependencyProvider: TypeVariableDependencyInformationProvider,
): TypeVariableFixationReadiness = when {
!notFixedTypeVariables.contains(variable) ||
dependencyProvider.isVariableRelatedToTopLevelType(variable) -> TypeVariableFixationReadiness.FORBIDDEN
hasDeclaredUpperBoundSelfTypes(variable) -> TypeVariableFixationReadiness.READY_FOR_FIXATION_DECLARED_UPPER_BOUND_WITH_SELF_TYPES
isTypeInferenceForSelfTypesSupported && hasDeclaredUpperBoundSelfTypes(variable) ->
TypeVariableFixationReadiness.READY_FOR_FIXATION_DECLARED_UPPER_BOUND_WITH_SELF_TYPES
!variableHasProperArgumentConstraints(variable) -> TypeVariableFixationReadiness.WITHOUT_PROPER_ARGUMENT_CONSTRAINT
hasDependencyToOtherTypeVariables(variable) -> TypeVariableFixationReadiness.WITH_COMPLEX_DEPENDENCY
variableHasTrivialOrNonProperConstraints(variable) -> TypeVariableFixationReadiness.WITH_TRIVIAL_OR_NON_PROPER_CONSTRAINTS
@@ -406,7 +406,9 @@ abstract class AbstractTypeApproximator(
val argumentTypeConstructor = argument.getType().typeConstructor()
if (argumentTypeConstructor is TypeVariableTypeConstructorMarker && conf.selfTypesWithTypeVariablesToCapturedStarProjection) {
val isTypeInferenceForSelfTypesSupported =
languageVersionSettings.supportsFeature(LanguageFeature.TypeInferenceOnCallsWithSelfTypes)
if (isTypeInferenceForSelfTypesSupported && argumentTypeConstructor is TypeVariableTypeConstructorMarker && conf.selfTypesWithTypeVariablesToCapturedStarProjection) {
// If we have Self<TypeVariable(T)> where T is bounded by Self<T>, we approximate it to CapturedType(*) to satisfy constraints
if (argumentTypeConstructor.typeParameter?.hasRecursiveBounds(type.typeConstructor()) == true) {
return createCapturedStarProjectionForSelfType(argumentTypeConstructor, type)