Skip to content

Commit 2783476

Browse files
author
EnzeXing
committed
Added second trace and enabled positions for tasty files
1 parent b0ba045 commit 2783476

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class Compiler {
167167
val rctx =
168168
if ctx.settings.Xsemanticdb.value then
169169
ctx.addMode(Mode.ReadPositions)
170+
else if ctx.settings.YcheckInitGlobal.value then
171+
ctx.addMode(Mode.ReadPositions)
170172
else
171173
ctx
172174
new Run(this, rctx)

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Objects:
197197
*
198198
* @param owner The static object whose initialization creates the array.
199199
*/
200-
case class OfArray(owner: ClassSymbol, regions: Regions.Data)(using @constructorOnly ctx: Context) extends ValueElement:
200+
case class OfArray(owner: ClassSymbol, regions: Regions.Data)(using @constructorOnly ctx: Context, @constructorOnly trace: Trace) extends ValueElement:
201201
val klass: ClassSymbol = defn.ArrayClass
202202
val addr: Heap.Addr = Heap.arrayAddr(regions, owner)
203203
def show(using Context) = "OfArray(owner = " + owner.show + ")"
@@ -455,9 +455,11 @@ class Objects:
455455
abstract class Addr:
456456
/** The static object which owns the mutable slot */
457457
def owner: ClassSymbol
458+
def getTrace: Trace = Trace.empty
458459

459460
/** The address for mutable fields of objects. */
460-
private case class FieldAddr(regions: Regions.Data, field: Symbol, owner: ClassSymbol) extends Addr
461+
private case class FieldAddr(regions: Regions.Data, field: Symbol, owner: ClassSymbol)(trace: Trace) extends Addr:
462+
override def getTrace: Trace = trace
461463

462464
/** The address for mutable local variables . */
463465
private case class LocalVarAddr(regions: Regions.Data, sym: Symbol, owner: ClassSymbol) extends Addr
@@ -497,11 +499,11 @@ class Objects:
497499
def localVarAddr(regions: Regions.Data, sym: Symbol, owner: ClassSymbol): Addr =
498500
LocalVarAddr(regions, sym, owner)
499501

500-
def fieldVarAddr(regions: Regions.Data, sym: Symbol, owner: ClassSymbol): Addr =
501-
FieldAddr(regions, sym, owner)
502+
def fieldVarAddr(regions: Regions.Data, sym: Symbol, owner: ClassSymbol)(using Trace): Addr =
503+
FieldAddr(regions, sym, owner)(summon[Trace])
502504

503-
def arrayAddr(regions: Regions.Data, owner: ClassSymbol)(using Context): Addr =
504-
FieldAddr(regions, defn.ArrayClass, owner)
505+
def arrayAddr(regions: Regions.Data, owner: ClassSymbol)(using Trace, Context): Addr =
506+
FieldAddr(regions, defn.ArrayClass, owner)(summon[Trace])
505507

506508
def getHeapData()(using mutable: MutableData): Data = mutable.heap
507509

@@ -654,12 +656,12 @@ class Objects:
654656
if arr.addr.owner == State.currentObject then
655657
Heap.read(arr.addr)
656658
else
657-
errorReadOtherStaticObject(State.currentObject, arr.addr.owner)
659+
errorReadOtherStaticObject(State.currentObject, arr.addr)
658660
Bottom
659661
else if target == defn.Array_update then
660662
assert(args.size == 2, "Incorrect number of arguments for Array update, found = " + args.size)
661663
if arr.addr.owner != State.currentObject then
662-
errorMutateOtherStaticObject(State.currentObject, arr.addr.owner)
664+
errorMutateOtherStaticObject(State.currentObject, arr.addr)
663665
else
664666
Heap.writeJoin(arr.addr, args.tail.head.value)
665667
Bottom
@@ -810,7 +812,7 @@ class Objects:
810812
if addr.owner == State.currentObject then
811813
Heap.read(addr)
812814
else
813-
errorReadOtherStaticObject(State.currentObject, addr.owner)
815+
errorReadOtherStaticObject(State.currentObject, addr)
814816
Bottom
815817
else if ref.isObjectRef && ref.klass.hasSource then
816818
report.warning("Access uninitialized field " + field.show + ". " + Trace.show, Trace.position)
@@ -879,7 +881,7 @@ class Objects:
879881
if ref.hasVar(field) then
880882
val addr = ref.varAddr(field)
881883
if addr.owner != State.currentObject then
882-
errorMutateOtherStaticObject(State.currentObject, addr.owner)
884+
errorMutateOtherStaticObject(State.currentObject, addr)
883885
else
884886
Heap.writeJoin(addr, rhs)
885887
else
@@ -968,7 +970,7 @@ class Objects:
968970
if addr.owner == State.currentObject then
969971
Heap.read(addr)
970972
else
971-
errorReadOtherStaticObject(State.currentObject, addr.owner)
973+
errorReadOtherStaticObject(State.currentObject, addr)
972974
Bottom
973975
end if
974976
case _ =>
@@ -1020,7 +1022,7 @@ class Objects:
10201022
Env.getVar(sym) match
10211023
case Some(addr) =>
10221024
if addr.owner != State.currentObject then
1023-
errorMutateOtherStaticObject(State.currentObject, addr.owner)
1025+
errorMutateOtherStaticObject(State.currentObject, addr)
10241026
else
10251027
Heap.writeJoin(addr, value)
10261028
case _ =>
@@ -1758,19 +1760,23 @@ class Objects:
17581760
else evalType(tref.prefix, thisV, klass, elideObjectAccess = cls.isStatic)
17591761

17601762
val mutateErrorSet: mutable.Set[(ClassSymbol, ClassSymbol)] = mutable.Set.empty
1761-
def errorMutateOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) =
1763+
def errorMutateOtherStaticObject(currentObj: ClassSymbol, addr: Heap.Addr)(using Trace, Context) =
1764+
val otherObj = addr.owner
17621765
if mutateErrorSet.add((currentObj, otherObj)) then
17631766
val msg =
17641767
s"Mutating ${otherObj.show} during initialization of ${currentObj.show}.\n" +
1765-
"Mutating other static objects during the initialization of one static object is forbidden. " + Trace.show
1768+
"Mutating other static objects during the initialization of one static object is forbidden. " + Trace.show +
1769+
"The mutable state is created through: " + Trace.show(using addr.getTrace)
17661770

17671771
report.warning(msg, Trace.position)
17681772

17691773
val readErrorSet: mutable.Set[(ClassSymbol, ClassSymbol)] = mutable.Set.empty
1770-
def errorReadOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) =
1774+
def errorReadOtherStaticObject(currentObj: ClassSymbol, addr: Heap.Addr)(using Trace, Context) =
1775+
val otherObj = addr.owner
17711776
if readErrorSet.add((currentObj, otherObj)) then
17721777
val msg =
17731778
"Reading mutable state of " + otherObj.show + " during initialization of " + currentObj.show + ".\n" +
1774-
"Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " + Trace.show
1779+
"Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " + Trace.show +
1780+
"The mutable state is created through: " + Trace.show(using addr.getTrace)
17751781

17761782
report.warning(msg, Trace.position)

0 commit comments

Comments
 (0)