@@ -6,6 +6,7 @@ import core.*
6
6
import Contexts .*
7
7
import ast .tpd .*
8
8
import util .SourcePosition
9
+ import util .SourceFile
9
10
10
11
import Decorators .* , printing .SyntaxHighlighting
11
12
@@ -42,17 +43,30 @@ object Trace:
42
43
43
44
inline def extendTrace [T ](node : Tree )(using t : Trace )(op : Trace ?=> T ): T = op(using t.add(node))
44
45
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
+
45
54
def buildStacktrace (trace : Trace , preamble : String )(using Context ): String = if trace.isEmpty then " " else preamble + {
46
55
var lastLineNum = - 1
47
56
var lines : mutable.ArrayBuffer [String ] = new mutable.ArrayBuffer
48
57
trace.foreach { tree =>
49
58
val isLastTraceItem = tree `eq` trace.last
50
59
val pos = tree.sourcePos
60
+ val hasSource = fileExists(pos.source)
51
61
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
+
56
70
else
57
71
tree match
58
72
case defDef : DefTree =>
@@ -62,7 +76,7 @@ object Trace:
62
76
tree.show.split(System .lineSeparator(), 2 ).head
63
77
64
78
val positionMarkerLine =
65
- if pos.exists && pos.source.exists then
79
+ if pos.exists && hasSource then
66
80
(if isLastTraceItem then EMPTY_PADDING else CONNECTING_INDENT )+ positionMarker(pos)
67
81
else
68
82
" "
0 commit comments