Skip to content

Commit d7521bf

Browse files
committed
Drop old TyperState based scheme and drop constrainRHSVars
It seems constrainRHSVars is no longer needed with the new way to keep track of hard type variables.
1 parent d15558d commit d7521bf

File tree

3 files changed

+10
-39
lines changed

3 files changed

+10
-39
lines changed

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -485,20 +485,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
485485
false
486486
}
487487

488-
// If LHS is a hard union, constrain any type variables of the RHS with it as lower bound
489-
// before splitting the LHS into its constituents. That way, the RHS variables are
490-
// constrained by the hard union and can be instantiated to it. If we just split and add
491-
// the two parts of the LHS separately to the constraint, the lower bound would become
492-
// a soft union.
493-
def constrainRHSVars(tp2: Type): Boolean = tp2.dealiasKeepRefiningAnnots match
494-
case tp2: TypeParamRef if constraint contains tp2 => compareTypeParamRef(tp2)
495-
case AndType(tp21, tp22) => constrainRHSVars(tp21) && constrainRHSVars(tp22)
496-
case _ => true
497-
498488
/** Mark toplevel type vars in `tp2` as hard in the current constraint */
499489
def hardenTypeVars(tp2: Type): Unit = tp2.dealiasKeepRefiningAnnots match
500490
case tvar: TypeVar if constraint.contains(tvar.origin) =>
501-
state.hardenTypeVar(tvar)
502491
constraint = constraint.withHard(tvar)
503492
case tp2: TypeParamRef if constraint.contains(tp2) =>
504493
hardenTypeVars(constraint.typeVarOfParam(tp2))
@@ -507,9 +496,8 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
507496
hardenTypeVars(tp2.tp2)
508497
case _ =>
509498

510-
val res = widenOK
511-
|| joinOK
512-
|| (tp1.isSoft || constrainRHSVars(tp2)) && recur(tp11, tp2) && recur(tp12, tp2)
499+
val res = widenOK || joinOK
500+
|| recur(tp11, tp2) && recur(tp12, tp2)
513501
|| containsAnd(tp1)
514502
&& !joined
515503
&& {
@@ -525,7 +513,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
525513
// recursively, so we do it only once. See i14870.scala as a test case, which would
526514
// loop for a very long time without the recursion brake.
527515

528-
if res && !tp1.isSoft && state.isCommittable then
516+
if res && !tp1.isSoft && state.isCommittable then
529517
// We use a heuristic here where every toplevel type variable on the right hand side
530518
// is marked so that it converts all soft unions in its lower bound to hard unions
531519
// before it is instantiated. The reason is that the variable's instance type will

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@ object TyperState {
2525

2626
type LevelMap = SimpleIdentityMap[TypeVar, Integer]
2727

28-
opaque type Snapshot = (Constraint, TypeVars, TypeVars, LevelMap)
28+
opaque type Snapshot = (Constraint, TypeVars, LevelMap)
2929

3030
extension (ts: TyperState)
3131
def snapshot()(using Context): Snapshot =
32-
(ts.constraint, ts.ownedVars, ts.hardVars, ts.upLevels)
32+
(ts.constraint, ts.ownedVars, ts.upLevels)
3333

3434
def resetTo(state: Snapshot)(using Context): Unit =
35-
val (constraint, ownedVars, hardVars, upLevels) = state
35+
val (constraint, ownedVars, upLevels) = state
3636
for tv <- ownedVars do
3737
if !ts.ownedVars.contains(tv) then // tv has been instantiated
3838
tv.resetInst(ts)
3939
ts.constraint = constraint
4040
ts.ownedVars = ownedVars
41-
ts.hardVars = hardVars
4241
ts.upLevels = upLevels
4342
}
4443

@@ -92,12 +91,6 @@ class TyperState() {
9291
def ownedVars: TypeVars = myOwnedVars
9392
def ownedVars_=(vs: TypeVars): Unit = myOwnedVars = vs
9493

95-
/** The set of type variables `tv` such that, if `tv` is instantiated to
96-
* its lower bound, top-level soft unions in the instance type are converted
97-
* to hard unions instead of being widened in `widenOr`.
98-
*/
99-
private var hardVars: TypeVars = _
100-
10194
private var upLevels: LevelMap = _
10295

10396
/** Initializes all fields except reporter, isCommittable, which need to be
@@ -110,7 +103,6 @@ class TyperState() {
110103
this.myConstraint = constraint
111104
this.previousConstraint = constraint
112105
this.myOwnedVars = SimpleIdentitySet.empty
113-
this.hardVars = SimpleIdentitySet.empty
114106
this.upLevels = SimpleIdentityMap.empty
115107
this.isCommitted = false
116108
this
@@ -122,19 +114,12 @@ class TyperState() {
122114
val ts = TyperState().init(this, this.constraint)
123115
.setReporter(reporter)
124116
.setCommittable(committable)
125-
ts.hardVars = this.hardVars
126117
ts.upLevels = upLevels
127118
ts
128119

129120
/** The uninstantiated variables */
130121
def uninstVars: collection.Seq[TypeVar] = constraint.uninstVars
131122

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-
138123
/** The nestingLevel of `tv` in this typer state */
139124
def nestingLevel(tv: TypeVar): Int =
140125
val own = upLevels(tv)
@@ -195,11 +180,9 @@ class TyperState() {
195180
constr.println(i"committing $this to $targetState, fromConstr = $constraint, toConstr = ${targetState.constraint}")
196181
if targetState.constraint eq previousConstraint then
197182
targetState.constraint = constraint
198-
targetState.hardVars = hardVars
199183
if !ownedVars.isEmpty then ownedVars.foreach(targetState.includeVar)
200184
else
201185
targetState.mergeConstraintWith(this)
202-
for tv <- hardVars do targetState.hardVars += tv
203186

204187
upLevels.foreachBinding { (tv, level) =>
205188
if level < targetState.nestingLevel(tv) then
@@ -247,7 +230,9 @@ class TyperState() {
247230
val tvars = tl.paramRefs.map(other.typeVarOfParam(_)).collect { case tv: TypeVar => tv }
248231
if this.isCommittable then
249232
tvars.foreach(tvar =>
250-
if !tvar.inst.exists && !isOwnedAnywhere(this, tvar) then includeVar(tvar))
233+
if !tvar.inst.exists then
234+
if !isOwnedAnywhere(this, tvar) then includeVar(tvar)
235+
if constraint.isHard(tvar) then constraint = constraint.withHard(tvar))
251236
typeComparer.addToConstraint(tl, tvars)
252237
}) &&
253238
// Integrate the additional constraints on type variables from `other`

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,9 +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-
if true then !ctx.typerState.constraint.isHard(this)
4726-
else !ctx.typerState.isHard(this)
4724+
def widenUnions(using Context): Boolean = !ctx.typerState.constraint.isHard(this)
47274725

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

0 commit comments

Comments
 (0)