From 4b7b06f9a06fd8b2febb7df25a79aab1b1a5ce74 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 9 Dec 2024 12:26:57 +0100 Subject: [PATCH] [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 --- .../inspections/dfa/KtControlFlowBuilder.kt | 2 +- .../k2/codeinsight/inspections/dfa/KtDfaHelpers.kt | 14 -------------- .../inspections/tests/K2DataFlowInspectionTest.kt | 1 + .../testData/inspections/dfa/PrimitiveBound.kt | 7 +++++++ 4 files changed, 9 insertions(+), 15 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/inspections/dfa/PrimitiveBound.kt diff --git a/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/dfa/KtControlFlowBuilder.kt b/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/dfa/KtControlFlowBuilder.kt index 57d2a3c16b51..dfa1db4f98ce 100644 --- a/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/dfa/KtControlFlowBuilder.kt +++ b/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/dfa/KtControlFlowBuilder.kt @@ -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)) } } diff --git a/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/dfa/KtDfaHelpers.kt b/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/dfa/KtDfaHelpers.kt index 1359fcce850b..59e98f8a25b2 100644 --- a/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/dfa/KtDfaHelpers.kt +++ b/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/dfa/KtDfaHelpers.kt @@ -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 diff --git a/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2DataFlowInspectionTest.kt b/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2DataFlowInspectionTest.kt index a3a046cf7e02..8f85fd9fdf54 100644 --- a/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2DataFlowInspectionTest.kt +++ b/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2DataFlowInspectionTest.kt @@ -97,6 +97,7 @@ class K2DataFlowInspectionTest : AbstractK2InspectionTest() { doTest() } fun testPrimitiveAndNullK2() = doTest() + fun testPrimitiveBound() = doTest() fun testProperty() = doTest() fun testQualifierK2() = doTest() fun testRangeAnnotation() = doTest() diff --git a/plugins/kotlin/idea/tests/testData/inspections/dfa/PrimitiveBound.kt b/plugins/kotlin/idea/tests/testData/inspections/dfa/PrimitiveBound.kt new file mode 100644 index 000000000000..28b11170fb1f --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspections/dfa/PrimitiveBound.kt @@ -0,0 +1,7 @@ +// WITH_STDLIB +class JJJChar> { + var s: Array = arrayOf() + fun m() { + s [0] = 2 + } +} \ No newline at end of file