[kotlin-dfa] Remove toPsiPrimitiveType() and use DfType.getPsiType() for consistent results in case of heavily broken code

Fixes KTIJ-32467 K2: Constant conditions inspection primitive type exceptions

GitOrigin-RevId: 02a670764539862ab1c72aa357e24158f1e14b2e
This commit is contained in:
Tagir Valeev
2024-12-09 14:56:39 +00:00
committed by intellij-monorepo-bot
parent 3f24e01775
commit 4b7b06f9a0
4 changed files with 9 additions and 15 deletions
@@ -1828,7 +1828,7 @@ class KtControlFlowBuilder(val factory: DfaValueFactory, val context: KtExpressi
addInstruction(WrapDerivedVariableInstruction(expectedType.toDfType().meet(dfType), SpecialField.UNBOX))
}
if (actualDfType is DfPrimitiveType && expectedDfType is DfPrimitiveType) {
addInstruction(PrimitiveConversionInstruction(expectedType.toPsiPrimitiveType(), null))
addInstruction(PrimitiveConversionInstruction(expectedDfType.psiType, null))
}
}
@@ -11,7 +11,6 @@ import com.intellij.codeInspection.dataFlow.types.DfReferenceType
import com.intellij.codeInspection.dataFlow.types.DfType
import com.intellij.codeInspection.dataFlow.types.DfTypes
import com.intellij.codeInspection.dataFlow.value.RelationType
import com.intellij.psi.PsiPrimitiveType
import com.intellij.psi.PsiTypes
import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.analysis.api.KaSession
@@ -193,19 +192,6 @@ internal fun mathOpFromAssignmentToken(token: IElementType): LongRangeBinOp? = w
else -> null
}
context(KaSession)
internal fun KaType.toPsiPrimitiveType(): PsiPrimitiveType = when ((this as? KaClassType)?.classId) {
DefaultTypeClassIds.BOOLEAN -> PsiTypes.booleanType()
DefaultTypeClassIds.BYTE -> PsiTypes.byteType()
DefaultTypeClassIds.CHAR -> PsiTypes.charType()
DefaultTypeClassIds.SHORT -> PsiTypes.shortType()
DefaultTypeClassIds.INT -> PsiTypes.intType()
DefaultTypeClassIds.LONG -> PsiTypes.longType()
DefaultTypeClassIds.FLOAT -> PsiTypes.floatType()
DefaultTypeClassIds.DOUBLE -> PsiTypes.doubleType()
else -> throw IllegalArgumentException("Not a primitive analog: $this")
}
context(KaSession)
internal fun KaType.canBeNull() = isMarkedNullable || hasFlexibleNullability
@@ -97,6 +97,7 @@ class K2DataFlowInspectionTest : AbstractK2InspectionTest() {
doTest()
}
fun testPrimitiveAndNullK2() = doTest()
fun testPrimitiveBound() = doTest()
fun testProperty() = doTest()
fun testQualifierK2() = doTest()
fun testRangeAnnotation() = doTest()
@@ -0,0 +1,7 @@
// WITH_STDLIB
class JJJ<T: <warning descr="[FINAL_UPPER_BOUND] Type 'kotlin.Char' is final, so the value of the type parameter is predetermined.">Char</warning>> {
var s: Array<T> = <error descr="[TYPE_PARAMETER_AS_REIFIED] Cannot use 'T' as reified type parameter. Use a class instead.">arrayOf</error>()
fun m() {
s [0] = <error descr="[ARGUMENT_TYPE_MISMATCH] Argument type mismatch: actual type is 'kotlin.Int', but 'T (of class JJJ<T : Char>)' was expected.">2</error>
}
}