Skip to content

Commit 224732b

Browse files
committed
Show file and line numbers at the beginning in stack trace
1 parent b70f63c commit 224732b

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import ast.tpd._
88
import core._
99
import Decorators._, printing.SyntaxHighlighting
1010
import Types._, Symbols._, Contexts._
11-
import util.{ SimpleIdentityMap, SourcePosition }
11+
import util.{ SimpleIdentityMap, SourcePosition, NoSourcePosition }
1212

1313
import reporting.MessageRendering
14+
import printing.Highlighting
1415

1516
import Effects._, Potentials._
1617

@@ -56,31 +57,33 @@ object Errors {
5657
val padding = pos.startColumnPadding + (" " * offset)
5758
val marker = padding + carets
5859
val textline = padding + msg
59-
"\n" + marker + "\n" + textline
60+
marker + "\n" + textline + "\n"
6061

6162
def stacktrace(using Context): String = if (trace.isEmpty) "" else stacktracePrefix + {
6263
var indentCount = 0
63-
var last: String = ""
64+
var last: SourcePosition = NoSourcePosition
6465
val sb = new StringBuilder
6566
trace.foreach { tree =>
6667
indentCount += 1
6768
val pos = tree.sourcePos
68-
val prefix = s"${ " " * indentCount }-> "
69+
val prefix = (" " * indentCount) + "-> "
70+
var pinpoint = ""
6971
val line =
7072
if pos.source.exists then
71-
val loc = "[ " + pos.source.file.name + ":" + (pos.line + 1) + " ]"
72-
val code = SyntaxHighlighting.highlight(pos.lineContent.stripLineEnd)
73-
val pinpoint =
74-
if !pinpoints.contains(tree) then ""
75-
else pinpointText(pos, pinpoints(tree), indentCount + 3)
73+
val locText = "[ " + pos.source.file.name + ":" + (pos.line + 1) + " ]"
74+
val loc = Highlighting.Blue(locText)
75+
val code = SyntaxHighlighting.highlight(pos.lineContent)
76+
if pinpoints.contains(tree) then
77+
pinpoint = pinpointText(pos, pinpoints(tree), locText.length + prefix.length)
7678

77-
i"$code\t$loc" + pinpoint
79+
i"$loc$prefix$code"
7880
else
7981
tree.show
8082

81-
if (last != line) sb.append(prefix + line + "\n")
83+
if (last.source != pos.source || last.line != pos.line)
84+
sb.append(line + pinpoint)
8285

83-
last = line
86+
last = pos
8487
}
8588
sb.toString
8689
}
@@ -161,7 +164,7 @@ object Errors {
161164

162165
def show(using Context): String = {
163166
var index = 0
164-
"Promoting the value to fully-initialized is unsafe.\n" + stacktrace +
167+
"Promoting the value to fully-initialized is unsafe. " + stacktrace +
165168
"\nThe unsafe promotion may cause the following problem(s):\n" +
166169
(errors.flatMap(_.flatten).map { error =>
167170
index += 1

tests/init/neg/t3273.check

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
-- Error: tests/init/neg/t3273.scala:4:42 ------------------------------------------------------------------------------
22
4 | val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error
33
| ^^^^^^^^^^^^^^^
4-
| Promoting the value to fully-initialized is unsafe.
5-
| Calling trace (full):
6-
| -> val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error [ t3273.scala:4 ]
4+
| Promoting the value to fully-initialized is unsafe. Calling trace (full):
5+
| [ t3273.scala:4 ] -> val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error
76
|
8-
| The unsafe promotion may cause the following problem(s):
7+
| The unsafe promotion may cause the following problem(s):
98
|
10-
| 1. Access non-initialized value num1. Calling trace (full):
11-
| -> val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error [ t3273.scala:4 ]
9+
| 1. Access non-initialized value num1. Calling trace (full):
10+
| [ t3273.scala:4 ] -> val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error
1211
-- Error: tests/init/neg/t3273.scala:5:61 ------------------------------------------------------------------------------
1312
5 | val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error
1413
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
| Promoting the value to fully-initialized is unsafe.
16-
| Calling trace (full):
17-
| -> val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error [ t3273.scala:5 ]
14+
| Promoting the value to fully-initialized is unsafe. Calling trace (full):
15+
| [ t3273.scala:5 ] -> val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error
1816
|
19-
| The unsafe promotion may cause the following problem(s):
17+
| The unsafe promotion may cause the following problem(s):
2018
|
21-
| 1. Access non-initialized value num2. Calling trace (full):
22-
| -> val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error [ t3273.scala:5 ]
19+
| 1. Access non-initialized value num2. Calling trace (full):
20+
| [ t3273.scala:5 ] -> val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error

0 commit comments

Comments
 (0)