Skip to content

Commit 69521b3

Browse files
committed
Context-sensitive closures
1 parent 9bd05b6 commit 69521b3

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ object Objects:
163163
*
164164
* @param owner The static object whose initialization creates the array.
165165
*/
166-
case class OfArray(owner: ClassSymbol)(using @constructorOnly ctx: Context)
166+
case class OfArray(owner: ClassSymbol, regions: Regions.Data)(using @constructorOnly ctx: Context)
167167
extends Ref(valsMap = mutable.Map.empty, varsMap = mutable.Map.empty, outersMap = mutable.Map.empty):
168168
val klass: ClassSymbol = defn.ArrayClass
169-
val addr: Heap.Addr = Heap.arrayAddr(this, owner)
169+
val addr: Heap.Addr = Heap.arrayAddr(regions, owner)
170170
def show(using Context) = "OfArray(owner = " + owner.show + ")"
171171

172172
/**
@@ -214,6 +214,7 @@ object Objects:
214214
given Trace = Trace.empty.add(classSym.defTree)
215215
given Env.Data = Env.emptyEnv(tpl.constr.symbol)
216216
given Heap.MutableData = Heap.empty()
217+
given regions: Regions.Data = Regions.empty // explicit name to avoid naming conflict
217218

218219
val obj = ObjectRef(classSym)
219220
log("Iteration " + count) {
@@ -281,7 +282,6 @@ object Objects:
281282
/** Local environments can be deeply nested, therefore we need `outer`.
282283
*
283284
* For local variables in rhs of class field definitions, the `meth` is the primary constructor.
284-
*
285285
*/
286286
private case class LocalEnv
287287
(private[Env] val params: Map[Symbol, Value], meth: Symbol, outer: Data)
@@ -407,10 +407,10 @@ object Objects:
407407
def owner: ClassSymbol
408408

409409
/** The address for mutable fields of objects. */
410-
private case class FieldAddr(ref: Ref, field: Symbol, owner: ClassSymbol) extends Addr
410+
private case class FieldAddr(regions: Regions.Data, field: Symbol, owner: ClassSymbol) extends Addr
411411

412412
/** The address for mutable local variables . */
413-
private case class LocalVarAddr(ref: Ref, env: Env.Data, sym: Symbol, owner: ClassSymbol) extends Addr
413+
private case class LocalVarAddr(regions: Regions.Data, sym: Symbol, owner: ClassSymbol) extends Addr
414414

415415
/** Immutable heap data used in the cache.
416416
*
@@ -444,14 +444,14 @@ object Objects:
444444
def write(addr: Addr, value: Value)(using mutable: MutableData): Unit =
445445
mutable.update(addr, value)
446446

447-
def localVarAddr(ref: Ref, env: Env.Data, sym: Symbol, owner: ClassSymbol): Addr =
448-
LocalVarAddr(ref, env, sym, owner)
447+
def localVarAddr(regions: Regions.Data, sym: Symbol, owner: ClassSymbol): Addr =
448+
LocalVarAddr(regions, sym, owner)
449449

450-
def fieldVarAddr(ref: Ref, sym: Symbol, owner: ClassSymbol): Addr =
451-
FieldAddr(ref, sym, owner)
450+
def fieldVarAddr(regions: Regions.Data, sym: Symbol, owner: ClassSymbol): Addr =
451+
FieldAddr(regions, sym, owner)
452452

453-
def arrayAddr(ref: Ref, owner: ClassSymbol): Addr =
454-
FieldAddr(ref, NoSymbol, owner)
453+
def arrayAddr(regions: Regions.Data, owner: ClassSymbol)(using Context): Addr =
454+
FieldAddr(regions, defn.ArrayClass, owner)
455455

456456
def getHeapData()(using mutable: MutableData): Data = mutable.heap
457457

@@ -482,7 +482,7 @@ object Objects:
482482
opaque type Data = List[SourcePosition]
483483
val empty: Data = Nil
484484
def extend(pos: SourcePosition)(using data: Data): Data = pos :: data
485-
def exists(pos: SourcePosition)(using data: Data): Data = data.indexOf(pos) >= 0
485+
def exists(pos: SourcePosition)(using data: Data): Boolean = data.indexOf(pos) >= 0
486486

487487
inline def cache(using c: Cache.Data): Cache.Data = c
488488

@@ -735,7 +735,7 @@ object Objects:
735735
// The outer can be a bottom value for top-level classes.
736736

737737
if klass == defn.ArrayClass then
738-
val arr = OfArray(State.currentObject)
738+
val arr = OfArray(State.currentObject, summon[Regions.Data])
739739
Heap.write(arr.addr, Bottom)
740740
arr
741741
else
@@ -761,7 +761,7 @@ object Objects:
761761

762762
def initLocal(ref: Ref, sym: Symbol, value: Value): Contextual[Unit] = log("initialize local " + sym.show + " with " + value.show, printer) {
763763
if sym.is(Flags.Mutable) then
764-
val addr = Heap.localVarAddr(ref, summon[Env.Data], sym, State.currentObject)
764+
val addr = Heap.localVarAddr(summon[Regions.Data], sym, State.currentObject)
765765
Env.setLocalVar(sym, addr)
766766
Heap.write(addr, value)
767767
else
@@ -1161,7 +1161,7 @@ object Objects:
11611161
klass.paramGetters.foreach { acc =>
11621162
val value = paramsMap(acc.name.toTermName)
11631163
if acc.is(Flags.Mutable) then
1164-
val addr = Heap.fieldVarAddr(thisV, acc, State.currentObject)
1164+
val addr = Heap.fieldVarAddr(summon[Regions.Data], acc, State.currentObject)
11651165
thisV.initVar(acc, addr)
11661166
Heap.write(addr, value)
11671167
else
@@ -1256,7 +1256,7 @@ object Objects:
12561256
val res = eval(vdef.rhs, thisV, klass)
12571257
val sym = vdef.symbol
12581258
if sym.is(Flags.Mutable) then
1259-
val addr = Heap.fieldVarAddr(thisV, sym, State.currentObject)
1259+
val addr = Heap.fieldVarAddr(summon[Regions.Data], sym, State.currentObject)
12601260
thisV.initVar(sym, addr)
12611261
Heap.write(addr, res)
12621262
else

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,4 @@ object Util:
104104
/** Whether the class or its super class/trait contains any mutable fields? */
105105
def isMutable(cls: ClassSymbol)(using Context): Boolean =
106106
cls.classInfo.decls.exists(_.is(Flags.Mutable)) ||
107-
cls.parentSyms.exists(parentCls => isMutable(parentCls))
107+
cls.parentSyms.exists(parentCls => isMutable(parentCls.asClass))

0 commit comments

Comments
 (0)