Skip to content

Commit f177266

Browse files
committed
Refactor hardenedVars handling
1 parent c1f35aa commit f177266

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
498498
/** Mark toplevel type vars in `tp2` as hard in the current typerState */
499499
def hardenTypeVars(tp2: Type): Unit = tp2.dealiasKeepRefiningAnnots match
500500
case tvar: TypeVar if constraint.contains(tvar.origin) =>
501-
state.hardVars += tvar
501+
state.hardenTypeVar(tvar)
502502
case tp2: TypeParamRef if constraint.contains(tp2) =>
503503
hardenTypeVars(constraint.typeVarOfParam(tp2))
504504
case tp2: AndOrType =>

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ class TyperState() {
9696
* its lower bound, top-level soft unions in the instance type are converted
9797
* to hard unions instead of being widened in `widenOr`.
9898
*/
99-
private var myHardVars: TypeVars = _
100-
def hardVars: TypeVars = myHardVars
101-
def hardVars_=(tvs: TypeVars): Unit = myHardVars = tvs
99+
private var hardVars: TypeVars = _
102100

103101
private var upLevels: LevelMap = _
104102

@@ -112,7 +110,7 @@ class TyperState() {
112110
this.myConstraint = constraint
113111
this.previousConstraint = constraint
114112
this.myOwnedVars = SimpleIdentitySet.empty
115-
this.myHardVars = SimpleIdentitySet.empty
113+
this.hardVars = SimpleIdentitySet.empty
116114
this.upLevels = SimpleIdentityMap.empty
117115
this.isCommitted = false
118116
this
@@ -131,6 +129,12 @@ class TyperState() {
131129
/** The uninstantiated variables */
132130
def uninstVars: collection.Seq[TypeVar] = constraint.uninstVars
133131

132+
/** Register type variable `tv` as hard. */
133+
def hardenTypeVar(tv: TypeVar): Unit = hardVars += tv
134+
135+
/** Is type variable `tv` registered as hard? */
136+
def isHard(tv: TypeVar): Boolean = hardVars.contains(tv)
137+
134138
/** The nestingLevel of `tv` in this typer state */
135139
def nestingLevel(tv: TypeVar): Int =
136140
val own = upLevels(tv)
@@ -195,6 +199,7 @@ class TyperState() {
195199
if !ownedVars.isEmpty then ownedVars.foreach(targetState.includeVar)
196200
else
197201
targetState.mergeConstraintWith(this)
202+
for tv <- hardVars do targetState.hardVars += tv
198203

199204
upLevels.foreachBinding { (tv, level) =>
200205
if level < targetState.nestingLevel(tv) then
@@ -250,7 +255,6 @@ class TyperState() {
250255
val otherLos = other.lower(p)
251256
val otherHis = other.upper(p)
252257
val otherEntry = other.entry(p)
253-
if that.hardVars.contains(tv) then this.myHardVars += tv
254258
( (otherLos eq constraint.lower(p)) || otherLos.forall(_ <:< p)) &&
255259
( (otherHis eq constraint.upper(p)) || otherHis.forall(p <:< _)) &&
256260
((otherEntry eq constraint.entry(p)) || otherEntry.match

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,8 +4721,7 @@ object Types {
47214721
instantiateWith(tp)
47224722

47234723
/** Widen unions when instantiating this variable in the current context? */
4724-
def widenUnions(using Context): Boolean =
4725-
!ctx.typerState.hardVars.contains(this)
4724+
def widenUnions(using Context): Boolean = !ctx.typerState.isHard(this)
47264725

47274726
/** For uninstantiated type variables: the entry in the constraint (either bounds or
47284727
* provisional instance value)

0 commit comments

Comments
 (0)