Skip to content

Commit 1d5db00

Browse files
committed
elide base index of sections
1 parent 6e1bba5 commit 1d5db00

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

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

+22-6
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,14 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
9797
sb.append(" tooling: ").append(header.toolingVersion).append("\n")
9898
sb.append(" UUID: ").append(header.uuid).append("\n")
9999
end if
100-
sb.append("\n")
101100

102101
private def printNames(sb: StringBuilder)(using refs: NameRefs): Unit =
103-
sb.append(s"Names (${unpickler.namesEnd.index - unpickler.namesStart.index} bytes, starting from ${unpickler.namesStart.index}):\n")
102+
sb.append(sectionHeader(
103+
name = "Names",
104+
count = (unpickler.namesEnd.index - unpickler.namesStart.index).toString,
105+
base = showBase(unpickler.namesStart.index),
106+
lineEnd = true
107+
))
104108
for ((name, idx) <- nameAtRef.contents.zipWithIndex) {
105109
val index = nameStr("%6d".format(idx))
106110
sb.append(index).append(": ").append(refs.nameRefToString(NameRef(idx))).append("\n")
@@ -187,19 +191,20 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
187191
}
188192
indent -= 2
189193
}
190-
sb.append(s"\n\nTrees (${endAddr.index - startAddr.index} bytes, starting from $base):")
194+
sb.append(sectionHeader("Trees", reader, lineEnd = false))
191195
while (!isAtEnd) {
192196
printTree()
193197
newLine()
194198
}
199+
sb.append("\n")
195200
}
196201
}
197202

198203
class PositionSectionUnpickler(sb: StringBuilder) extends PrinterSectionUnpickler[Unit](PositionsSection) {
199204
def unpickle0(reader: TastyReader)(using tastyName: NameRefs): Unit = {
200205
import reader.*
201206
val posUnpickler = new PositionUnpickler(reader, tastyName)
202-
sb.append(s"\n\nPositions (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
207+
sb.append(sectionHeader("Positions", reader))
203208
val lineSizes = posUnpickler.lineSizes
204209
sb.append(s" lines: ${lineSizes.length}\n")
205210
sb.append(s" line sizes:\n")
@@ -232,7 +237,7 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
232237
import reader.*
233238
val comments = new CommentUnpickler(reader).comments
234239
if !comments.isEmpty then
235-
sb.append(s"\n\nComments (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
240+
sb.append(sectionHeader("Comments", reader))
236241
val sorted = comments.toSeq.sortBy(_._1.index)
237242
for ((addr, cmt) <- sorted) {
238243
sb.append(treeStr("%6d".format(addr.index)))
@@ -245,7 +250,7 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
245250
import dotty.tools.tasty.TastyFormat.*
246251
def unpickle0(reader: TastyReader)(using nameAtRef: NameRefs): Unit = {
247252
import reader.*
248-
sb.append(s"\n\nAttributes (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
253+
sb.append(sectionHeader("Attributes", reader))
249254
while !isAtEnd do
250255
// TODO: Should we elide attributes under testPickler? (i.e.
251256
// if we add new attributes many check files will need to be updated)
@@ -290,6 +295,17 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
290295
}
291296
}
292297

298+
private final def showBase(index: Int): String =
299+
if testPickler then "<elided base index>" else index.toString()
300+
301+
private final def sectionHeader(name: String, reader: TastyReader, lineEnd: Boolean = true): String =
302+
val count = reader.endAddr.index - reader.startAddr.index
303+
sectionHeader(name, count.toString, {showBase(reader.base)}, lineEnd)
304+
305+
private final def sectionHeader(name: String, count: String, base: String, lineEnd: Boolean): String =
306+
val suffix = if lineEnd then "\n" else ""
307+
s"\n$name ($count bytes, starting from $base):$suffix"
308+
293309
abstract class PrinterSectionUnpickler[T](val name: String) {
294310
def unpickle0(reader: TastyReader)(using refs: NameRefs): T
295311
}

compiler/src/dotty/tools/dotc/transform/Pickler.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -305,21 +305,21 @@ class Pickler extends Phase {
305305
end testSame
306306

307307
private def testSamePrinted(printed: String, checkContents: String, cls: ClassSymbol, check: AbstractFile)(using Context): Unit = {
308-
if hasDiff(printed, checkContents) then
308+
for lines <- diff(printed, checkContents) do
309309
output("after-printing.txt", printed)
310310
report.error(em"""TASTy printer difference for $cls in ${cls.source}, did not match ${check},
311-
| output dumped in after-printing.txt, check diff with `git diff --no-index -- after-printing.txt ${check}`
311+
| output dumped in after-printing.txt, check diff with `git diff --no-index -- $check after-printing.txt`
312312
| actual output:
313-
|$printed""")
313+
|$lines%\n%""")
314314
}
315315

316316
/** Reuse diff logic from compiler/test/dotty/tools/vulpix/FileDiff.scala */
317-
private def hasDiff(actual: String, expect: String): Boolean =
317+
private def diff(actual: String, expect: String): Option[Seq[String]] =
318318
import scala.util.Using
319319
import scala.io.Source
320320
val actualLines = Using(Source.fromString(actual))(_.getLines().toList).get
321321
val expectLines = Using(Source.fromString(expect))(_.getLines().toList).get
322-
!matches(actualLines, expectLines)
322+
Option.when(!matches(actualLines, expectLines))(actualLines)
323323

324324
private def matches(actual: String, expect: String): Boolean = {
325325
import java.io.File

tests/pos/i19806/J.tastycheck

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Header:
33
tooling: <elided>
44
UUID: <elided>
55

6-
Names (217 bytes, starting from 80):
6+
Names (217 bytes, starting from <elided base index>):
77
0: ASTs
88
1: p
99
2: J
@@ -31,8 +31,7 @@ Names (217 bytes, starting from 80):
3131
24: Comments
3232
25: Attributes
3333

34-
35-
Trees (145 bytes, starting from 300):
34+
Trees (145 bytes, starting from <elided base index>):
3635
0: PACKAGE(142)
3736
3: TERMREFpkg 1 [p]
3837
5: VALDEF(11) 2 [J]
@@ -113,7 +112,7 @@ Trees (145 bytes, starting from 300):
113112
143: SHAREDtype 38
114113
145:
115114

116-
Positions (145 bytes, starting from 448):
115+
Positions (145 bytes, starting from <elided base index>):
117116
lines: 23
118117
line sizes:
119118
10, 0, 19, 0, 15, 0, 35, 29, 3, 0, 36, 29, 3, 0, 52, 41, 3, 0, 53, 41
@@ -154,8 +153,7 @@ Positions (145 bytes, starting from 448):
154153
source paths:
155154
0: 23 [<elided source file name>]
156155

157-
158-
Attributes (4 bytes, starting from 597):
156+
Attributes (4 bytes, starting from <elided base index>):
159157
JAVAattr
160158
OUTLINEattr
161159
SOURCEFILEattr 23 [<elided source file name>]

0 commit comments

Comments
 (0)