[kotlin-dfa] Minor cleanup in K2 DFA

KTIJ-27359 K2 IDE: migrate KotlinConstantConditionsInspection to K2

GitOrigin-RevId: 8305113ff7066ab354749ea29135152f6b21ffda
This commit is contained in:
Tagir Valeev
2024-03-11 11:07:53 +01:00
committed by intellij-monorepo-bot
parent 41c23e253e
commit e2e0d20d61
2 changed files with 12 additions and 26 deletions

View File

@@ -5,7 +5,6 @@ import com.intellij.codeInspection.dataFlow.TypeConstraint
import com.intellij.codeInspection.dataFlow.TypeConstraints
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiEnumConstant
import com.intellij.psi.PsiType
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
@@ -75,7 +74,11 @@ class KtClassDef(val module: KtModule, val hash: Int, val cls: KtSymbolPointer<K
override fun getEnumConstant(ordinal: Int): PsiEnumConstant? = analyze(module) {
val classLikeSymbol = cls.restoreSymbol() ?: return@analyze null
val psiClass = classLikeSymbol.psi.asPsiClass() ?: return@analyze null
val psiClass = when (val psi = classLikeSymbol.psi) {
is PsiClass -> psi
is KtClassOrObject -> psi.toLightClass() ?: return@analyze null
else -> return@analyze null
}
var cur = 0
for (field in psiClass.fields) {
if (field is PsiEnumConstant) {
@@ -185,8 +188,3 @@ class KtClassDef(val module: KtModule, val hash: Int, val cls: KtSymbolPointer<K
}
}
private fun PsiElement?.asPsiClass(): PsiClass? = when (this) {
is PsiClass -> this
is KtClassOrObject -> toLightClass()
else -> null
}

View File

@@ -37,24 +37,17 @@ internal fun KtType?.toDfType(): DfType {
var notNullableType = this.withNullability(KtTypeNullability.NON_NULLABLE).toDfTypeNotNullable()
if (notNullableType is DfPrimitiveType) {
val cls = (this as? KtNonErrorClassType)?.expandedClassSymbol
val boxedType: DfType
if (cls != null) {
boxedType = TypeConstraints.exactClass(cls.classDef()).asDfType()
val boxedType = if (cls != null) {
TypeConstraints.exactClass(cls.classDef()).asDfType()
} else {
boxedType = DfTypes.OBJECT_OR_NULL
DfTypes.OBJECT_OR_NULL
}
notNullableType = SpecialField.UNBOX.asDfType(notNullableType).meet(boxedType)
}
return when (notNullableType) {
is DfReferenceType -> {
notNullableType.dropNullability().meet(DfaNullability.NULLABLE.asDfType())
}
DfType.BOTTOM -> {
DfTypes.NULL
}
else -> {
notNullableType
}
is DfReferenceType -> notNullableType.dropNullability().meet(DfaNullability.NULLABLE.asDfType())
DfType.BOTTOM -> DfTypes.NULL
else -> notNullableType
}
}
return toDfTypeNotNullable()
@@ -69,12 +62,7 @@ private fun KtType.toDfTypeNotNullable(): DfType {
DefaultTypeClassIds.BOOLEAN -> DfTypes.BOOLEAN
DefaultTypeClassIds.BYTE -> DfTypes.intRange(LongRangeSet.range(Byte.MIN_VALUE.toLong(), Byte.MAX_VALUE.toLong()))
DefaultTypeClassIds.CHAR -> DfTypes.intRange(
LongRangeSet.range(
Character.MIN_VALUE.code.toLong(),
Character.MAX_VALUE.code.toLong()
)
)
LongRangeSet.range(Character.MIN_VALUE.code.toLong(), Character.MAX_VALUE.code.toLong()))
DefaultTypeClassIds.SHORT -> DfTypes.intRange(LongRangeSet.range(Short.MIN_VALUE.toLong(), Short.MAX_VALUE.toLong()))
DefaultTypeClassIds.INT -> DfTypes.INT
DefaultTypeClassIds.LONG -> DfTypes.LONG