ConeSimpleKotlinType: introduce & use unwrapDefinitelyNotNull

This commit is contained in:
Mikhail Glukhikh
2022-02-03 15:52:35 +03:00
committed by TeamCityServer
parent d1194e5fd2
commit 27d4c745cb
3 changed files with 14 additions and 5 deletions
@@ -14,7 +14,6 @@ import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeCyclicTypeBound
import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol
import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
object FirCyclicTypeBoundsChecker : FirBasicDeclarationChecker() { object FirCyclicTypeBoundsChecker : FirBasicDeclarationChecker() {
@@ -77,5 +76,6 @@ object FirCyclicTypeBoundsChecker : FirBasicDeclarationChecker() {
private fun extractTypeParamNames(ref: FirTypeRef): Set<Name> = private fun extractTypeParamNames(ref: FirTypeRef): Set<Name> =
ref.unwrapBound().mapNotNull { extractTypeParamName(it.coneType) }.toSet() ref.unwrapBound().mapNotNull { extractTypeParamName(it.coneType) }.toSet()
private fun extractTypeParamName(type: ConeKotlinType): Name? = type.safeAs<ConeTypeParameterType>()?.lookupTag?.name private fun extractTypeParamName(type: ConeKotlinType): Name? =
(type.lowerBoundIfFlexible().unwrapDefinitelyNotNull() as? ConeTypeParameterType)?.lookupTag?.name
} }
@@ -24,12 +24,14 @@ object FirTopLevelTypeAliasChecker : FirTypeAliasChecker() {
} }
fun containsTypeParameter(type: ConeKotlinType): Boolean { fun containsTypeParameter(type: ConeKotlinType): Boolean {
if (type is ConeTypeParameterType) { val unwrapped = type.lowerBoundIfFlexible().unwrapDefinitelyNotNull()
if (unwrapped is ConeTypeParameterType) {
return true return true
} }
if (type is ConeClassLikeType && type.lookupTag.toSymbol(context.session) is FirTypeAliasSymbol) { if (unwrapped is ConeClassLikeType && unwrapped.lookupTag.toSymbol(context.session) is FirTypeAliasSymbol) {
for (typeArgument in type.typeArguments) { for (typeArgument in unwrapped.typeArguments) {
val typeArgumentType = (typeArgument as? ConeKotlinType) ?: (typeArgument as? ConeKotlinTypeProjection)?.type val typeArgumentType = (typeArgument as? ConeKotlinType) ?: (typeArgument as? ConeKotlinTypeProjection)?.type
if (typeArgumentType != null && containsTypeParameter(typeArgumentType)) { if (typeArgumentType != null && containsTypeParameter(typeArgumentType)) {
return true return true
@@ -175,6 +175,13 @@ fun ConeKotlinType.lowerBoundIfFlexible(): ConeSimpleKotlinType {
} }
} }
fun ConeSimpleKotlinType.unwrapDefinitelyNotNull(): ConeSimpleKotlinType {
return when (this) {
is ConeDefinitelyNotNullType -> original
else -> this
}
}
class ConeCapturedTypeConstructor( class ConeCapturedTypeConstructor(
val projection: ConeTypeProjection, val projection: ConeTypeProjection,
var supertypes: List<ConeKotlinType>? = null, var supertypes: List<ConeKotlinType>? = null,