Skip to content

Commit bfa800e

Browse files
authored
Two fixes in trace showing (#16200)
2 parents 75f35a9 + 2168103 commit bfa800e

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ package reporting
44

55
import scala.language.unsafeNulls
66

7-
import core.Contexts._
8-
import config.Config
9-
import config.Printers
10-
import core.Mode
7+
import core.*, Contexts.*, Decorators.*
8+
import config.*
9+
import printing.Formatting.*
10+
11+
import scala.compiletime.*
1112

1213
/** Exposes the {{{ trace("question") { op } }}} syntax.
1314
*
@@ -51,9 +52,20 @@ trait TraceSyntax:
5152
else op
5253

5354
inline def apply[T](inline question: String, inline printer: Printers.Printer, inline show: Boolean)(inline op: T)(using Context): T =
54-
inline if isEnabled then
55-
doTrace[T](question, printer, if show then showShowable(_) else alwaysToString)(op)
56-
else op
55+
apply(question, printer, {
56+
val showOp: T => String = inline if show == true then
57+
val showT = summonInline[Show[T]]
58+
{
59+
given Show[T] = showT
60+
t => i"$t"
61+
}
62+
else
63+
summonFrom {
64+
case given Show[T] => t => i"$t"
65+
case _ => alwaysToString
66+
}
67+
showOp
68+
})(op)
5769

5870
inline def apply[T](inline question: String, inline printer: Printers.Printer)(inline op: T)(using Context): T =
5971
apply[T](question, printer, false)(op)
@@ -64,15 +76,11 @@ trait TraceSyntax:
6476
inline def apply[T](inline question: String)(inline op: T)(using Context): T =
6577
apply[T](question, false)(op)
6678

67-
private def showShowable(x: Any)(using Context) = x match
68-
case x: printing.Showable => x.show
69-
case _ => String.valueOf(x)
70-
7179
private val alwaysToString = (x: Any) => String.valueOf(x)
7280

7381
private def doTrace[T](question: => String,
7482
printer: Printers.Printer = Printers.default,
75-
showOp: T => String = alwaysToString)
83+
showOp: T => String)
7684
(op: => T)(using Context): T =
7785
if ctx.mode.is(Mode.Printing) || !isForced && (printer eq Printers.noPrinter) then op
7886
else

0 commit comments

Comments
 (0)