Revert "Constraint system: add variable dependencies properly"

This reverts commit b2a9d14ad1164e92b0387ac5e9c0a8ad5cd89c2a.
diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt
index db8c1c1..70f56bc 100644
--- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt
+++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArgumentsAnalyzer.kt
@@ -29,7 +29,6 @@
 import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
 import org.jetbrains.kotlin.resolve.calls.inference.addSubtypeConstraintIfCompatible
 import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
-import org.jetbrains.kotlin.resolve.calls.inference.model.UnstableSystemMergeMode
 import org.jetbrains.kotlin.types.model.KotlinTypeMarker
 import org.jetbrains.kotlin.types.model.TypeConstructorMarker
 import org.jetbrains.kotlin.types.model.freshTypeConstructor
@@ -281,8 +280,7 @@
         val returnArguments = returnAtoms.map { it.expression }
 
         if (additionalConstraintStorage != null) {
-            @OptIn(UnstableSystemMergeMode::class)
-            c.mergeOtherSystem(additionalConstraintStorage)
+            c.addOtherSystem(additionalConstraintStorage)
         }
 
         val checkerSink: CheckerSink = CheckerSinkImpl(candidate)
diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzerContext.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzerContext.kt
index 46d1060..1cd05f0 100644
--- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzerContext.kt
+++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/components/PostponedArgumentsAnalyzerContext.kt
@@ -7,7 +7,6 @@
 
 import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
 import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
-import org.jetbrains.kotlin.resolve.calls.inference.model.UnstableSystemMergeMode
 import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints
 import org.jetbrains.kotlin.types.model.*
 
@@ -31,10 +30,6 @@
     // mutable operations
     fun addOtherSystem(otherSystem: ConstraintStorage)
 
-    @K2Only
-    @UnstableSystemMergeMode
-    fun mergeOtherSystem(otherSystem: ConstraintStorage)
-
     fun getBuilder(): ConstraintSystemBuilder
     fun resolveForkPointsConstraints()
 }
diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt
index 753d3b8..5e04d2d 100644
--- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt
+++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/MutableConstraintStorage.kt
@@ -13,7 +13,6 @@
 import org.jetbrains.kotlin.types.model.*
 import org.jetbrains.kotlin.utils.SmartList
 import org.jetbrains.kotlin.utils.addToStdlib.trimToSize
-import java.util.IdentityHashMap
 
 private typealias Context = TypeSystemInferenceExtensionContext
 
@@ -27,14 +26,6 @@
 
     constructor(context: Context, other: VariableWithConstraints) : this(context, other.typeVariable, other.constraints)
 
-    @UnstableSystemMergeMode
-    constructor(context: Context, first: VariableWithConstraints, second: VariableWithConstraints) :
-            this(
-                context,
-                first.typeVariable.also { require(it == second.typeVariable) },
-                identityHashSetFromSum(first.constraints, second.constraints).toList()
-            )
-
     override val constraints: List<Constraint>
         get() {
             if (simplifiedConstraints == null) {
@@ -321,30 +312,8 @@
     internal var outerCS: ConstraintStorage? = null
 }
 
-fun <T> identityHashSetFromSum(first: List<T>, second: List<T>): Set<T> =
-    IdentityHashMap<T, Boolean>().apply {
-        for (elem in first) {
-            put(elem, true)
-        }
-        for (elem in second) {
-            put(elem, true)
-        }
-    }.keys
-
 /**
  * Annotated member is used only for assertion purposes and does not affect semantics
  */
 @RequiresOptIn
 annotation class AssertionsOnly
-
-/**
- * Annotated member is used during "constraint system merge"
- * only for "overload resolution by lambda return type" mode.
- * Please don't use in other modes
- */
-@RequiresOptIn(
-    message = "This member is a part of unstable constraint system merge mode and " +
-            "is intended to be used exclusively for OverloadResolutionByLambdaReturnType resolve. " +
-            "Please don't use in other modes."
-)
-annotation class UnstableSystemMergeMode
diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt
index 0037ada..d1ababa 100644
--- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt
+++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt
@@ -333,7 +333,7 @@
         @OptIn(AssertionsOnly::class)
         runOuterCSRelatedAssertions(outerSystem, isAddingOuter = true)
 
-        doAddOtherSystem(outerSystem, mergeMode = false)
+        doAddOtherSystem(outerSystem)
     }
 
     @K2Only
