Skip to content

Commit fde2189

Browse files
authored
Give GadtConstraint a nicer toText (#16085)
2 parents fff8c0d + 1094cbe commit fde2189

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ sealed abstract class GadtConstraint extends Showable {
5656
/** Restore the state from other [[GadtConstraint]], probably copied using [[fresh]] */
5757
def restore(other: GadtConstraint): Unit
5858

59+
/** Provides more information than toText, by showing the underlying Constraint details. */
5960
def debugBoundsDescription(using Context): String
6061
}
6162

@@ -134,7 +135,7 @@ final class ProperGadtConstraint private(
134135

135136
// The replaced symbols are picked up here.
136137
addToConstraint(poly1, tvars)
137-
.showing(i"added to constraint: [$poly1] $params%, %\n$debugBoundsDescription", gadts)
138+
.showing(i"added to constraint: [$poly1] $params%, % gadt = $this", gadts)
138139
}
139140

140141
override def addBound(sym: Symbol, bound: Type, isUpper: Boolean)(using Context): Boolean = {
@@ -291,17 +292,9 @@ final class ProperGadtConstraint private(
291292

292293
override def constr = gadtsConstr
293294

294-
override def toText(printer: Printer): Texts.Text = constraint.toText(printer)
295+
override def toText(printer: Printer): Texts.Text = printer.toText(this)
295296

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-
}
297+
override def debugBoundsDescription(using Context): String = i"$this\n$constraint"
305298
}
306299

307300
@sharable object EmptyGadtConstraint extends GadtConstraint {
@@ -325,7 +318,6 @@ final class ProperGadtConstraint private(
325318
override def restore(other: GadtConstraint): Unit =
326319
assert(!other.isNarrowing, "cannot restore a non-empty GADTMap")
327320

328-
override def debugBoundsDescription(using Context): String = "EmptyGadtConstraint"
329-
330-
override def toText(printer: Printer): Texts.Text = "EmptyGadtConstraint"
321+
override def toText(printer: Printer): Texts.Text = printer.toText(this)
322+
override def debugBoundsDescription(using Context): String = i"$this"
331323
}

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)