Skip to content

Commit fff6f48

Browse files
committed
Only cache top-level expressions
1 parent 252f3e4 commit fff6f48

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class Semantic {
169169
if target.is(Flags.Lazy) then value.call(target, superType = NoType, source)
170170
else if target.hasSource then
171171
val rhs = target.defTree.asInstanceOf[ValDef].rhs
172-
eval(rhs, warm, target.owner.asClass)
172+
eval(rhs, warm, target.owner.asClass, cacheResult = true)
173173
else
174174
val error = CallUnknown(field, source, trace)
175175
Result(Hot, error :: Nil)
@@ -205,7 +205,7 @@ class Semantic {
205205
else if target.isOneOf(Flags.Method | Flags.Lazy) then
206206
if target.hasSource then
207207
val rhs = target.defTree.asInstanceOf[DefDef].rhs
208-
eval(rhs, thisRef, target.owner.asClass)
208+
eval(rhs, thisRef, target.owner.asClass, cacheResult = true)
209209
else
210210
val error = CallUnknown(target, source, trace)
211211
Result(Hot, error :: Nil)
@@ -225,19 +225,19 @@ class Semantic {
225225
if target.isOneOf(Flags.Method | Flags.Lazy) then
226226
if target.hasSource then
227227
val rhs = target.defTree.asInstanceOf[DefDef].rhs
228-
eval(rhs, warm, target.owner.asClass)
228+
eval(rhs, warm, target.owner.asClass, cacheResult = true)
229229
else
230230
val error = CallUnknown(target, source, trace)
231231
Result(Hot, error :: Nil)
232232
else if target.hasSource then
233233
val rhs = target.defTree.asInstanceOf[ValDef].rhs
234-
eval(rhs, warm, target.owner.asClass)
234+
eval(rhs, warm, target.owner.asClass, cacheResult = true)
235235
else
236236
val error = CallUnknown(target, source, trace)
237237
Result(Hot, error :: Nil)
238238

239239
case Fun(body, thisV, klass) =>
240-
if meth.name == nme.apply then eval(body, thisV, klass)
240+
if meth.name == nme.apply then eval(body, thisV, klass, cacheResult = true)
241241
else if meth.name.toString == "tupled" then Result(value, Nil)
242242
else Result(Hot, Nil) // TODO: refine
243243

@@ -282,17 +282,17 @@ class Semantic {
282282
*
283283
* This method only handles cache logic and delegates the work to `cases`.
284284
*/
285-
def eval(expr: Tree, thisV: Value, klass: ClassSymbol)(using Context, Trace): Result = log("evaluating " + expr.show, printer, res => res.asInstanceOf[Result].show) {
285+
def eval(expr: Tree, thisV: Value, klass: ClassSymbol, cacheResult: Boolean = false)(using Context, Trace): Result = log("evaluating " + expr.show, printer, res => res.asInstanceOf[Result].show) {
286286
val cfg = Config(thisV, expr.sourcePos)
287287
if (cache.contains(cfg)) Result(cache(cfg), noErrors)
288288
else {
289289
// no need to compute fix-point, because
290290
// 1. the result is decided by `cfg` for a legal program
291291
// (heap change is irrelevant thanks to monotonicity)
292292
// 2. errors will have been reported for an illegal program
293-
cache(cfg) = Hot
293+
if cacheResult then cache(cfg) = Hot
294294
val res = cases(expr, thisV, klass)
295-
cache(cfg) = res.value
295+
if cacheResult then cache(cfg) = res.value
296296
res
297297
}
298298
}
@@ -355,7 +355,7 @@ class Semantic {
355355
case Hot => Result(Hot, errors)
356356
case _ =>
357357
val rhs = id.symbol.defTree.asInstanceOf[DefDef].rhs
358-
eval(rhs, thisValue2, enclosingClass)(using ctx, trace.add(expr))
358+
eval(rhs, thisValue2, enclosingClass, cacheResult = true)(using ctx, trace.add(expr))
359359
case TermRef(prefix, _) =>
360360
val res = cases(prefix, thisV, klass, id) ++ errors
361361
res.call(id.symbol, superType = NoType, source = expr)(using ctx, trace.add(expr))

0 commit comments

Comments
 (0)