Skip to content

Commit 581f7e9

Browse files
committed
Fix printing of OrderingConstraint
When printing a constraint, we print types which might refer to type variables defined in the constraint, but the type printer will rely on ctx.typerState.constraint to determine if these type variables are instantiated, and this might be a different constraint than the one we're trying to print, leading to an incorrect output. This commit fixes this by temporarily setting ctx.typerState.constraint to the current constraint when printing it, this required moving the printing logic from OrderingConstraint to PlainPrinter. At the same time, we drop the distinction between `toText` and `contentsToString` (the former wrapped the printed output in "Constraint(...)" and the latter didn't, now we never do) because preserving it would have been complicated and it didn't seem worth it. Also fix Ordering#toString to correctly print the bounds (the logic was there but it was dead code).
1 parent d71e8ef commit 581f7e9

File tree

5 files changed

+48
-46
lines changed

5 files changed

+48
-46
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,4 @@ abstract class Constraint extends Showable {
183183
* of athe type lambda that is associated with the typevar itself.
184184
*/
185185
def checkConsistentVars()(using Context): Unit
186-
187-
/** A string describing the constraint's contents without a header or trailer */
188-
def contentsToString(using Context): String
189186
}

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

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -641,49 +641,10 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
641641
upperMap.foreachBinding((_, paramss) => paramss.foreach(_.foreach(checkClosedType(_, "upper"))))
642642
end checkClosed
643643

644-
// ---------- toText -----------------------------------------------------
645-
646-
private def contentsToText(printer: Printer): Text =
647-
//Printer.debugPrintUnique = true
648-
def entryText(tp: Type) = tp match {
649-
case tp: TypeBounds =>
650-
tp.toText(printer)
651-
case _ =>
652-
" := " ~ tp.toText(printer)
653-
}
654-
val indent = 3
655-
val uninstVarsText = " uninstantiated variables: " ~
656-
Text(uninstVars.map(_.toText(printer)), ", ")
657-
val constrainedText =
658-
" constrained types: " ~ Text(domainLambdas map (_.toText(printer)), ", ")
659-
val boundsText =
660-
" bounds: " ~ {
661-
val assocs =
662-
for (param <- domainParams)
663-
yield (" " * indent) ~ param.toText(printer) ~ entryText(entry(param))
664-
Text(assocs, "\n")
665-
}
666-
val orderingText =
667-
" ordering: " ~ {
668-
val deps =
669-
for {
670-
param <- domainParams
671-
ups = minUpper(param)
672-
if ups.nonEmpty
673-
}
674-
yield
675-
(" " * indent) ~ param.toText(printer) ~ " <: " ~
676-
Text(ups.map(_.toText(printer)), ", ")
677-
Text(deps, "\n")
678-
}
679-
//Printer.debugPrintUnique = false
680-
Text.lines(List(uninstVarsText, constrainedText, boundsText, orderingText))
644+
// ---------- Printing -----------------------------------------------------
681645

682646
override def toText(printer: Printer): Text =
683-
Text.lines(List("Constraint(", contentsToText(printer), ")"))
684-
685-
def contentsToString(using Context): String =
686-
contentsToText(ctx.printer).show
647+
printer.toText(this)
687648

688649
override def toString: String = {
689650
def entryText(tp: Type): String = tp match {
@@ -692,7 +653,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
692653
}
693654
val constrainedText =
694655
" constrained types = " + domainLambdas.mkString("\n")
695-
val boundsText = domainLambdas
656+
val boundsText =
696657
" bounds = " + {
697658
val assocs =
698659
for (param <- domainParams)

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,47 @@ class PlainPrinter(_ctx: Context) extends Printer {
599599
case _ => "{...}"
600600
s"import $exprStr.$selectorStr"
601601

602+
def toText(c: OrderingConstraint): Text =
603+
val savedConstraint = ctx.typerState.constraint
604+
try
605+
// The current TyperState constraint determines how type variables are printed
606+
ctx.typerState.constraint = c
607+
def entryText(tp: Type) = tp match {
608+
case tp: TypeBounds =>
609+
toText(tp)
610+
case _ =>
611+
" := " ~ toText(tp)
612+
}
613+
val indent = 3
614+
val uninstVarsText = " uninstantiated variables: " ~
615+
Text(c.uninstVars.map(toText), ", ")
616+
val constrainedText =
617+
" constrained types: " ~ Text(c.domainLambdas.map(toText), ", ")
618+
val boundsText =
619+
" bounds: " ~ {
620+
val assocs =
621+
for (param <- c.domainParams)
622+
yield (" " * indent) ~ toText(param) ~ entryText(c.entry(param))
623+
Text(assocs, "\n")
624+
}
625+
val orderingText =
626+
" ordering: " ~ {
627+
val deps =
628+
for {
629+
param <- c.domainParams
630+
ups = c.minUpper(param)
631+
if ups.nonEmpty
632+
}
633+
yield
634+
(" " * indent) ~ toText(param) ~ " <: " ~
635+
Text(ups.map(toText), ", ")
636+
Text(deps, "\n")
637+
}
638+
//Printer.debugPrintUnique = false
639+
Text.lines(List(uninstVarsText, constrainedText, boundsText, orderingText))
640+
finally
641+
ctx.typerState.constraint = savedConstraint
642+
602643
def plain: PlainPrinter = this
603644

604645
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
@@ -154,6 +154,9 @@ abstract class Printer {
154154
/** Textual representation of info relating to an import clause */
155155
def toText(result: ImportInfo): Text
156156

157+
/** Textual representation of a constraint */
158+
def toText(c: OrderingConstraint): Text
159+
157160
/** Render element within highest precedence */
158161
def toTextLocal(elem: Showable): Text =
159162
atPrec(DotPrec) { elem.toText(this) }

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ object ErrorReporting {
149149
"the empty constraint"
150150
else
151151
i"""a constraint with:
152-
|${c.contentsToString}"""
152+
|$c"""
153153
i"""
154154
|${TypeComparer.explained(_.isSubType(found, expected), header)}
155155
|

0 commit comments

Comments
 (0)