[FIR] Map platform types to Kotlin for equality checks

During the DM it was decided to support
such comparisons, if I remember correctly.

^KT-62646
This commit is contained in:
Nikolay Lunyak
2024-02-27 12:29:18 +02:00
committed by Space Team
parent 6607c8a056
commit cd3202ddd4
2 changed files with 8 additions and 8 deletions
@@ -24,9 +24,9 @@ import org.jetbrains.kotlin.fir.declarations.utils.isInterface
import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.isPrimitiveType import org.jetbrains.kotlin.fir.isPrimitiveType
import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.fullyExpandedType
import org.jetbrains.kotlin.fir.scopes.platformClassMapper
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.text import org.jetbrains.kotlin.text
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
@@ -321,7 +321,7 @@ private fun ConeKotlinType.isEnum(session: FirSession) = toRegularClassSymbol(se
private fun ConeKotlinType.isClass(session: FirSession) = toRegularClassSymbol(session) != null private fun ConeKotlinType.isClass(session: FirSession) = toRegularClassSymbol(session) != null
private fun ConeKotlinType.toTypeInfo(session: FirSession): TypeInfo { private fun ConeKotlinType.toTypeInfo(session: FirSession): TypeInfo {
val bounds = collectUpperBounds().map { type -> toKotlinType(type).replaceArgumentsWithStarProjections() } val bounds = collectUpperBounds().map { type -> type.toKotlinTypeIfPlatform(session).replaceArgumentsWithStarProjections() }
val type = bounds.ifNotEmpty { ConeTypeIntersector.intersectTypes(session.typeContext, this) } val type = bounds.ifNotEmpty { ConeTypeIntersector.intersectTypes(session.typeContext, this) }
?: session.builtinTypes.nullableAnyType.type ?: session.builtinTypes.nullableAnyType.type
val notNullType = type.withNullability(ConeNullability.NOT_NULL, session.typeContext) val notNullType = type.withNullability(ConeNullability.NOT_NULL, session.typeContext)
@@ -338,9 +338,9 @@ private fun ConeKotlinType.toTypeInfo(session: FirSession): TypeInfo {
) )
} }
private fun toKotlinType(type: ConeClassLikeType): ConeClassLikeType { private fun ConeClassLikeType.toKotlinTypeIfPlatform(session: FirSession): ConeClassLikeType {
// Type arguments are ignored by design val kotlinClassId = session.platformClassMapper.getCorrespondingKotlinClass(lookupTag.classId)
return ConeClassLikeTypeImpl(type.lookupTag, type.typeArguments, type.isNullable) return kotlinClassId?.constructClassLikeType(typeArguments, isNullable, attributes) ?: this
} }
private class ArgumentInfo( private class ArgumentInfo(
@@ -8,8 +8,8 @@ import checkSubtype
fun main(args : Array<String>) { fun main(args : Array<String>) {
val x = checkSubtype<Any>(args[0]) val x = checkSubtype<Any>(args[0])
if(x is <!PLATFORM_CLASS_MAPPED_TO_KOTLIN!>java.lang.CharSequence<!>) { if(x is <!PLATFORM_CLASS_MAPPED_TO_KOTLIN!>java.lang.CharSequence<!>) {
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!>) x.length else x.length() // OK if ("a" == x) x.length else x.length() // OK
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!> || <!EQUALITY_NOT_APPLICABLE_WARNING!>"b" == x<!>) x.length else x.length() // < THEN ERROR if ("a" == x || "b" == x) x.length else x.length() // < THEN ERROR
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!> && "a" == x) x.length else x.length() // < ELSE ERROR if ("a" == x && "a" == x) x.length else x.length() // < ELSE ERROR
} }
} }