Skip to content

Commit fb848cd

Browse files
committed
Skip caret when source is missing
1 parent afbb66b commit fb848cd

File tree

1 file changed

+21
-5
lines changed
  • compiler/src/dotty/tools/dotc/transform/init

1 file changed

+21
-5
lines changed

compiler/src/dotty/tools/dotc/transform/init/Trace.scala

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import Decorators.*, printing.SyntaxHighlighting
1111

1212
import scala.collection.mutable
1313

14+
import java.nio.file.Files
15+
import java.nio.file.Paths
16+
1417
/** Logic related to evaluation trace for showing friendly error messages
1518
*
1619
* A trace is a sequence of program positions which tells the evaluation order
@@ -42,17 +45,30 @@ object Trace:
4245

4346
inline def extendTrace[T](node: Tree)(using t: Trace)(op: Trace ?=> T): T = op(using t.add(node))
4447

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+
4556
def buildStacktrace(trace: Trace, preamble: String)(using Context): String = if trace.isEmpty then "" else preamble + {
4657
var lastLineNum = -1
4758
var lines: mutable.ArrayBuffer[String] = new mutable.ArrayBuffer
4859
trace.foreach { tree =>
4960
val isLastTraceItem = tree `eq` trace.last
5061
val pos = tree.sourcePos
62+
val hasSource = fileExists(pos.source.path)
5163
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+
5672
else
5773
tree match
5874
case defDef: DefTree =>
@@ -62,7 +78,7 @@ object Trace:
6278
tree.show.split(System.lineSeparator(), 2).head
6379

6480
val positionMarkerLine =
65-
if pos.exists && pos.source.exists then
81+
if pos.exists && hasSource then
6682
(if isLastTraceItem then EMPTY_PADDING else CONNECTING_INDENT)+ positionMarker(pos)
6783
else
6884
""

0 commit comments

Comments
 (0)