Skip to content

Give GadtConstraint a nicer toText #16085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions compiler/src/dotty/tools/dotc/core/GadtConstraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ sealed abstract class GadtConstraint extends Showable {

/** Restore the state from other [[GadtConstraint]], probably copied using [[fresh]] */
def restore(other: GadtConstraint): Unit

def debugBoundsDescription(using Context): String
}

final class ProperGadtConstraint private(
Expand Down Expand Up @@ -134,7 +132,7 @@ final class ProperGadtConstraint private(

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

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

override def constr = gadtsConstr

override def toText(printer: Printer): Texts.Text = constraint.toText(printer)

override def debugBoundsDescription(using Context): String = {
val sb = new mutable.StringBuilder
sb ++= constraint.show
sb += '\n'
mapping.foreachBinding { case (sym, _) =>
sb ++= i"$sym: ${fullBounds(sym)}\n"
}
sb.result
}
override def toText(printer: Printer): Texts.Text = printer.toText(this)
}

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

override def debugBoundsDescription(using Context): String = "EmptyGadtConstraint"

override def toText(printer: Printer): Texts.Text = "EmptyGadtConstraint"
override def toText(printer: Printer): Texts.Text = printer.toText(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ trait PatternTypeConstrainer { self: TypeComparer =>
val assumeInvariantRefinement =
migrateTo3 || forceInvariantRefinement || refinementIsInvariant(patternTp)

trace(i"constraining simple pattern type $tp >:< $pt", gadts, res => s"$res\ngadt = ${ctx.gadt.debugBoundsDescription}") {
trace(i"constraining simple pattern type $tp >:< $pt", gadts, (res: Boolean) => i"$res gadt = ${ctx.gadt}") {
(tp, pt) match {
case (AppliedType(tyconS, argsS), AppliedType(tyconP, argsP)) =>
val saved = state.nn.constraint
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3226,7 +3226,7 @@ class ExplainingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
}

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

Expand Down
8 changes: 8 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,14 @@ class PlainPrinter(_ctx: Context) extends Printer {
finally
ctx.typerState.constraint = savedConstraint

def toText(g: GadtConstraint): Text = g match
case EmptyGadtConstraint => "EmptyGadtConstraint"
case g: ProperGadtConstraint =>
val deps = for sym <- g.symbols yield
val bound = g.fullBounds(sym).nn
(typeText(toText(sym.typeRef)) ~ toText(bound)).close
("GadtConstraint(" ~ Text(deps, ", ") ~ ")").close

def plain: PlainPrinter = this

protected def keywordStr(text: String): String = coloredStr(text, SyntaxHighlighting.KeywordColor)
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/Printer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ abstract class Printer {
/** Textual representation of a constraint */
def toText(c: OrderingConstraint): Text

/** Textual representation of a GADT constraint */
def toText(c: GadtConstraint): Text

/** Render element within highest precedence */
def toTextLocal(elem: Showable): Text =
atPrec(DotPrec) { elem.toText(this) }
Expand Down