Skip to content

Commit 32f86d0

Browse files
committed
Fix access local var
1 parent c2eac0c commit 32f86d0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ object Objects:
695695
val value = eval(rhs, thisV, klass)
696696

697697
if isLocal then
698+
// TODO: the local var might be from outer environment.
698699
Heap.writeLocalVar(receiver.asInstanceOf[Ref], summon[Env.Data], lhs.symbol, value)
699700
Bottom
700701
else
@@ -755,9 +756,7 @@ object Objects:
755756
if vdef.symbol.is(Flags.Mutable) then
756757
val ref = thisV.asInstanceOf[Ref]
757758
val env = summon[Env.Data]
758-
// Ignore writing to outer locals, will be abstracted by Cold in read.
759-
if Heap.containsLocalVar(ref, env, sym) then
760-
Heap.writeLocalVar(ref, env, sym, rhs)
759+
Heap.writeLocalVar(ref, env, sym, rhs)
761760
else
762761
Env.setLocalVal(vdef.symbol, rhs)
763762

tests/init/pos/global-local-var.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class A(x: Int) {
2+
def foo(): Int = {
3+
val to = x
4+
var sum = 0
5+
var i = 0
6+
while i < to do
7+
sum += i
8+
i += 1
9+
10+
B.a + 10 + sum
11+
}
12+
}
13+
14+
object B {
15+
val a: Int = A(4).foo()
16+
}

0 commit comments

Comments
 (0)