[NI] Fold { Lower | Upper } & Equality constraints if it's possible
This commit is contained in:
+26
-2
@@ -17,7 +17,9 @@
|
|||||||
package org.jetbrains.kotlin.resolve.calls.inference.model
|
package org.jetbrains.kotlin.resolve.calls.inference.model
|
||||||
|
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.trimToSize
|
import org.jetbrains.kotlin.resolve.calls.inference.trimToSize
|
||||||
import org.jetbrains.kotlin.resolve.calls.model.*
|
import org.jetbrains.kotlin.resolve.calls.model.KotlinCallDiagnostic
|
||||||
|
import org.jetbrains.kotlin.resolve.calls.model.PostponedKotlinCallArgument
|
||||||
|
import org.jetbrains.kotlin.resolve.calls.model.ResolvedKotlinCall
|
||||||
import org.jetbrains.kotlin.types.TypeConstructor
|
import org.jetbrains.kotlin.types.TypeConstructor
|
||||||
import org.jetbrains.kotlin.types.UnwrappedType
|
import org.jetbrains.kotlin.types.UnwrappedType
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -28,9 +30,16 @@ class MutableVariableWithConstraints(
|
|||||||
override val typeVariable: NewTypeVariable,
|
override val typeVariable: NewTypeVariable,
|
||||||
constraints: Collection<Constraint> = emptyList()
|
constraints: Collection<Constraint> = emptyList()
|
||||||
) : VariableWithConstraints {
|
) : VariableWithConstraints {
|
||||||
override val constraints: List<Constraint> get() = mutableConstraints
|
override val constraints: List<Constraint> get() {
|
||||||
|
if (simplifiedConstraints == null) {
|
||||||
|
simplifiedConstraints = simplifyConstraints()
|
||||||
|
}
|
||||||
|
return simplifiedConstraints!!
|
||||||
|
}
|
||||||
private val mutableConstraints = ArrayList(constraints)
|
private val mutableConstraints = ArrayList(constraints)
|
||||||
|
|
||||||
|
private var simplifiedConstraints: List<Constraint>? = null
|
||||||
|
|
||||||
// return new actual constraint, if this constraint is new
|
// return new actual constraint, if this constraint is new
|
||||||
fun addConstraint(constraint: Constraint): Constraint? {
|
fun addConstraint(constraint: Constraint): Constraint? {
|
||||||
val previousConstraintWithSameType = constraints.filter { it.typeHashCode == constraint.typeHashCode && it.type == constraint.type }
|
val previousConstraintWithSameType = constraints.filter { it.typeHashCode == constraint.typeHashCode && it.type == constraint.type }
|
||||||
@@ -47,6 +56,7 @@ class MutableVariableWithConstraints(
|
|||||||
constraint
|
constraint
|
||||||
}
|
}
|
||||||
mutableConstraints.add(actualConstraint)
|
mutableConstraints.add(actualConstraint)
|
||||||
|
simplifiedConstraints = null
|
||||||
return actualConstraint
|
return actualConstraint
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,11 +64,13 @@ class MutableVariableWithConstraints(
|
|||||||
// shouldRemove should give true only for tail elements
|
// shouldRemove should give true only for tail elements
|
||||||
internal fun removeLastConstraints(shouldRemove: (Constraint) -> Boolean) {
|
internal fun removeLastConstraints(shouldRemove: (Constraint) -> Boolean) {
|
||||||
mutableConstraints.trimToSize(mutableConstraints.indexOfLast { !shouldRemove(it) } + 1)
|
mutableConstraints.trimToSize(mutableConstraints.indexOfLast { !shouldRemove(it) } + 1)
|
||||||
|
simplifiedConstraints = null
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method should be used only when constraint system has state COMPLETION
|
// This method should be used only when constraint system has state COMPLETION
|
||||||
internal fun removeConstrains(shouldRemove: (Constraint) -> Boolean) {
|
internal fun removeConstrains(shouldRemove: (Constraint) -> Boolean) {
|
||||||
mutableConstraints.removeAll(shouldRemove)
|
mutableConstraints.removeAll(shouldRemove)
|
||||||
|
simplifiedConstraints = null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun newConstraintIsUseless(oldKind: ConstraintKind, newKind: ConstraintKind) =
|
private fun newConstraintIsUseless(oldKind: ConstraintKind, newKind: ConstraintKind) =
|
||||||
@@ -68,6 +80,18 @@ class MutableVariableWithConstraints(
|
|||||||
ConstraintKind.UPPER -> newKind == ConstraintKind.UPPER
|
ConstraintKind.UPPER -> newKind == ConstraintKind.UPPER
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun simplifyConstraints(): List<Constraint> {
|
||||||
|
val equalityConstraints = mutableConstraints
|
||||||
|
.filter { it.kind == ConstraintKind.EQUALITY }
|
||||||
|
.groupBy { it.typeHashCode }
|
||||||
|
return mutableConstraints.filter { isUsefulConstraint(it, equalityConstraints) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isUsefulConstraint(constraint: Constraint, equalityConstraints: Map<Int, List<Constraint>>): Boolean {
|
||||||
|
if (constraint.kind == ConstraintKind.EQUALITY) return true
|
||||||
|
return equalityConstraints[constraint.typeHashCode]?.none { it.type == constraint.type } ?: true
|
||||||
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "Constraints for $typeVariable"
|
return "Constraints for $typeVariable"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user