@@ -356,16 +356,7 @@
         @OptIn(AssertionsOnly::class)
         runOuterCSRelatedAssertions(otherSystem, isAddingOuter = false)
 
-        doAddOtherSystem(otherSystem, mergeMode = false)
-    }
-
-    @K2Only
-    @UnstableSystemMergeMode
-    override fun mergeOtherSystem(otherSystem: ConstraintStorage) {
-        @OptIn(AssertionsOnly::class)
-        runOuterCSRelatedAssertions(otherSystem, isAddingOuter = false)
-
-        doAddOtherSystem(otherSystem, mergeMode = true)
+        doAddOtherSystem(otherSystem)
     }
 
     /**
@@ -391,10 +382,10 @@
             check(otherSystem.allTypeVariables.keys.containsAll(storage.allTypeVariables.keys))
         }
 
-        doAddOtherSystem(otherSystem, mergeMode = false)
+        doAddOtherSystem(otherSystem)
     }
 
-    private fun doAddOtherSystem(otherSystem: ConstraintStorage, mergeMode: Boolean) {
+    private fun doAddOtherSystem(otherSystem: ConstraintStorage) {
         if (otherSystem.allTypeVariables.isNotEmpty()) {
             otherSystem.allTypeVariables.forEach {
                 transactionRegisterVariable(it.value)
@@ -404,56 +395,25 @@
         }
 
         for ((variable, constraints) in otherSystem.notFixedTypeVariables) {
-            if (!mergeMode) {
-                notFixedTypeVariables[variable] = MutableVariableWithConstraints(this, constraints)
-            } else {
-                val previous = notFixedTypeVariables[variable]
-                if (previous != null) {
-                    @OptIn(UnstableSystemMergeMode::class)
-                    notFixedTypeVariables[variable] = MutableVariableWithConstraints(this, previous, constraints)
-                } else {
-                    notFixedTypeVariables[variable] = MutableVariableWithConstraints(this, constraints)
-                }
-            }
+            notFixedTypeVariables[variable] = MutableVariableWithConstraints(this, constraints)
         }
 
         for ((variable, variablesThatReferenceGivenOne) in otherSystem.typeVariableDependencies) {
-            if (!mergeMode || variable !in typeVariableDependencies) {
-                typeVariableDependencies[variable] = variablesThatReferenceGivenOne.toMutableSet()
-            } else {
-                typeVariableDependencies[variable]?.addAll(variablesThatReferenceGivenOne)
-            }
+            typeVariableDependencies[variable] = variablesThatReferenceGivenOne.toMutableSet()
         }
 
-        // Merge mode: filtering identical constraints
-        if (mergeMode) {
-            storage.initialConstraints.addAllDistinct(otherSystem.initialConstraints)
-            storage.constraintsFromAllForkPoints.addAllDistinct(otherSystem.constraintsFromAllForkPoints)
-            storage.errors.addAllDistinct(otherSystem.errors)
-        } else {
-            storage.initialConstraints.addAll(otherSystem.initialConstraints)
-            storage.constraintsFromAllForkPoints.addAll(otherSystem.constraintsFromAllForkPoints)
-            storage.errors.addAll(otherSystem.errors)
-        }
+        storage.initialConstraints.addAll(otherSystem.initialConstraints)
 
         storage.maxTypeDepthFromInitialConstraints =
             max(storage.maxTypeDepthFromInitialConstraints, otherSystem.maxTypeDepthFromInitialConstraints)
-        // Keys are compared by identity only.
-        // Sometimes we create structurally identical type variables (at least in K2),
-        // and they should be considered different.
+        storage.errors.addAll(otherSystem.errors)
         storage.fixedTypeVariables.putAll(otherSystem.fixedTypeVariables)
-        // K1-only, so merge isn't important here
         storage.postponedTypeVariables.addAll(otherSystem.postponedTypeVariables)
+        storage.constraintsFromAllForkPoints.addAll(otherSystem.constraintsFromAllForkPoints)
 
         hasContradictionInForkPointsCache = null
     }
 
-    private fun <T> MutableList<T>.addAllDistinct(other: List<T>) {
-        val set = identityHashSetFromSum(this, other)
-        clear()
-        addAll(set)
-    }
-
     @AssertionsOnly
     private fun runOuterCSRelatedAssertions(otherSystem: ConstraintStorage, isAddingOuter: Boolean) {
         if (!otherSystem.usesOuterCs) return