Skip to content

Commit

Permalink
Fix cache for function and lazy vals
Browse files Browse the repository at this point in the history
Otherwise, the test tests/init-global/pos/lazy-local-val2.scala would stackoverflow
  • Loading branch information
liufengyun committed Sep 29, 2024
1 parent f15ec64 commit aa27034
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,10 @@ class Objects(using Context @constructorOnly):
instance

def cachedEval(thisV: ThisValue, expr: Tree, klass: ClassSymbol, ctx: EvalContext): Contextual[Value] =
val env = summon[Env.Data]
ctx match
case EvalContext.Method(meth) =>
// Only perform footprint optimization for method context
val env = summon[Env.Data]
val roots = env.flatten.toSeq :+ thisV :+ State.currentObjectRef
val footprint = Heap.footprint(Heap.getHeapData(), roots, State.currentObjectRef)
val config = Config.Call(thisV, env, footprint)
Expand All @@ -768,7 +768,16 @@ class Objects(using Context @constructorOnly):
val returns = Returns.popHandler(meth)
res.join(returns)

case _ =>
case EvalContext.LazyVal | EvalContext.Function =>
val config = Config.Call(thisV, env, Heap.getHeapData())
val result = super.cachedEval(config, expr, default = Res(Bottom, config.heap, Set.empty)) {
val resValue = cases(expr, thisV, klass)
Res(resValue, Heap.getHeapData(), Heap.getChangeSet())
}
Heap.update(result.heap, result.changeSet)
result.value

case EvalContext.Other =>
cases(expr, thisV, klass)

def cachedWithMemoryOptimization(config: Config, tree: Tree, default: Value)(doEval: => Value)(using Heap.MutableData, State.Data, Trace): Value =
Expand Down

0 comments on commit aa27034

Please sign in to comment.