Skip to content

Commit 216da52

Browse files
committed
Improve trace for TASTy without valid line numbers
Ref: #18882
1 parent 8cb4945 commit 216da52

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ object Objects:
380380
case Some(theValue) =>
381381
theValue
382382
case _ =>
383-
report.warning("[Internal error] Value not found " + x.show + "\nenv = " + data.show + ". Calling trace:\n" + Trace.show, Trace.position)
383+
report.warning("[Internal error] Value not found " + x.show + "\nenv = " + data.show + ". " + Trace.show, Trace.position)
384384
Bottom
385385

386386
def getVal(x: Symbol)(using data: Data, ctx: Context): Option[Value] = data.getVal(x)
@@ -619,7 +619,7 @@ object Objects:
619619
def call(value: Value, meth: Symbol, args: List[ArgInfo], receiver: Type, superType: Type, needResolve: Boolean = true): Contextual[Value] = log("call " + meth.show + ", this = " + value.show + ", args = " + args.map(_.value.show), printer, (_: Value).show) {
620620
value match
621621
case Cold =>
622-
report.warning("Using cold alias. Calling trace:\n" + Trace.show, Trace.position)
622+
report.warning("Using cold alias. " + Trace.show, Trace.position)
623623
Bottom
624624

625625
case Bottom =>
@@ -781,15 +781,15 @@ object Objects:
781781
errorReadOtherStaticObject(State.currentObject, addr.owner)
782782
Bottom
783783
else if ref.isObjectRef && ref.klass.hasSource then
784-
report.warning("Access uninitialized field " + field.show + ". Call trace: " + Trace.show, Trace.position)
784+
report.warning("Access uninitialized field " + field.show + ". " + Trace.show, Trace.position)
785785
Bottom
786786
else
787787
// initialization error, reported by the initialization checker
788788
Bottom
789789
else if ref.hasVal(target) then
790790
ref.valValue(target)
791791
else if ref.isObjectRef && ref.klass.hasSource then
792-
report.warning("Access uninitialized field " + field.show + ". Call trace: " + Trace.show, Trace.position)
792+
report.warning("Access uninitialized field " + field.show + ". " + Trace.show, Trace.position)
793793
Bottom
794794
else
795795
// initialization error, reported by the initialization checker
@@ -836,7 +836,7 @@ object Objects:
836836
report.warning("[Internal error] unexpected tree in assignment, array = " + arr.show + Trace.show, Trace.position)
837837

838838
case Cold =>
839-
report.warning("Assigning to cold aliases is forbidden. Calling trace:\n" + Trace.show, Trace.position)
839+
report.warning("Assigning to cold aliases is forbidden. " + Trace.show, Trace.position)
840840

841841
case Bottom =>
842842

@@ -851,7 +851,7 @@ object Objects:
851851
else
852852
Heap.writeJoin(addr, rhs)
853853
else
854-
report.warning("Mutating a field before its initialization: " + field.show + ". Calling trace:\n" + Trace.show, Trace.position)
854+
report.warning("Mutating a field before its initialization: " + field.show + ". " + Trace.show, Trace.position)
855855
end match
856856

857857
Bottom
@@ -936,7 +936,7 @@ object Objects:
936936
Bottom
937937
end if
938938
case _ =>
939-
report.warning("[Internal error] Variable not found " + sym.show + "\nenv = " + env.show + ". Calling trace:\n" + Trace.show, Trace.position)
939+
report.warning("[Internal error] Variable not found " + sym.show + "\nenv = " + env.show + ". " + Trace.show, Trace.position)
940940
Bottom
941941
else
942942
given Env.Data = env
@@ -948,17 +948,17 @@ object Objects:
948948
given Env.Data = fun.env
949949
eval(fun.code, fun.thisV, fun.klass)
950950
case Cold =>
951-
report.warning("Calling cold by-name alias. Call trace: \n" + Trace.show, Trace.position)
951+
report.warning("Calling cold by-name alias. " + Trace.show, Trace.position)
952952
Bottom
953953
case _: ValueSet | _: Ref | _: OfArray =>
954-
report.warning("[Internal error] Unexpected by-name value " + value.show + ". Calling trace:\n" + Trace.show, Trace.position)
954+
report.warning("[Internal error] Unexpected by-name value " + value.show + ". " + Trace.show, Trace.position)
955955
Bottom
956956
else
957957
value
958958

959959
case _ =>
960960
if isByNameParam(sym) then
961-
report.warning("Calling cold by-name alias. Call trace: \n" + Trace.show, Trace.position)
961+
report.warning("Calling cold by-name alias. " + Trace.show, Trace.position)
962962
Bottom
963963
else
964964
Cold
@@ -983,10 +983,10 @@ object Objects:
983983
else
984984
Heap.writeJoin(addr, value)
985985
case _ =>
986-
report.warning("[Internal error] Variable not found " + sym.show + "\nenv = " + env.show + ". Calling trace:\n" + Trace.show, Trace.position)
986+
report.warning("[Internal error] Variable not found " + sym.show + "\nenv = " + env.show + ". " + Trace.show, Trace.position)
987987

988988
case _ =>
989-
report.warning("Assigning to variables in outer scope. Calling trace:\n" + Trace.show, Trace.position)
989+
report.warning("Assigning to variables in outer scope. " + Trace.show, Trace.position)
990990

991991
Bottom
992992
}
@@ -1716,14 +1716,14 @@ object Objects:
17161716
val msg =
17171717
s"Mutating ${otherObj.show} during initialization of ${currentObj.show}.\n" +
17181718
"Mutating other static objects during the initialization of one static object is forbidden. " +
1719-
"Calling trace:\n" + Trace.show
1719+
"Calling trace:" + Trace.show
17201720

