[kotlin-dfa] Box/unbox result of try/catch (KTIJ-29010)

GitOrigin-RevId: 40a12cce011a186e1cc730df757d5e5db2ecbef1
This commit is contained in:
Tagir Valeev
2024-03-12 17:35:26 +00:00
committed by intellij-monorepo-bot
parent 44bd206e34
commit fd50f306c9
5 changed files with 14 additions and 2 deletions
@@ -1005,7 +1005,8 @@ class KtControlFlowBuilder(val factory: DfaValueFactory, val context: KtExpressi
val finallyDescriptor = if (finallyBlock != null) EnterFinallyTrap(finallyBlock, finallyStart) else null
finallyDescriptor?.let { trapTracker.pushTrap(it) }
val tempVar = flow.createTempVariable(DfType.TOP)
val kotlinType = statement.getKotlinType()
val tempVar = flow.createTempVariable(kotlinType.toDfType())
val sections = statement.catchClauses
val clauses = LinkedHashMap<CatchClauseDescriptor, DeferredOffset>()
if (sections.isNotEmpty()) {
@@ -1019,6 +1020,7 @@ class KtControlFlowBuilder(val factory: DfaValueFactory, val context: KtExpressi
}
processExpression(tryBlock)
addImplicitConversion(tryBlock, kotlinType)
addInstruction(JvmAssignmentInstruction(null, tempVar))
val gotoEnd = createTransfer(statement, tryBlock, tempVar, true)
@@ -1035,6 +1037,7 @@ class KtControlFlowBuilder(val factory: DfaValueFactory, val context: KtExpressi
setOffset(offset)
val catchBlock = section.catchBody
processExpression(catchBlock)
addImplicitConversion(catchBlock, kotlinType)
addInstruction(JvmAssignmentInstruction(null, tempVar))
controlTransfer(gotoEnd, singleFinally)
}
@@ -92,6 +92,7 @@ class K2DataFlowInspectionTest : AbstractK2InspectionTest() {
fun testSuppressionsK2() = doTest()
fun testTopLevelDeclaration() = doTest()
fun testTryCatch() = doTest()
fun testTryCatchReturnValue() = doTest()
fun testTryCatchInsideFinally() = doTest()
fun testTryFinally() = doTest()
fun testTypeCastK2() = doTest()
@@ -208,7 +208,8 @@ class KtControlFlowBuilder(val factory: DfaValueFactory, val context: KtExpressi
val finallyDescriptor = if (finallyBlock != null) EnterFinallyTrap(finallyBlock, finallyStart) else null
finallyDescriptor?.let { trapTracker.pushTrap(it) }
val tempVar = flow.createTempVariable(DfType.TOP)
val kotlinType = statement.getKotlinType()
val tempVar = flow.createTempVariable(kotlinType.toDfType())
val sections = statement.catchClauses
val clauses = LinkedHashMap<CatchClauseDescriptor, DeferredOffset>()
if (sections.isNotEmpty()) {
@@ -222,6 +223,7 @@ class KtControlFlowBuilder(val factory: DfaValueFactory, val context: KtExpressi
}
processExpression(tryBlock)
addImplicitConversion(tryBlock, kotlinType)
addInstruction(JvmAssignmentInstruction(null, tempVar))
val gotoEnd = createTransfer(statement, tryBlock, tempVar, true)
@@ -238,6 +240,7 @@ class KtControlFlowBuilder(val factory: DfaValueFactory, val context: KtExpressi
setOffset(offset)
val catchBlock = section.catchBody
processExpression(catchBlock)
addImplicitConversion(catchBlock, kotlinType)
addInstruction(JvmAssignmentInstruction(null, tempVar))
controlTransfer(gotoEnd, singleFinally)
}
@@ -93,6 +93,7 @@ class KtDataFlowInspectionTest : KotlinLightCodeInsightFixtureTestCase() {
fun testSuppressions() = doTest()
fun testTopLevelDeclaration() = doTest()
fun testTryCatch() = doTest()
fun testTryCatchReturnValue() = doTest()
fun testTryCatchInsideFinally() = doTest()
fun testTryFinally() = doTest()
fun testTypeCast() = doTest()
@@ -0,0 +1,4 @@
// WITH_STDLIB
fun main() {
if (<warning descr="Condition '1 == try { 1 } catch (e: Exception) { null }' is always true">1 == try { 1 } catch (e: Exception) { null }</warning>) {}
}