Skip to content

Commit bfc5ff3

Browse files
author
Aleksander Boruch-Gruszecki
committed
Refactor TypeComparer.explained
TypeComparer.explain is easier to just drop where necessary. Also, debugging an ExplainingTypeComparer calls .toString, which destroyed the stack trace previously.
1 parent 1a16f91 commit bfc5ff3

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,9 @@ class TypeComparer(initctx: Context) extends ConstraintHandling {
17701770
totalCount = 0
17711771
}
17721772
}
1773+
1774+
/** Returns last check's debug mode, if explicitly enabled. */
1775+
def lastTrace(): String = ""
17731776
}
17741777

17751778
object TypeComparer {
@@ -1801,11 +1804,21 @@ object TypeComparer {
18011804

18021805
val NoApprox: ApproxState = new ApproxState(0)
18031806

1807+
def explain[T](say: String => Unit)(op: Context => T)(implicit ctx: Context): T = {
1808+
val (res, explanation) = underlyingExplained(op)
1809+
say(explanation)
1810+
res
1811+
}
1812+
18041813
/** Show trace of comparison operations when performing `op` as result string */
18051814
def explained[T](op: Context => T)(implicit ctx: Context): String = {
1815+
underlyingExplained(op)._2
1816+
}
1817+
1818+
private def underlyingExplained[T](op: Context => T)(implicit ctx: Context): (T, String) = {
18061819
val nestedCtx = ctx.fresh.setTypeComparerFn(new ExplainingTypeComparer(_))
1807-
op(nestedCtx)
1808-
nestedCtx.typeComparer.toString
1820+
val res = op(nestedCtx)
1821+
(res, nestedCtx.typeComparer.lastTrace())
18091822
}
18101823
}
18111824

@@ -1937,5 +1950,5 @@ class ExplainingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
19371950

19381951
override def copyIn(ctx: Context): ExplainingTypeComparer = new ExplainingTypeComparer(ctx)
19391952

1940-
override def toString: String = "Subtype trace:" + { try b.toString finally b.clear() }
1953+
override def lastTrace(): String = "Subtype trace:" + { try b.toString finally b.clear() }
19411954
}

0 commit comments

Comments
 (0)