Skip to content

Commit 2cb6965

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

File tree

1 file changed

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

1 file changed

+19
-5
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import core.*
66
import Contexts.*
77
import ast.tpd.*
88
import util.SourcePosition
9+
import util.SourceFile
910

1011
import 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

Comments
 (0)