@@ -172,17 +172,6 @@ object Contexts {
172172 protected def searchHistory_= (searchHistory : SearchHistory ): Unit = _searchHistory = searchHistory
173173 final def searchHistory : SearchHistory = _searchHistory
174174
175- /** The current type comparer. This ones updates itself automatically for
176- * each new context.
177- */
178- private var _typeComparer : TypeComparer = _
179- protected def typeComparer_= (typeComparer : TypeComparer ): Unit = _typeComparer = typeComparer
180- def typeComparer : TypeComparer = {
181- if (_typeComparer.comparerCtx ne this )
182- _typeComparer = _typeComparer.copyIn(this )
183- _typeComparer
184- }
185-
186175 /** The current source file */
187176 private var _source : SourceFile = _
188177 protected def source_= (source : SourceFile ): Unit = _source = source
@@ -479,7 +468,6 @@ object Contexts {
479468 _typeAssigner = origin.typeAssigner
480469 _gadt = origin.gadt
481470 _searchHistory = origin.searchHistory
482- _typeComparer = origin.typeComparer
483471 _source = origin.source
484472 _moreProperties = origin.moreProperties
485473 _store = origin.store
@@ -610,7 +598,6 @@ object Contexts {
610598 util.Stats .record(" Context.setSource" )
611599 this .source = source
612600 this
613- def setTypeComparerFn (tcfn : Context => TypeComparer ): this .type = { this .typeComparer = tcfn(this ); this }
614601 private def setMoreProperties (moreProperties : Map [Key [Any ], Any ]): this .type =
615602 util.Stats .record(" Context.setMoreProperties" )
616603 this .moreProperties = moreProperties
@@ -699,26 +686,57 @@ object Contexts {
699686 val base = ctx.base
700687 import base ._
701688 val nestedCtx =
702- if testsInUse < testContexts .size then
703- testContexts(testsInUse ).reuseIn(ctx)
689+ if exploresInUse < exploreContexts .size then
690+ exploreContexts(exploresInUse ).reuseIn(ctx)
704691 else
705692 val ts = TyperState ()
706693 .setReporter(ExploringReporter ())
707694 .setCommittable(false )
708695 val c = FreshContext (ctx.base).init(ctx, ctx).setTyperState(ts)
709- testContexts += c
696+ exploreContexts += c
710697 c
711- testsInUse += 1
698+ exploresInUse += 1
712699 val nestedTS = nestedCtx.typerState
713700 nestedTS.init(ctx.typerState, ctx.typerState.constraint)
714701 val result =
715702 try op(using nestedCtx)
716703 finally
717704 nestedTS.reporter.asInstanceOf [ExploringReporter ].reset()
718- testsInUse -= 1
705+ exploresInUse -= 1
719706 result
720707 end explore
721708
709+ /** The type comparer of the kind created by `maker` to be used.
710+ * This is the currently active type comparer CMP if
711+ * - CMP is associated with the current context, and
712+ * - CMP is of the kind created by maker or maker creates a plain type comparer.
713+ * Note: plain TypeComparers always take on the kind of the outer comparer if they are in the same context.
714+ * In other words: tracking or explaining is a sticky property in the same context.
715+ */
716+ private def comparer (using Context ): TypeComparer =
717+ val base = ctx.base
718+ if base.comparersInUse > 0
719+ && (base.comparers(base.comparersInUse - 1 ).comparerContext eq ctx)
720+ then
721+ base.comparers(base.comparersInUse - 1 ).currentInstance
722+ else
723+ val result =
724+ if base.comparersInUse < base.comparers.size then
725+ base.comparers(base.comparersInUse)
726+ else
727+ val result = TypeComparer (ctx)
728+ base.comparers += result
729+ result
730+ base.comparersInUse += 1
731+ result.init(ctx)
732+ result
733+
734+ inline def comparing [T ](inline op : TypeComparer => T )(using Context ): T =
735+ val saved = ctx.base.comparersInUse
736+ try op(comparer)
737+ finally ctx.base.comparersInUse = saved
738+ end comparing
739+
722740 /** A class defining the initial context with given context base
723741 * and set of possible settings.
724742 */
@@ -735,7 +753,6 @@ object Contexts {
735753 store = initialStore
736754 .updated(settingsStateLoc, settingsGroup.defaultState)
737755 .updated(notNullInfosLoc, Nil )
738- typeComparer = new TypeComparer (using this )
739756 searchHistory = new SearchRoot
740757 gadt = EmptyGadtConstraint
741758 }
@@ -871,14 +888,18 @@ object Contexts {
871888
872889 protected [dotc] val indentTab : String = " "
873890
874- private [dotc] val testContexts = new mutable.ArrayBuffer [FreshContext ]
875- private [dotc] var testsInUse : Int = 0
891+ private [Contexts ] val exploreContexts = new mutable.ArrayBuffer [FreshContext ]
892+ private [Contexts ] var exploresInUse : Int = 0
893+
894+ private [Contexts ] val comparers = new mutable.ArrayBuffer [TypeComparer ]
895+ private [Contexts ] var comparersInUse : Int = 0
876896
877897 def reset (): Unit = {
878898 for ((_, set) <- uniqueSets) set.clear()
879899 errorTypeMsg.clear()
880900 sources.clear()
881901 sourceNamed.clear()
902+ comparers.clear() // forces re-evaluation of top and bottom classes in TypeComparer
882903 }
883904
884905 // Test that access is single threaded
0 commit comments