@@ -11,6 +11,9 @@ import Decorators.*, printing.SyntaxHighlighting
11
11
12
12
import scala .collection .mutable
13
13
14
+ import java .nio .file .Files
15
+ import java .nio .file .Paths
16
+
14
17
/** Logic related to evaluation trace for showing friendly error messages
15
18
*
16
19
* A trace is a sequence of program positions which tells the evaluation order
@@ -42,17 +45,30 @@ object Trace:
42
45
43
46
inline def extendTrace [T ](node : Tree )(using t : Trace )(op : Trace ?=> T ): T = op(using t.add(node))
44
47
48
+ /**
49
+ * Returns whether the source file exists
50
+ *
51
+ * The method SourceFile#exists always return true thus cannot be used.
52
+ */
53
+ def fileExists (path : String ): Boolean =
54
+ Files .exists(Paths .get(path))
55
+
45
56
def buildStacktrace (trace : Trace , preamble : String )(using Context ): String = if trace.isEmpty then " " else preamble + {
46
57
var lastLineNum = - 1
47
58
var lines : mutable.ArrayBuffer [String ] = new mutable.ArrayBuffer
48
59
trace.foreach { tree =>
49
60
val isLastTraceItem = tree `eq` trace.last
50
61
val pos = tree.sourcePos
62
+ val hasSource = fileExists(pos.source.path)
51
63
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"
64
+ if pos.exists then
65
+ val loc = pos.source.file.name + " :" + (pos.line + 1 )
66
+ if hasSource then
67
+ val code = SyntaxHighlighting .highlight(pos.lineContent.trim)
68
+ i " $code\t [ $loc ] "
69
+ else
70
+ loc
71
+
56
72
else
57
73
tree match
58
74
case defDef : DefTree =>
@@ -62,7 +78,7 @@ object Trace:
62
78
tree.show.split(System .lineSeparator(), 2 ).head
63
79
64
80
val positionMarkerLine =
65
- if pos.exists && pos.source.exists then
81
+ if pos.exists && hasSource then
66
82
(if isLastTraceItem then EMPTY_PADDING else CONNECTING_INDENT )+ positionMarker(pos)
67
83
else
68
84
" "
0 commit comments