diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/LogicSystem.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/LogicSystem.kt index 20c2001ec71..dfdc0d3fe79 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/LogicSystem.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/LogicSystem.kt @@ -122,16 +122,22 @@ abstract class LogicSystem(protected val context: ConeInferenceCont return result } - protected fun or(statements: Collection): MutableTypeStatement { + private inline fun manipulateTypeStatements( + statements: Collection, + op: (Collection>) -> MutableSet + ): MutableTypeStatement { require(statements.isNotEmpty()) statements.singleOrNull()?.let { return it as MutableTypeStatement } val variable = statements.first().variable assert(statements.all { it.variable == variable }) - val exactType = orForTypes(statements.map { it.exactType }) - val exactNotType = orForTypes(statements.map { it.exactNotType }) + val exactType = op.invoke(statements.map { it.exactType }) + val exactNotType = op.invoke(statements.map { it.exactNotType }) return MutableTypeStatement(variable, exactType, exactNotType) } + protected fun or(statements: Collection): MutableTypeStatement = + manipulateTypeStatements(statements, ::orForTypes) + private fun orForTypes(types: Collection>): MutableSet { if (types.any { it.isEmpty() }) return mutableSetOf() val allTypes = types.flatMapTo(mutableSetOf()) { it } @@ -144,15 +150,8 @@ abstract class LogicSystem(protected val context: ConeInferenceCont return commonTypes } - protected fun and(statements: Collection): MutableTypeStatement { - require(statements.isNotEmpty()) - statements.singleOrNull()?.let { return it as MutableTypeStatement } - val variable = statements.first().variable - assert(statements.all { it.variable == variable }) - val exactType = andForTypes(statements.map { it.exactType }) - val exactNotType = andForTypes(statements.map { it.exactNotType }) - return MutableTypeStatement(variable, exactType, exactNotType) - } + protected fun and(statements: Collection): MutableTypeStatement = + manipulateTypeStatements(statements, ::andForTypes) private fun andForTypes(types: Collection>): MutableSet { return types.flatMapTo(mutableSetOf()) { it }