Skip to content

Commit dde0a0c

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

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

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

Lines changed: 14 additions & 16 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
}
@@ -1715,15 +1715,13 @@ object Objects:
17151715
def errorMutateOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) =
17161716
val msg =
17171717
s"Mutating ${otherObj.show} during initialization of ${currentObj.show}.\n" +
1718-
"Mutating other static objects during the initialization of one static object is forbidden. " +
1719-
"Calling trace:\n" + Trace.show
1718+
"Mutating other static objects during the initialization of one static object is forbidden. " + Trace.show
17201719

17211720
report.warning(msg, Trace.position)
17221721

17231722
def errorReadOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) =
17241723
val msg =
17251724
"Reading mutable state of " + otherObj.show + " during initialization of " + currentObj.show + ".\n" +
1726-
"Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " +
1727-
"Calling trace: " + Trace.show
1725+
"Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " + Trace.show
17281726

17291727
report.warning(msg, Trace.position)

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

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

2323
val empty: Trace = Vector.empty
2424

25+
val EMPTY_PADDING = " "
26+
val CONNECTING_INDENT = "\u2502 " // "| "
27+
val CHILD = "\u251c\u2500\u2500 " // "|-- "
28+
val LAST_CHILD = "\u2514\u2500\u2500 " // "\-- "
29+
2530
extension (trace: Trace)
2631
def add(node: Tree): Trace = trace :+ node
2732
def toVector: Vector[Tree] = trace
2833
def ++(trace2: Trace): Trace = trace ++ trace2
2934

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

3237
def position(using trace: Trace): Tree = trace.last
3338

@@ -41,8 +46,8 @@ object Trace:
4146
var lastLineNum = -1
4247
var lines: mutable.ArrayBuffer[String] = new mutable.ArrayBuffer
4348
trace.foreach { tree =>
49+
val isLastTraceItem = tree `eq` trace.last
4450
val pos = tree.sourcePos
45-
val prefix = "-> "
4651
val line =
4752
if pos.source.exists then
4853
val loc = "[ " + pos.source.file.name + ":" + (pos.line + 1) + " ]"
@@ -52,19 +57,22 @@ object Trace:
5257
tree match
5358
case defDef: DefTree =>
5459
// The definition can be huge, avoid printing the whole definition.
55-
defDef.symbol.show
60+
defDef.symbol.showFullName
5661
case _ =>
57-
tree.show
62+
tree.show.split(System.lineSeparator(), 2).nn.head.nn
63+
5864
val positionMarkerLine =
5965
if pos.exists && pos.source.exists then
60-
positionMarker(pos)
61-
else ""
66+
(if isLastTraceItem then EMPTY_PADDING else CONNECTING_INDENT)+ positionMarker(pos)
67+
else
68+
""
6269

6370
// always use the more precise trace location
64-
if lastLineNum == pos.line then
71+
if lastLineNum >= 0 && lastLineNum == pos.line then
6572
lines.dropRightInPlace(1)
6673

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

6977
lastLineNum = pos.line
7078
}
@@ -78,10 +86,10 @@ object Trace:
7886
*/
7987
private def positionMarker(pos: SourcePosition): String =
8088
val trimmed = pos.source.lineContent(pos.start).takeWhile(c => c.isWhitespace).length
81-
val padding = pos.startColumnPadding.substring(trimmed).nn + " "
89+
val padding = pos.startColumnPadding.substring(trimmed).nn
8290
val carets =
8391
if (pos.startLine == pos.endLine)
8492
"^" * math.max(1, pos.endColumn - pos.startColumn)
8593
else "^"
8694

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

0 commit comments

Comments
 (0)