Skip to content

Commit 354b536

Browse files
committed
Respect cacheResult flag in safe initialization cache.
1 parent 3e21f03 commit 354b536

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,41 +85,46 @@ class Cache[Config, Res]:
8585
*
8686
* The algorithmic skeleton is as follows:
8787
*
88+
* if don't cache result then
89+
* return eval(expr)
8890
* if this.current.contains(config, expr) then
8991
* return cached value
9092
* else
9193
* val assumed = this.last(config, expr) or bottom value if absent
9294
* this.current(config, expr) = assumed
93-
* val actual = eval(exp)
95+
* val actual = eval(expr)
9496
*
9597
* if assumed != actual then
9698
* this.changed = true
9799
* this.current(config, expr) = actual
98100
*
99101
*/
100102
def cachedEval(config: Config, expr: Tree, cacheResult: Boolean, default: Res)(eval: Tree => Res): Res =
101-
this.get(config, expr) match
102-
case Some(value) => value
103-
case None =>
104-
val assumeValue: Res =
105-
this.last.get(config, expr) match
106-
case Some(value) => value
107-
case None =>
108-
this.last = this.last.updatedNested(config, expr, default)
109-
default
110-
111-
this.current = this.current.updatedNested(config, expr, assumeValue)
112-
113-
val actual = eval(expr)
114-
if actual != assumeValue then
115-
// println("Changed! from = " + assumeValue + ", to = " + actual)
116-
this.changed = true
117-
// TODO: respect cacheResult to reduce cache size
118-
this.current = this.current.updatedNested(config, expr, actual)
119-
// this.current = this.current.removed(config, expr)
120-
end if
121-
122-
actual
103+
if !cacheResult then
104+
eval(expr)
105+
else
106+
this.get(config, expr) match
107+
case Some(value) => value
108+
case None =>
109+
val assumeValue: Res =
110+
this.last.get(config, expr) match
111+
case Some(value) => value
112+
case None =>
113+
this.last = this.last.updatedNested(config, expr, default)
114+
default
115+
116+
this.current = this.current.updatedNested(config, expr, assumeValue)
117+
118+
val actual = eval(expr)
119+
if actual != assumeValue then
120+
// println("Changed! from = " + assumeValue + ", to = " + actual)
121+
this.changed = true
122+
this.current = this.current.updatedNested(config, expr, actual)
123+
// this.current = this.current.removed(config, expr)
124+
end if
125+
126+
actual
127+
end if
123128
end cachedEval
124129

125130
def hasChanged = changed

tests/init/neg/apply2.scala

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ object O:
33
println(n)
44

55
class B:
6-
val a = A(this)
6+
val a = A(this) // error
77

88
val b = new B
9-
val n = 10 // error
9+
val n = 10
1010
end O

0 commit comments

Comments
 (0)