diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt index 6b3ec58294d..52a708029d3 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt @@ -1384,18 +1384,22 @@ abstract class FirDataFlowAnalyzer( } } - private fun MutableFlow.addAllStatements(statements: TypeStatements) = + private fun MutableFlow.addAllStatements(statements: TypeStatements) { statements.values.forEach { addTypeStatement(it) } + } - private fun MutableFlow.addAllConditionally(condition: OperationStatement, statements: TypeStatements) = + private fun MutableFlow.addAllConditionally(condition: OperationStatement, statements: TypeStatements) { statements.values.forEach { addImplication(condition implies it) } + } - private fun MutableFlow.addAllConditionally(condition: OperationStatement, from: Flow) = + private fun MutableFlow.addAllConditionally(condition: OperationStatement, from: Flow) { from.knownVariables.forEach { // Only add the statement if this variable is not aliasing another in `this` (but it could be aliasing in `from`). if (unwrapVariable(it) == it) addImplication(condition implies (from.getTypeStatement(it) ?: return@forEach)) } + } - private fun MutableFlow.commitOperationStatement(statement: OperationStatement) = + private fun MutableFlow.commitOperationStatement(statement: OperationStatement) { addAllStatements(logicSystem.approveOperationStatement(this, statement, removeApprovedOrImpossible = true)) + } } diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/LogicSystem.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/LogicSystem.kt index c49c396d1b4..d31b36843aa 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/LogicSystem.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/dfa/LogicSystem.kt @@ -17,8 +17,9 @@ abstract class LogicSystem(private val context: ConeInferenceContext) { abstract val variableStorage: VariableStorageImpl - protected open fun ConeKotlinType.isAcceptableForSmartcast(): Boolean = - !isNullableNothing + protected open fun ConeKotlinType.isAcceptableForSmartcast(): Boolean { + return !isNullableNothing + } fun joinFlow(flows: Collection, union: Boolean): MutableFlow { when (flows.size) { @@ -62,8 +63,8 @@ abstract class LogicSystem(private val context: ConeInferenceContext) { fun addImplication(flow: MutableFlow, implication: Implication) { val effect = implication.effect if (effect == implication.condition) return - if (effect is TypeStatement && (effect.isEmpty || - flow.approvedTypeStatements[effect.variable]?.exactType?.containsAll(effect.exactType) == true) + if (effect is TypeStatement && + (effect.isEmpty || flow.approvedTypeStatements[effect.variable]?.exactType?.containsAll(effect.exactType) == true) ) return val variable = implication.condition.variable flow.logicStatements[variable] = flow.logicStatements[variable]?.add(implication) ?: persistentListOf(implication) @@ -88,14 +89,17 @@ abstract class LogicSystem(private val context: ConeInferenceContext) { }.build() } - fun approveOperationStatement(flow: PersistentFlow, statement: OperationStatement): TypeStatements = - approveOperationStatement(flow.logicStatements, statement, removeApprovedOrImpossible = false) + fun approveOperationStatement(flow: PersistentFlow, statement: OperationStatement): TypeStatements { + return approveOperationStatement(flow.logicStatements, statement, removeApprovedOrImpossible = false) + } fun approveOperationStatement( flow: MutableFlow, statement: OperationStatement, removeApprovedOrImpossible: Boolean, - ): TypeStatements = approveOperationStatement(flow.logicStatements, statement, removeApprovedOrImpossible) + ): TypeStatements { + return approveOperationStatement(flow.logicStatements, statement, removeApprovedOrImpossible) + } fun recordNewAssignment(flow: MutableFlow, variable: RealVariable, index: Int) { flow.replaceVariable(variable, null) @@ -294,8 +298,9 @@ abstract class LogicSystem(private val context: ConeInferenceContext) { exactType += other.exactType } - fun and(a: TypeStatement?, b: TypeStatement): TypeStatement = - a?.toMutable()?.apply { this += b } ?: b + fun and(a: TypeStatement?, b: TypeStatement): TypeStatement { + return a?.toMutable()?.apply { this += b } ?: b + } fun and(statements: Collection): TypeStatement? { when (statements.size) { @@ -372,13 +377,14 @@ private fun MutableMap>.replaceVar } } -private inline fun PersistentList.replaceAll(block: (T) -> T): PersistentList = - mutate { result -> +private inline fun PersistentList.replaceAll(block: (T) -> T): PersistentList { + return mutate { result -> val it = result.listIterator() while (it.hasNext()) { it.set(block(it.next())) } } +} private fun Implication.replaceVariable(from: RealVariable, to: RealVariable): Implication = when { condition.variable == from -> Implication(condition.copy(variable = to), effect.replaceVariable(from, to)) @@ -386,9 +392,11 @@ private fun Implication.replaceVariable(from: RealVariable, to: RealVariable): I else -> this } -private fun Statement.replaceVariable(from: RealVariable, to: RealVariable): Statement = - if (variable != from) this else when (this) { +private fun Statement.replaceVariable(from: RealVariable, to: RealVariable): Statement { + if (variable != from) return this + return when (this) { is OperationStatement -> copy(variable = to) is PersistentTypeStatement -> copy(variable = to) is MutableTypeStatement -> MutableTypeStatement(to, exactType) } +}