Skip to content

Commit cc8a73e

Browse files
committed
Move instType to Constraint
Move instType from ConstraintHandling to Constraint. It does not rely on any state in ConstraintHandling, but is a pure function of the current constraint. Todo: Do the same for other TypeComparer functions with the same property.
1 parent 659ffeb commit cc8a73e

File tree

7 files changed

+16
-19
lines changed

7 files changed

+16
-19
lines changed

compiler/src/dotty/tools/dotc/core/Constraint.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ abstract class Constraint extends Showable {
121121
/** A new constraint with entry `tl` renamed to a fresh type lambda */
122122
def rename(tl: TypeLambda)(using Context): This
123123

124+
/** Gives for each instantiated type var that does not yet have its `inst` field
125+
* set, the instance value stored in the constraint. Storing instances in constraints
126+
* is done only in a temporary way for contexts that may be retracted
127+
* without also retracting the type var as a whole.
128+
*/
129+
def instType(tvar: TypeVar): Type
130+
124131
/** The given `tl` in case it is not contained in this constraint,
125132
* a fresh copy of `tl` otherwise.
126133
*/

compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,6 @@ trait ConstraintHandling {
6161
assert(homogenizeArgs == false)
6262
assert(comparedTypeLambdas == Set.empty)
6363

64-
/** Gives for each instantiated type var that does not yet have its `inst` field
65-
* set, the instance value stored in the constraint. Storing instances in constraints
66-
* is done only in a temporary way for contexts that may be retracted
67-
* without also retracting the type var as a whole.
68-
*/
69-
def instType(tvar: TypeVar): Type = constraint.entry(tvar.origin) match {
70-
case _: TypeBounds => NoType
71-
case tp: TypeParamRef =>
72-
var tvar1 = constraint.typeVarOfParam(tp)
73-
if (tvar1.exists) tvar1 else tp
74-
case tp => tp
75-
}
76-
7764
def nonParamBounds(param: TypeParamRef)(using Context): TypeBounds = constraint.nonParamBounds(param)
7865

7966
def fullLowerBound(param: TypeParamRef)(using Context): Type =

compiler/src/dotty/tools/dotc/core/GadtConstraint.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ final class ProperGadtConstraint private(
131131
override def addBound(sym: Symbol, bound: Type, isUpper: Boolean)(using Context): Boolean = {
132132
@annotation.tailrec def stripInternalTypeVar(tp: Type): Type = tp match {
133133
case tv: TypeVar =>
134-
val inst = instType(tv)
134+
val inst = constraint.instType(tv)
135135
if (inst.exists) stripInternalTypeVar(inst) else tv
136136
case _ => tp
137137
}

compiler/src/dotty/tools/dotc/core/OrderingConstraint.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
530530
current.checkNonCyclic()
531531
}
532532

533+
def instType(tvar: TypeVar): Type = entry(tvar.origin) match
534+
case _: TypeBounds => NoType
535+
case tp: TypeParamRef => typeVarOfParam(tp).orElse(tp)
536+
case tp => tp
537+
533538
def ensureFresh(tl: TypeLambda)(using Context): TypeLambda =
534539
if (contains(tl)) {
535540
var paramInfos = tl.paramInfos

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,9 +2605,6 @@ object TypeComparer {
26052605
def subtypeCheckInProgress(using Context): Boolean =
26062606
comparing(_.subtypeCheckInProgress)
26072607

2608-
def instType(tvar: TypeVar)(using Context): Type =
2609-
comparing(_.instType(tvar))
2610-
26112608
def instanceType(param: TypeParamRef, fromBelow: Boolean)(using Context): Type =
26122609
comparing(_.instanceType(param, fromBelow))
26132610

compiler/src/dotty/tools/dotc/core/TyperState.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ class TyperState() {
138138
* no-longer needed constraint entries.
139139
*/
140140
def gc()(using Context): Unit = {
141+
Stats.record("typerState.gc")
141142
val toCollect = new mutable.ListBuffer[TypeLambda]
142143
constraint foreachTypeVar { tvar =>
143144
if (!tvar.inst.exists) {
144-
val inst = TypeComparer.instType(tvar)
145+
val inst = constraint.instType(tvar)
145146
if (inst.exists && (tvar.owningState.get eq this)) {
146147
tvar.inst = inst
147148
val lam = tvar.origin.binder

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4192,7 +4192,7 @@ object Types {
41924192
* uninstantiated
41934193
*/
41944194
def instanceOpt(using Context): Type =
4195-
if (inst.exists) inst else TypeComparer.instType(this)
4195+
if (inst.exists) inst else ctx.typerState.constraint.instType(this)
41964196

41974197
/** Is the variable already instantiated? */
41984198
def isInstantiated(using Context): Boolean = instanceOpt.exists

0 commit comments

Comments
 (0)