K2: drop unnecessary attributes when inferring declaration types

Related to KT-62578
This commit is contained in:
Mikhail Glukhikh
2023-10-17 13:44:40 +02:00
committed by Space Team
parent 90eaf5f70f
commit cebb6747e3
5 changed files with 29 additions and 13 deletions
@@ -30,10 +30,21 @@ fun FirTypeRef.approximateDeclarationType(
}
val preparedType = if (isLocal) baseType else baseType.substituteAlternativesInPublicType(session)
val approximatedType = session.typeApproximator.approximateToSuperType(preparedType, configuration) ?: preparedType
var approximatedType = session.typeApproximator.approximateToSuperType(preparedType, configuration) ?: preparedType
if (approximatedType.contains { type -> type.attributes.any { !it.keepInInferredDeclarationType } }) {
approximatedType = UnnecessaryAttributesRemover(session).substituteOrSelf(approximatedType)
}
return this.withReplacedConeType(approximatedType).applyIf(stripEnhancedNullability) { withoutEnhancedNullability() }
}
private class UnnecessaryAttributesRemover(session: FirSession) : AbstractConeSubstitutor(session.typeContext) {
override fun substituteType(type: ConeKotlinType): ConeKotlinType? {
val filteredAttributes = type.attributes.filterNecessaryToKeep()
return if (filteredAttributes === type.attributes) null
else type.withAttributes(filteredAttributes)
}
}
private fun ConeKotlinType.substituteAlternativesInPublicType(session: FirSession): ConeKotlinType {
val substitutor = object : AbstractConeSubstitutor(session.typeContext) {
override fun substituteType(type: ConeKotlinType): ConeKotlinType? {