From b78b50e1f8da508fc0b3f812dcc82ec0a5b7cac0 Mon Sep 17 00:00:00 2001 From: Tianyu Geng Date: Wed, 19 May 2021 16:29:20 -0700 Subject: [PATCH] FIR DFA: workaround KT-46826 --- .../fir/resolve/dfa/PersistentLogicSystem.kt | 18 ++++-------------- .../smartcasts/letChangesToNull.fir.kt | 2 +- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/PersistentLogicSystem.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/PersistentLogicSystem.kt index 95a90f6ce18..bb3479242e1 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/PersistentLogicSystem.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/PersistentLogicSystem.kt @@ -134,7 +134,7 @@ abstract class PersistentLogicSystem(context: ConeInferenceContext) : LogicSyste val variables = flows.flatMap { it.approvedTypeStatements.keys }.toSet() for (variable in variables) { - val info = mergeOperation(flows.map { it.getApprovedTypeStatements(variable, commonFlow) }) ?: continue + val info = mergeOperation(flows.map { it.getApprovedTypeStatements(variable) }) ?: continue removeTypeStatementsAboutVariable(commonFlow, variable) val thereWereReassignments = variable.hasDifferentReassignments(flows) if (thereWereReassignments) { @@ -210,23 +210,13 @@ abstract class PersistentLogicSystem(context: ConeInferenceContext) : LogicSyste } @OptIn(DfaInternals::class) - private fun PersistentFlow.getApprovedTypeStatements(variable: RealVariable, parentFlow: PersistentFlow): MutableTypeStatement { + private fun PersistentFlow.getApprovedTypeStatements(variable: RealVariable): MutableTypeStatement { var flow = this val result = MutableTypeStatement(variable) val variableUnderAlias = directAliasMap[variable] if (variableUnderAlias == null) { - // get approved type statement even though the starting flow == parent flow - if (flow == parentFlow) { - flow.approvedTypeStatements[variable]?.let { - result += it - } - } else { - while (flow != parentFlow) { - flow.approvedTypeStatements[variable]?.let { - result += it - } - flow = flow.previousFlow!! - } + flow.approvedTypeStatements[variable]?.let { + result += it } } else { result.exactType.addIfNotNull(variableUnderAlias.originalType) diff --git a/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letChangesToNull.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letChangesToNull.fir.kt index 06db53d4e93..d320fa3509a 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letChangesToNull.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/smartcasts/letChangesToNull.fir.kt @@ -4,6 +4,6 @@ fun foo(y: String?) { var x: String? = "" if (x != null) { y?.let { x = null } - x.length // Smart cast is not possible + x.length // Smart cast is not possible } }