Skip to content

Commit

Permalink
Fix TASTy source position printer
Browse files Browse the repository at this point in the history
Now it properly shows that the sources form the position section are
references in the name table. This includes the coloring of the indices
and referenced names.

```diff
 source paths:
-         0: t/Test.scala
+         0: 21 [t/Test.scala]
```
  • Loading branch information
nicolasstucki committed Nov 30, 2023
1 parent 2e4e8fe commit 324f1d7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
13 changes: 6 additions & 7 deletions compiler/src/dotty/tools/dotc/core/tasty/PositionUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PositionUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName) {

private var myLineSizes: Array[Int] = uninitialized
private var mySpans: util.HashMap[Addr, Span] = uninitialized
private var mySourcePaths: util.HashMap[Addr, String] = uninitialized
private var mySourceNameRefs: util.HashMap[Addr, NameRef] = uninitialized
private var isDefined = false

def ensureDefined(): Unit = {
Expand All @@ -31,15 +31,14 @@ class PositionUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName) {
i += 1

mySpans = util.HashMap[Addr, Span]()
mySourcePaths = util.HashMap[Addr, String]()
mySourceNameRefs = util.HashMap[Addr, NameRef]()
var curIndex = 0
var curStart = 0
var curEnd = 0
while (!isAtEnd) {
val header = readInt()
if (header == SOURCE) {
val path = nameAtRef(readNameRef()).toString
mySourcePaths(Addr(curIndex)) = path
mySourceNameRefs(Addr(curIndex)) = readNameRef()
}
else {
val addrDelta = header >> 3
Expand All @@ -64,9 +63,9 @@ class PositionUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName) {
mySpans
}

private[tasty] def sourcePaths: util.ReadOnlyMap[Addr, String] = {
private[tasty] def sourceNameRefs: util.ReadOnlyMap[Addr, NameRef] = {
ensureDefined()
mySourcePaths
mySourceNameRefs
}

private[tasty] def lineSizes: Array[Int] = {
Expand All @@ -75,5 +74,5 @@ class PositionUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName) {
}

def spanAt(addr: Addr): Span = spans.getOrElse(addr, NoSpan)
def sourcePathAt(addr: Addr): String = sourcePaths.getOrElse(addr, "")
def sourcePathAt(addr: Addr): String = sourceNameRefs.get(addr).fold("")(nameAtRef(_).toString)
}
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ class TastyPrinter(bytes: Array[Byte]) {
sb.append(s": ${offsetToInt(pos.start)} .. ${pos.end}\n")
}

val sources = posUnpickler.sourcePaths
val sources = posUnpickler.sourceNameRefs
sb.append(s"\n source paths:\n")
val sortedPath = sources.toSeq.sortBy(_._1.index)
for ((addr, path) <- sortedPath) {
for ((addr, nameRef) <- sortedPath) {
sb.append(treeStr("%6d: ".format(addr.index)))
sb.append(path)
sb.append(nameStr(s"${nameRef.index} [${tastyName(nameRef)}]"))
sb.append("\n")
}

Expand Down
2 changes: 1 addition & 1 deletion project/scripts/cmdTests
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ echo "testing sbt scalac -print-tasty"
clear_out "$OUT"
"$SBT" ";scalac $SOURCE -d $OUT ;scalac -print-tasty -color:never $TASTY" > "$tmp"
grep -qe "0: ASTs" "$tmp"
grep -qe "0: tests/pos/HelloWorld.scala" "$tmp"
grep -qe "0: 41 [tests/pos/HelloWorld.scala]" "$tmp"

echo "testing that paths SourceFile annotations are relativized"
clear_out "$OUT"
Expand Down

0 comments on commit 324f1d7

Please sign in to comment.