Skip to content

Commit 8dc0cb5

Browse files
committed
Reset typerState when reducing a match type
It is not enough to reset the constraint only, since type variables might have gotten instantiated during matchtype reduction. With that change we now test in -Ycheck that constraints are empty after each phase.
1 parent 3efcebe commit 8dc0cb5

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
212212
Stats.record(s"total trees at end of $phase", ast.Trees.ntrees)
213213
for (unit <- units)
214214
Stats.record(s"retained typed trees at end of $phase", unit.tpdTree.treeSize)
215+
ctx.typerState.gc()
215216
}
216217

217218
profiler.finished()

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,12 +2876,8 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
28762876
// obviously sound, but quite restrictive. With the current formulation,
28772877
// we need to be careful that `provablyEmpty` covers all the conditions
28782878
// used to conclude disjointness in `provablyDisjoint`.
2879-
if (provablyEmpty(scrut))
2880-
NoType
2881-
else
2882-
val savedConstraint = constraint
2883-
try recur(cases)
2884-
finally constraint = savedConstraint // caseLambda additions are dropped
2879+
if (provablyEmpty(scrut)) NoType
2880+
else recur(cases)
28852881
}
28862882
}
28872883
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4597,10 +4597,16 @@ object Types {
45974597
myReduced =
45984598
trace(i"reduce match type $this $hashCode", matchTypes, show = true) {
45994599
def matchCases(cmp: TrackingTypeComparer): Type =
4600+
val saved = ctx.typerState.snapshot()
46004601
try cmp.matchCases(scrutinee.normalized, cases)
46014602
catch case ex: Throwable =>
46024603
handleRecursive("reduce type ", i"$scrutinee match ...", ex)
4603-
finally updateReductionContext(cmp.footprint)
4604+
finally
4605+
updateReductionContext(cmp.footprint)
4606+
ctx.typerState.resetTo(saved)
4607+
// this drops caseLambdas in constraint and undoes any typevar
4608+
// instantiations during matchtype reduction
4609+
46044610
TypeComparer.tracked(matchCases)
46054611
}
46064612
myReduced

compiler/src/dotty/tools/dotc/transform/TreeChecker.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class TreeChecker extends Phase with SymTransformer {
128128
report.echo(s"checking ${ctx.compilationUnit} after phase ${fusedPhase}")(using ctx)
129129

130130
inContext(ctx) {
131+
assert(ctx.typerState.constraint.domainLambdas.isEmpty,
132+
i"non-empty constraint at end of $fusedPhase: ${ctx.typerState.constraint}, ownedVars = ${ctx.typerState.ownedVars.toList}%, %")
131133
assertSelectWrapsNew(ctx.compilationUnit.tpdTree)
132134
}
133135

0 commit comments

Comments
 (0)