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]
```

[Cherry-picked 931eae4][modified]
  • Loading branch information
WojciechMazur committed Jun 25, 2024
1 parent eeca6f1 commit 9f5c064
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions compiler/src/dotty/tools/dotc/core/tasty/PositionUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dotc
package core
package tasty

import scala.compiletime.uninitialized

import dotty.tools.tasty.{TastyFormat, TastyBuffer, TastyReader}
import TastyFormat.SOURCE
import TastyBuffer.{Addr, NameRef}
Expand All @@ -14,9 +16,9 @@ import Names.TermName
class PositionUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName) {
import reader.*

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

def ensureDefined(): Unit = {
Expand All @@ -29,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 @@ -62,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 @@ -73,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 @@ -216,12 +216,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 9f5c064

Please sign in to comment.