Skip to content

Commit e5b4f22

Browse files
committed
Use correct env for writing local vars
1 parent 6dd611d commit e5b4f22

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ object Objects:
753753
assert(sym.is(Flags.Mutable), "Writing to immutable variable " + sym.show)
754754
Env.resolveEnv(sym.enclosingMethod, thisV, summon[Env.Data]) match
755755
case Some(thisV -> env) =>
756+
given Env.Data = env
756757
val addr = Env.varAddr(sym)
757758
if addr.owner != State.currentObject then
758759
errorMutateOtherStaticObject(State.currentObject, addr.owner)
@@ -1092,7 +1093,12 @@ object Objects:
10921093
// init param fields
10931094
klass.paramGetters.foreach { acc =>
10941095
val value = paramsMap(acc.name.toTermName)
1095-
thisV.initVal(acc, value)
1096+
if acc.is(Flags.Mutable) then
1097+
val addr = Heap.fieldVarAddr(thisV, acc, State.currentObject)
1098+
thisV.initVar(acc, addr)
1099+
Heap.write(addr, value)
1100+
else
1101+
thisV.initVal(acc, value)
10961102
printer.println(acc.show + " initialized with " + value)
10971103
}
10981104

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import scala.annotation.init
2+
13
object A:
24
class Pair(val f: Int => Unit, val g: () => Int)
35
val p: Pair = foo()
46

57
def foo(): Pair =
68
var x = 6
7-
new Pair(y => x = y, () => x)
8-
9+
new Pair(
10+
(y => x = y): @init.expose, // error
11+
() => x
12+
)
913

1014
object B:
11-
A.p.f(10) // error
15+
A.p.f(10)

0 commit comments

Comments
 (0)