[FE] Add ability to configure PublicDeclaration type approximation configuration

This configuration allows to choose what to do with local and anonymous types
This commit is contained in:
Dmitriy Novozhilov
2022-11-09 10:33:18 +02:00
committed by Space Team
parent 82c39187f6
commit 32f6b71525
7 changed files with 14 additions and 11 deletions
@@ -114,9 +114,9 @@ private fun ReceiverParameterDescriptor.substituteTopLevelType(newType: KotlinTy
private fun TypeApproximator.approximate(type: UnwrappedType, toSuper: Boolean): KotlinType? {
if (type.arguments.isEmpty() && type.constructor.isDenotable) return null
return if (toSuper)
approximateToSuperType(type, TypeApproximatorConfiguration.PublicDeclaration)
approximateToSuperType(type, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes)
else
approximateToSubType(type, TypeApproximatorConfiguration.PublicDeclaration)
approximateToSubType(type, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes)
}
/**
@@ -702,9 +702,9 @@ class FirElementSerializer private constructor(
}
is ConeIntersectionType -> {
val approximatedType = if (toSuper) {
typeApproximator.approximateToSuperType(type, TypeApproximatorConfiguration.PublicDeclaration)
typeApproximator.approximateToSuperType(type, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes)
} else {
typeApproximator.approximateToSubType(type, TypeApproximatorConfiguration.PublicDeclaration)
typeApproximator.approximateToSubType(type, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes)
}
assert(approximatedType != type && approximatedType is ConeKotlinType) {
"Approximation failed: ${type.renderForDebugging()}"
@@ -276,9 +276,9 @@ internal fun FirTypeRef.approximated(
toSuper: Boolean
): FirTypeRef {
val approximatedType = if (toSuper)
approximator.approximateToSuperType(coneType, TypeApproximatorConfiguration.PublicDeclaration)
approximator.approximateToSuperType(coneType, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes)
else
approximator.approximateToSubType(coneType, TypeApproximatorConfiguration.PublicDeclaration)
approximator.approximateToSubType(coneType, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes)
return withReplacedConeType(approximatedType as? ConeKotlinType).apply { coneType.collectTypeParameters(typeParameterSet) }
}
@@ -340,9 +340,9 @@ fun FirTypeRef.approximated(
return withReplacedConeType(alternativeType)
}
val approximatedType = if (toSuper)
typeApproximator.approximateToSuperType(alternativeType, TypeApproximatorConfiguration.PublicDeclaration)
typeApproximator.approximateToSuperType(alternativeType, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes)
else
typeApproximator.approximateToSubType(alternativeType, TypeApproximatorConfiguration.PublicDeclaration)
typeApproximator.approximateToSubType(alternativeType, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes)
return withReplacedConeType(approximatedType)
}
@@ -139,7 +139,7 @@ class ResultTypeResolver(
}
private fun KotlinTypeMarker.toPublicType(): KotlinTypeMarker =
typeApproximator.approximateToSuperType(this, TypeApproximatorConfiguration.PublicDeclaration) ?: this
typeApproximator.approximateToSuperType(this, TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes) ?: this
private fun Context.isSuitableType(resultType: KotlinTypeMarker, variableWithConstraints: VariableWithConstraints): Boolean {
val filteredConstraints = variableWithConstraints.constraints.filter { isProperTypeForFixation(it.type) }
@@ -52,12 +52,15 @@ open class TypeApproximatorConfiguration {
override val intersectionTypesInContravariantPositions: Boolean get() = true
}
object PublicDeclaration : AllFlexibleSameValue() {
open class PublicDeclaration(override val localTypes: Boolean, override val anonymous: Boolean) : AllFlexibleSameValue() {
override val allFlexible: Boolean get() = true
override val errorType: Boolean get() = true
override val definitelyNotNullType: Boolean get() = false
override val integerLiteralConstantType: Boolean get() = true
override val intersectionTypesInContravariantPositions: Boolean get() = true
object SaveAnonymousTypes : PublicDeclaration(localTypes = false, anonymous = false)
object ApproximateAnonymousTypes : PublicDeclaration(localTypes = false, anonymous = true)
}
sealed class AbstractCapturedTypesApproximation(val approximatedCapturedStatus: CaptureStatus?) :
@@ -32,7 +32,7 @@ class TypeApproximator(
fun approximateDeclarationType(baseType: KotlinType, local: Boolean): UnwrappedType {
if (!languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) return baseType.unwrap()
val configuration = if (local) TypeApproximatorConfiguration.LocalDeclaration else TypeApproximatorConfiguration.PublicDeclaration
val configuration = if (local) TypeApproximatorConfiguration.LocalDeclaration else TypeApproximatorConfiguration.PublicDeclaration.SaveAnonymousTypes
val preparedType = if (local) baseType.unwrap() else substituteAlternativesInPublicType(baseType)
return approximateToSuperType(preparedType, configuration) ?: preparedType
}