17211721
report.warning(msg, Trace.position)
17221722

17231723
def errorReadOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) =
17241724
val msg =
17251725
"Reading mutable state of " + otherObj.show + " during initialization of " + currentObj.show + ".\n" +
17261726
"Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " +
1727-
"Calling trace: " + Trace.show
1727+
"Calling trace:" + Trace.show
17281728

17291729
report.warning(msg, Trace.position)

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ object Trace:
2222

2323
val empty: Trace = Vector.empty
2424

25+
val CONNECTING_INDENT = "\u2502 " // "| "
26+
val CHILD = "\u251c\u2500\u2500 " // "|-- "
27+
val LAST_CHILD = "\u2514\u2500\u2500 " // "\-- "
28+
2529
extension (trace: Trace)
2630
def add(node: Tree): Trace = trace :+ node
2731
def toVector: Vector[Tree] = trace
2832
def ++(trace2: Trace): Trace = trace ++ trace2
2933

30-
def show(using trace: Trace, ctx: Context): String = buildStacktrace(trace, "\n")
34+
def show(using trace: Trace, ctx: Context): String = buildStacktrace(trace, "Calling trace:" + System.lineSeparator())
3135

3236
def position(using trace: Trace): Tree = trace.last
3337

@@ -41,8 +45,8 @@ object Trace:
4145
var lastLineNum = -1
4246
var lines: mutable.ArrayBuffer[String] = new mutable.ArrayBuffer
4347
trace.foreach { tree =>
48+
val isLastTraceItem = tree `eq` trace.last
4449
val pos = tree.sourcePos
45-
val prefix = "-> "
4650
val line =
4751
if pos.source.exists then
4852
val loc = "[ " + pos.source.file.name + ":" + (pos.line + 1) + " ]"
@@ -52,19 +56,22 @@ object Trace:
5256
tree match
5357
case defDef: DefTree =>
5458
// The definition can be huge, avoid printing the whole definition.
55-
defDef.symbol.show
59+
defDef.symbol.showFullName
5660
case _ =>
57-
tree.show
61+
tree.show.split(System.lineSeparator(), 2).nn.head.nn
62+
5863
val positionMarkerLine =
5964
if pos.exists && pos.source.exists then
60-
positionMarker(pos)
61-
else ""
65+
(if isLastTraceItem then "" else CONNECTING_INDENT)+ positionMarker(pos)
66+
else
67+
""
6268

6369
// always use the more precise trace location
64-
if lastLineNum == pos.line then
70+
if lastLineNum >= 0 && lastLineNum == pos.line then
6571
lines.dropRightInPlace(1)
6672

67-
lines += (prefix + line + "\n" + positionMarkerLine)
73+
val prefix = if isLastTraceItem then LAST_CHILD else CHILD
74+
lines += (prefix + line + System.lineSeparator() + positionMarkerLine)
6875

6976
lastLineNum = pos.line
7077
}
@@ -84,4 +91,4 @@ object Trace:
8491
"^" * math.max(1, pos.endColumn - pos.startColumn)
8592
else "^"
8693

87-
s"$padding$carets\n"
94+
s"$padding$carets" + System.lineSeparator()

0 commit comments

Comments
 (0)