Skip to content

Commit 7973062

Browse files
committed
Give GadtConstraint a nicer toText
Instead of showing the underlying constraint info, which just looks bad, takes up lots of space, and shows details that aren't relevant and often includes leaked type constructors, just show the symbols and their full bounds, nice and compact. And do that as a part of toText instead of having a side "debugBoundsDescription" alternative.
1 parent afc6ce4 commit 7973062

File tree

5 files changed

+16
-19
lines changed

5 files changed

+16
-19
lines changed

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ sealed abstract class GadtConstraint extends Showable {
5555

5656
/** Restore the state from other [[GadtConstraint]], probably copied using [[fresh]] */
5757
def restore(other: GadtConstraint): Unit
58-
59-
def debugBoundsDescription(using Context): String
6058
}
6159

6260
final class ProperGadtConstraint private(
@@ -134,7 +132,7 @@ final class ProperGadtConstraint private(
134132

135133
// The replaced symbols are picked up here.
136134
addToConstraint(poly1, tvars)
137-
.showing(i"added to constraint: [$poly1] $params%, %\n$debugBoundsDescription", gadts)
135+
.showing(i"added to constraint: [$poly1] $params%, % gadt = $this", gadts)
138136
}
139137

140138
override def addBound(sym: Symbol, bound: Type, isUpper: Boolean)(using Context): Boolean = {
@@ -291,17 +289,7 @@ final class ProperGadtConstraint private(
291289

292290
override def constr = gadtsConstr
293291

294-
override def toText(printer: Printer): Texts.Text = constraint.toText(printer)
295-
296-
override def debugBoundsDescription(using Context): String = {
297-
val sb = new mutable.StringBuilder
298-
sb ++= constraint.show
299-
sb += '\n'
300-
mapping.foreachBinding { case (sym, _) =>
301-
sb ++= i"$sym: ${fullBounds(sym)}\n"
302-
}
303-
sb.result
304-
}
292+
override def toText(printer: Printer): Texts.Text = printer.toText(this)
305293
}
306294

307295
@sharable object EmptyGadtConstraint extends GadtConstraint {
@@ -325,7 +313,5 @@ final class ProperGadtConstraint private(
325313
override def restore(other: GadtConstraint): Unit =
326314
assert(!other.isNarrowing, "cannot restore a non-empty GADTMap")
327315

328-
override def debugBoundsDescription(using Context): String = "EmptyGadtConstraint"
329-
330-
override def toText(printer: Printer): Texts.Text = "EmptyGadtConstraint"
316+
override def toText(printer: Printer): Texts.Text = printer.toText(this)
331317
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ trait PatternTypeConstrainer { self: TypeComparer =>
261261
val assumeInvariantRefinement =
262262
migrateTo3 || forceInvariantRefinement || refinementIsInvariant(patternTp)
263263

264-
trace(i"constraining simple pattern type $tp >:< $pt", gadts, res => s"$res\ngadt = ${ctx.gadt.debugBoundsDescription}") {
264+
trace(i"constraining simple pattern type $tp >:< $pt", gadts, (res: Boolean) => i"$res gadt = ${ctx.gadt}") {
265265
(tp, pt) match {
266266
case (AppliedType(tyconS, argsS), AppliedType(tyconP, argsP)) =>
267267
val saved = state.nn.constraint

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3226,7 +3226,7 @@ class ExplainingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
32263226
}
32273227

32283228
override def gadtAddBound(sym: Symbol, b: Type, isUpper: Boolean): Boolean =
3229-
traceIndented(s"add GADT constraint ${show(sym)} ${if isUpper then "<:" else ">:"} ${show(b)} $frozenNotice, GADT constraint = ${show(ctx.gadt.debugBoundsDescription)}") {
3229+
traceIndented(s"add GADT constraint ${show(sym)} ${if isUpper then "<:" else ">:"} ${show(b)} $frozenNotice, GADT constraint = ${show(ctx.gadt)}") {
32303230
super.gadtAddBound(sym, b, isUpper)
32313231
}
32323232

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,14 @@ class PlainPrinter(_ctx: Context) extends Printer {
693693
finally
694694
ctx.typerState.constraint = savedConstraint
695695

696+
def toText(g: GadtConstraint): Text = g match
697+
case EmptyGadtConstraint => "EmptyGadtConstraint"
698+
case g: ProperGadtConstraint =>
699+
val deps = for sym <- g.symbols yield
700+
val bound = g.fullBounds(sym).nn
701+
(typeText(toText(sym.typeRef)) ~ toText(bound)).close
702+
("GadtConstraint(" ~ Text(deps, ", ") ~ ")").close
703+
696704
def plain: PlainPrinter = this
697705

698706
protected def keywordStr(text: String): String = coloredStr(text, SyntaxHighlighting.KeywordColor)

compiler/src/dotty/tools/dotc/printing/Printer.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ abstract class Printer {
163163
/** Textual representation of a constraint */
164164
def toText(c: OrderingConstraint): Text
165165

166+
/** Textual representation of a GADT constraint */
167+
def toText(c: GadtConstraint): Text
168+
166169
/** Render element within highest precedence */
167170
def toTextLocal(elem: Showable): Text =
168171
atPrec(DotPrec) { elem.toText(this) }

0 commit comments

Comments
 (0)