[FIR] Add smartcasts from == if equals is from Any

^KT-49127 Fixed
This commit is contained in:
Dmitriy Novozhilov
2021-12-01 13:50:55 +03:00
committed by teamcityserver
parent ac718cd1c4
commit 1f0b62b25f
16 changed files with 472 additions and 29 deletions
@@ -17,12 +17,14 @@ import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutorByMap
import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
import org.jetbrains.kotlin.fir.symbols.ensureResolved
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.types.model.CaptureStatus
import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.kotlin.utils.SmartSet
import org.jetbrains.kotlin.utils.addIfNotNull
abstract class SupertypeSupplier {
abstract fun forClass(firClass: FirClass, useSiteSession: FirSession): List<ConeClassLikeType>
@@ -44,6 +46,48 @@ abstract class SupertypeSupplier {
}
}
fun collectSymbolsForType(type: ConeKotlinType, useSiteSession: FirSession): List<FirClassSymbol<*>> {
val lookupTags = mutableListOf<ConeClassLikeLookupTag>()
fun ConeKotlinType.collectClassIds() {
when (val unwrappedType = lowerBoundIfFlexible().fullyExpandedType(useSiteSession)) {
is ConeClassLikeType -> lookupTags.addIfNotNull(unwrappedType.lookupTag)
is ConeIntersectionType -> unwrappedType.intersectedTypes.forEach { it.collectClassIds() }
else -> {}
}
}
type.collectClassIds()
return lookupTags.mapNotNull { it.toSymbol(useSiteSession) as? FirClassSymbol<*> }
}
fun lookupSuperTypes(
type: ConeKotlinType,
lookupInterfaces: Boolean,
deep: Boolean,
useSiteSession: FirSession,
substituteTypes: Boolean,
supertypeSupplier: SupertypeSupplier = SupertypeSupplier.Default,
): List<ConeClassLikeType> {
return lookupSuperTypes(collectSymbolsForType(type, useSiteSession), lookupInterfaces, deep, useSiteSession, substituteTypes, supertypeSupplier)
}
fun lookupSuperTypes(
symbols: List<FirClassifierSymbol<*>>,
lookupInterfaces: Boolean,
deep: Boolean,
useSiteSession: FirSession,
substituteTypes: Boolean,
supertypeSupplier: SupertypeSupplier = SupertypeSupplier.Default,
): List<ConeClassLikeType> {
return SmartList<ConeClassLikeType>().also {
val visitedSymbols = SmartSet.create<FirClassifierSymbol<*>>()
for (symbol in symbols) {
symbol.collectSuperTypes(it, visitedSymbols, deep, lookupInterfaces, substituteTypes, useSiteSession, supertypeSupplier)
}
}
}
fun lookupSuperTypes(
klass: FirClass,
lookupInterfaces: Boolean,