Skip to content

Commit 7390f25

Browse files
committed
Follow source changes when unpickling
1 parent 2e99976 commit 7390f25

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ class TreeUnpickler(reader: TastyReader,
742742
}
743743

744744
private def readNewDef()(implicit ctx: Context): Tree = {
745+
val sctx = sourceChangeContext
746+
if (sctx `ne` ctx) return readNewDef()(sctx)
745747
val start = currentAddr
746748
val sym = symAtAddr(start)
747749
val tag = readByte()
@@ -867,6 +869,7 @@ class TreeUnpickler(reader: TastyReader,
867869

868870
private def readTemplate(implicit ctx: Context): Template = {
869871
val start = currentAddr
872+
assert(sourcePathAt(start).isEmpty)
870873
val cls = ctx.owner.asClass
871874
val assumedSelfType =
872875
if (cls.is(Module) && cls.owner.isClass) TermRef(cls.owner.thisType, cls.name.sourceModuleName)
@@ -952,6 +955,7 @@ class TreeUnpickler(reader: TastyReader,
952955

953956
def readImport()(implicit ctx: Context): Tree = {
954957
val start = currentAddr
958+
assert(sourcePathAt(start).isEmpty)
955959
readByte()
956960
readEnd()
957961
val expr = readTerm()
@@ -961,6 +965,7 @@ class TreeUnpickler(reader: TastyReader,
961965
def readSelectors()(implicit ctx: Context): List[untpd.Tree] = nextByte match {
962966
case IMPORTED =>
963967
val start = currentAddr
968+
assert(sourcePathAt(start).isEmpty)
964969
readByte()
965970
val from = setPos(start, untpd.Ident(readName()))
966971
nextByte match {
@@ -995,6 +1000,8 @@ class TreeUnpickler(reader: TastyReader,
9951000
// ------ Reading trees -----------------------------------------------------
9961001

9971002
def readTerm()(implicit ctx: Context): Tree = { // TODO: rename to readTree
1003+
val sctx = sourceChangeContext
1004+
if (sctx `ne` ctx) return readTerm()(sctx)
9981005
val start = currentAddr
9991006
val tag = readByte()
10001007
pickling.println(s"reading term ${astTagToString(tag)} at $start")
@@ -1236,6 +1243,8 @@ class TreeUnpickler(reader: TastyReader,
12361243
}
12371244

12381245
def readCase()(implicit ctx: Context): CaseDef = {
1246+
val sctx = sourceChangeContext
1247+
if (sctx `ne` ctx) return readCase()(sctx)
12391248
val start = currentAddr
12401249
assert(readByte() == CASEDEF)
12411250
val end = readEnd()
@@ -1284,6 +1293,25 @@ class TreeUnpickler(reader: TastyReader,
12841293
}
12851294
} else NoPosition
12861295

1296+
/** Pickled source path at `addr`. */
1297+
def sourcePathAt(addr: Addr)(implicit ctx: Context): String =
1298+
if (ctx.mode.is(Mode.ReadPositions)) {
1299+
posUnpicklerOpt match {
1300+
case Some(posUnpickler) =>
1301+
posUnpickler.sourcePathAt(addr)
1302+
case _ =>
1303+
""
1304+
}
1305+
} else ""
1306+
1307+
/** If currentAddr carries a source path, the current context with
1308+
* the source of that path, otherwise the current context itself.
1309+
*/
1310+
def sourceChangeContext(implicit ctx: Context): Context = {
1311+
val path = sourcePathAt(currentAddr)
1312+
if (path.nonEmpty) ctx.withSource(ctx.getSource(path)) else ctx
1313+
}
1314+
12871315
/** Coordinate for the symbol at `addr`. */
12881316
def coordAt(addr: Addr)(implicit ctx: Context): Coord = {
12891317
val pos = posAt(addr)

0 commit comments

Comments
 (0)