From 86f249c50a6e5ad360de6cc65a12092ee2694dad Mon Sep 17 00:00:00 2001 From: odersky Date: Sun, 21 Aug 2022 12:32:30 +0200 Subject: [PATCH] Fix resetInst logic The previous way never reset any type variable since it traversed `ownedVars`, and instantiated TypeVars are no longer in the ownedVars of their owning TyperState. --- .../src/dotty/tools/dotc/core/TyperState.scala | 18 ++++++++---------- compiler/src/dotty/tools/dotc/core/Types.scala | 1 + 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/TyperState.scala b/compiler/src/dotty/tools/dotc/core/TyperState.scala index cea96e29fab6..81b60c608e28 100644 --- a/compiler/src/dotty/tools/dotc/core/TyperState.scala +++ b/compiler/src/dotty/tools/dotc/core/TyperState.scala @@ -25,21 +25,19 @@ object TyperState { type LevelMap = SimpleIdentityMap[TypeVar, Integer] - opaque type Snapshot = (Constraint, TypeVars, TypeVars, LevelMap) + opaque type Snapshot = (Constraint, TypeVars, LevelMap) extension (ts: TyperState) def snapshot()(using Context): Snapshot = - var previouslyInstantiated: TypeVars = SimpleIdentitySet.empty - for tv <- ts.ownedVars do if tv.inst.exists then previouslyInstantiated += tv - (ts.constraint, ts.ownedVars, previouslyInstantiated, ts.upLevels) + (ts.constraint, ts.ownedVars, ts.upLevels) def resetTo(state: Snapshot)(using Context): Unit = - val (c, tvs, previouslyInstantiated, upLevels) = state - for tv <- tvs do - if tv.inst.exists && !previouslyInstantiated.contains(tv) then + val (constraint, ownedVars, upLevels) = state + for tv <- ownedVars do + if !ts.ownedVars.contains(tv) then // tv has been instantiated tv.resetInst(ts) - ts.ownedVars = tvs - ts.constraint = c + ts.constraint = constraint + ts.ownedVars = ownedVars ts.upLevels = upLevels } @@ -190,7 +188,7 @@ class TyperState() { if level < targetState.nestingLevel(tv) then targetState.setNestingLevel(tv, level) } - + targetState.gc() isCommitted = true ownedVars = SimpleIdentitySet.empty diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index e906313510a7..65352aaae219 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -4524,6 +4524,7 @@ object Types { owningState = null // no longer needed; null out to avoid a memory leak private[core] def resetInst(ts: TyperState): Unit = + assert(myInst.exists) myInst = NoType owningState = new WeakReference(ts)