@@ -6,6 +6,7 @@ import core.*
66import Contexts .*
77import ast .tpd .*
88import util .SourcePosition
9+ import util .SourceFile
910
1011import Decorators .* , printing .SyntaxHighlighting
1112
@@ -42,17 +43,30 @@ object Trace:
4243
4344 inline def extendTrace [T ](node : Tree )(using t : Trace )(op : Trace ?=> T ): T = op(using t.add(node))
4445
46+ /**
47+ * Returns whether the source file exists
48+ *
49+ * The method SourceFile#exists always return true thus cannot be used.
50+ */
51+ def fileExists (source : SourceFile ): Boolean =
52+ source.content().nonEmpty
53+
4554 def buildStacktrace (trace : Trace , preamble : String )(using Context ): String = if trace.isEmpty then " " else preamble + {
4655 var lastLineNum = - 1
4756 var lines : mutable.ArrayBuffer [String ] = new mutable.ArrayBuffer
4857 trace.foreach { tree =>
4958 val isLastTraceItem = tree `eq` trace.last
5059 val pos = tree.sourcePos
60+ val hasSource = fileExists(pos.source)
5161 val line =
52- if pos.source.exists then
53- val loc = " [ " + pos.source.file.name + " :" + (pos.line + 1 ) + " ]"
54- val code = SyntaxHighlighting .highlight(pos.lineContent.trim)
55- i " $code\t $loc"
62+ if pos.exists then
63+ val loc = pos.source.file.name + " :" + (pos.line + 1 )
64+ if hasSource then
65+ val code = SyntaxHighlighting .highlight(pos.lineContent.trim)
66+ i " $code\t [ $loc ] "
67+ else
68+ loc
69+
5670 else
5771 tree match
5872 case defDef : DefTree =>
@@ -62,7 +76,7 @@ object Trace:
6276 tree.show.split(System .lineSeparator(), 2 ).head
6377
6478 val positionMarkerLine =
65- if pos.exists && pos.source.exists then
79+ if pos.exists && hasSource then
6680 (if isLastTraceItem then EMPTY_PADDING else CONNECTING_INDENT )+ positionMarker(pos)
6781 else
6882 " "
0 commit comments