Skip to content

Commit

Permalink
Improve trace for TASTy without valid line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Nov 15, 2023
1 parent 8cb4945 commit 6fa5aea
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/init/Trace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ object Trace:

val empty: Trace = Vector.empty

val CONNECTING_INDENT = "\u2502 " // "| "
val CHILD = "\u251c\u2500\u2500 " // "|-- "
val LAST_CHILD = "\u2514\u2500\u2500 " // "\-- "

extension (trace: Trace)
def add(node: Tree): Trace = trace :+ node
def toVector: Vector[Tree] = trace
def ++(trace2: Trace): Trace = trace ++ trace2

def show(using trace: Trace, ctx: Context): String = buildStacktrace(trace, "\n")
def show(using trace: Trace, ctx: Context): String = buildStacktrace(trace, "")

def position(using trace: Trace): Tree = trace.last

Expand All @@ -42,7 +46,6 @@ object Trace:
var lines: mutable.ArrayBuffer[String] = new mutable.ArrayBuffer
trace.foreach { tree =>
val pos = tree.sourcePos
val prefix = "-> "
val line =
if pos.source.exists then
val loc = "[ " + pos.source.file.name + ":" + (pos.line + 1) + " ]"
Expand All @@ -52,18 +55,19 @@ object Trace:
tree match
case defDef: DefTree =>
// The definition can be huge, avoid printing the whole definition.
defDef.symbol.show
defDef.symbol.showFullName
case _ =>
tree.show
tree.show.split(System.lineSeparator(), 2).nn.head.nn
val positionMarkerLine =
if pos.exists && pos.source.exists then
positionMarker(pos)
CONNECTING_INDENT + positionMarker(pos)
else ""

// always use the more precise trace location
if lastLineNum == pos.line then
if lastLineNum >= 0 && lastLineNum == pos.line then
lines.dropRightInPlace(1)

val prefix = if tree `eq` trace.last then LAST_CHILD else CHILD
lines += (prefix + line + "\n" + positionMarkerLine)

lastLineNum = pos.line
Expand Down

0 comments on commit 6fa5aea

Please sign in to comment.