Skip to content

Commit 9f5c064

Browse files
committed
Fix TASTy source position printer
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]
1 parent eeca6f1 commit 9f5c064

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package dotc
33
package core
44
package tasty
55

6+
import scala.compiletime.uninitialized
7+
68
import dotty.tools.tasty.{TastyFormat, TastyBuffer, TastyReader}
79
import TastyFormat.SOURCE
810
import TastyBuffer.{Addr, NameRef}
@@ -14,9 +16,9 @@ import Names.TermName
1416
class PositionUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName) {
1517
import reader.*
1618

17-
private var myLineSizes: Array[Int] = _
18-
private var mySpans: util.HashMap[Addr, Span] = _
19-
private var mySourcePaths: util.HashMap[Addr, String] = _
19+
private var myLineSizes: Array[Int] = uninitialized
20+
private var mySpans: util.HashMap[Addr, Span] = uninitialized
21+
private var mySourceNameRefs: util.HashMap[Addr, NameRef] = uninitialized
2022
private var isDefined = false
2123

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

3133
mySpans = util.HashMap[Addr, Span]()
32-
mySourcePaths = util.HashMap[Addr, String]()
34+
mySourceNameRefs = util.HashMap[Addr, NameRef]()
3335
var curIndex = 0
3436
var curStart = 0
3537
var curEnd = 0
3638
while (!isAtEnd) {
3739
val header = readInt()
3840
if (header == SOURCE) {
39-
val path = nameAtRef(readNameRef()).toString
40-
mySourcePaths(Addr(curIndex)) = path
41+
mySourceNameRefs(Addr(curIndex)) = readNameRef()
4142
}
4243
else {
4344
val addrDelta = header >> 3
@@ -62,9 +63,9 @@ class PositionUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName) {
6263
mySpans
6364
}
6465

65-
private[tasty] def sourcePaths: util.ReadOnlyMap[Addr, String] = {
66+
private[tasty] def sourceNameRefs: util.ReadOnlyMap[Addr, NameRef] = {
6667
ensureDefined()
67-
mySourcePaths
68+
mySourceNameRefs
6869
}
6970

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

7576
def spanAt(addr: Addr): Span = spans.getOrElse(addr, NoSpan)
76-
def sourcePathAt(addr: Addr): String = sourcePaths.getOrElse(addr, "")
77+
def sourcePathAt(addr: Addr): String = sourceNameRefs.get(addr).fold("")(nameAtRef(_).toString)
7778
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ class TastyPrinter(bytes: Array[Byte]) {
216216
sb.append(s": ${offsetToInt(pos.start)} .. ${pos.end}\n")
217217
}
218218

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

project/scripts/cmdTests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ echo "testing sbt scalac -print-tasty"
2727
clear_out "$OUT"
2828
"$SBT" ";scalac $SOURCE -d $OUT ;scalac -print-tasty -color:never $TASTY" > "$tmp"
2929
grep -qe "0: ASTs" "$tmp"
30-
grep -qe "0: tests/pos/HelloWorld.scala" "$tmp"
30+
grep -qe "0: 41 \[tests/pos/HelloWorld.scala\]" "$tmp"
3131

3232
echo "testing that paths SourceFile annotations are relativized"
3333
clear_out "$OUT"

0 commit comments

Comments
 (0)