Skip to content

Commit 860d102

Browse files
committed
More flexible tracing
Allow to define what gets shown as a result on a backtrace
1 parent 90a661b commit 860d102

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

compiler/src/dotty/tools/dotc/reporting/trace.scala

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ object trace {
2121
}
2222

2323
@inline
24-
def apply[T](question: => String, printer: Printers.Printer, show: Boolean)(op: => T)(implicit ctx: Context): T = {
24+
def apply[T](question: => String, printer: Printers.Printer, showOp: Any => String)(op: => T)(implicit ctx: Context): T = {
2525
def op1 = op
2626
if (!Config.tracingEnabled || printer.eq(config.Printers.noPrinter)) op1
27-
else doTrace[T](question, printer, show)(op1)
27+
else doTrace[T](question, printer, showOp)(op1)
2828
}
2929

30+
@inline
31+
def apply[T](question: => String, printer: Printers.Printer, show: Boolean)(op: => T)(implicit ctx: Context): T =
32+
apply[T](question, printer, if (show) showShowable(_) else alwaysToString)(op)
33+
3034
@inline
3135
def apply[T](question: => String, printer: Printers.Printer)(op: => T)(implicit ctx: Context): T =
3236
apply[T](question, printer, false)(op)
@@ -39,16 +43,21 @@ object trace {
3943
def apply[T](question: => String)(op: => T)(implicit ctx: Context): T =
4044
apply[T](question, Printers.default, false)(op)
4145

42-
private def doTrace[T](question: => String, printer: Printers.Printer = Printers.default, show: Boolean = false)
43-
(op: => T)(implicit ctx: Context): T = {
44-
def resStr(res: Any): String = res match {
45-
case res: printing.Showable if show => res.show
46-
case _ => String.valueOf(res)
47-
}
46+
private def showShowable(x: Any)(implicit ctx: Context) = x match {
47+
case x: printing.Showable => x.show
48+
case _ => String.valueOf(x)
49+
}
50+
51+
private val alwaysToString = (x: Any) => String.valueOf(x)
52+
53+
private def doTrace[T](question: => String,
54+
printer: Printers.Printer = Printers.default,
55+
showOp: Any => String = alwaysToString)
56+
(op: => T)(implicit ctx: Context): T = {
4857
// Avoid evaluating question multiple time, since each evaluation
4958
// may cause some extra logging output.
5059
lazy val q: String = question
51-
apply[T](s"==> $q?", (res: Any) => s"<== $q = ${resStr(res)}")(op)
60+
apply[T](s"==> $q?", (res: Any) => s"<== $q = ${showOp(res)}")(op)
5261
}
5362

5463
def apply[T](leading: => String, trailing: Any => String)(op: => T)(implicit ctx: Context): T =

0 commit comments

Comments
 (0)