@@ -103,11 +103,12 @@ class TyperState() {
103103 this
104104
105105 /** A fresh typer state with the same constraint as this one. */
106- def fresh (reporter : Reporter = StoreReporter (this .reporter)): TyperState =
106+ def fresh (reporter : Reporter = StoreReporter (this .reporter),
107+ committable : Boolean = this .isCommittable): TyperState =
107108 util.Stats .record(" TyperState.fresh" )
108109 TyperState ().init(this , this .constraint)
109110 .setReporter(reporter)
110- .setCommittable(this .isCommittable )
111+ .setCommittable(committable )
111112
112113 /** The uninstantiated variables */
113114 def uninstVars : collection.Seq [TypeVar ] = constraint.uninstVars
@@ -182,24 +183,25 @@ class TyperState() {
182183
183184 /** Integrate the constraints from `that` into this TyperState.
184185 *
185- * @pre If `that` is committable, it must not contain any type variable which
186+ * @pre If `this` and ` that` are committable, `that` must not contain any type variable which
186187 * does not exist in `this` (in other words, all its type variables must
187188 * be owned by a common parent of `this` and `that`).
188189 */
189- def mergeConstraintWith (that : TyperState )(using Context ): Unit =
190+ def mergeConstraintWith (that : TyperState )(using Context ): this .type =
191+ if this eq that then return this
192+
190193 that.ensureNotConflicting(constraint)
191194
192- val comparingCtx =
193- if ctx.typerState == this then ctx
194- else ctx.fresh.setTyperState(this )
195+ val comparingCtx = ctx.withTyperState(this )
195196
196- comparing(typeComparer =>
197+ inContext(comparingCtx)( comparing(typeComparer =>
197198 val other = that.constraint
198199 val res = other.domainLambdas.forall(tl =>
199200 // Integrate the type lambdas from `other`
200201 constraint.contains(tl) || other.isRemovable(tl) || {
201202 val tvars = tl.paramRefs.map(other.typeVarOfParam(_)).collect { case tv : TypeVar => tv }
202- tvars.foreach(tvar => if ! tvar.inst.exists && ! isOwnedAnywhere(this , tvar) then includeVar(tvar))
203+ if this .isCommittable then
204+ tvars.foreach(tvar => if ! tvar.inst.exists && ! isOwnedAnywhere(this , tvar) then includeVar(tvar))
203205 typeComparer.addToConstraint(tl, tvars)
204206 }) &&
205207 // Integrate the additional constraints on type variables from `other`
@@ -220,10 +222,11 @@ class TyperState() {
220222 )
221223 )
222224 assert(res || ctx.reporter.errorsReported, i " cannot merge $constraint with $other. " )
223- )( using comparingCtx )
225+ ))
224226
225227 for tl <- constraint.domainLambdas do
226228 if constraint.isRemovable(tl) then constraint = constraint.remove(tl)
229+ this
227230 end mergeConstraintWith
228231
229232 /** Take ownership of `tvar`.
0 commit comments