Skip to content

Commit 6fd9ce0

Browse files
committed
Cleanup Constraint printing
1 parent 44009a6 commit 6fd9ce0

File tree

4 files changed

+29
-43
lines changed

4 files changed

+29
-43
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,11 @@ abstract class Constraint extends Showable {
197197
*/
198198
def validBoundsFor(param: TypeParamRef, bounds: TypeBounds)(using Context): Type
199199

200-
/** A string that shows the reverse dependencies maintained by this constraint
201-
* (coDeps and contraDeps for OrderingConstraints).
202-
*/
203-
def depsToString(using Context): String
200+
/** The covariant reverse dependencies maintained by this constraint */
201+
def covDeps: SimpleIdentityMap[TypeParamRef, SimpleIdentitySet[TypeParamRef]]
202+
203+
/** The contravariant reverse dependencies maintained by this constraint */
204+
def conDeps: SimpleIdentityMap[TypeParamRef, SimpleIdentitySet[TypeParamRef]]
204205

205206
/** Does the constraint restricted to variables outside `except` depend on `tv`
206207
* in the given direction `co`?

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,8 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
439439
coDeps = coDeps.remove(param)
440440
contraDeps = contraDeps.remove(param)
441441

442-
/** A string representing the two dependency maps */
443-
def depsToString(using Context): String =
444-
def depsStr(deps: ReverseDeps): String =
445-
def depStr(param: TypeParamRef) = i"$param --> ${deps.at(param).toList}%, %"
446-
if deps.isEmpty then "" else i"\n ${deps.toList.map((k, v) => depStr(k))}%\n %"
447-
i" co-deps:${depsStr(coDeps)}\n contra-deps:${depsStr(contraDeps)}\n"
442+
def covDeps: ReverseDeps = coDeps
443+
def conDeps: ReverseDeps = contraDeps
448444

449445
// ---------- Adding TypeLambdas --------------------------------------------------
450446

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

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -717,40 +717,27 @@ class PlainPrinter(_ctx: Context) extends Printer {
717717
try
718718
// The current TyperState constraint determines how type variables are printed
719719
ctx.typerState.constraint = c
720-
def entryText(tp: Type) = tp match {
721-
case tp: TypeBounds =>
722-
toText(tp)
723-
case _ =>
724-
" := " ~ toText(tp)
725-
}
726-
val indent = 3
727-
val uninstVarsText = " uninstantiated variables: " ~
728-
Text(c.uninstVars.map(toText), ", ")
729-
val constrainedText =
730-
" constrained types: " ~ Text(c.domainLambdas.map(toText), ", ")
731-
val boundsText =
732-
" bounds: " ~ {
733-
val assocs =
734-
for (param <- c.domainParams)
735-
yield (" " * indent) ~ toText(param) ~ entryText(c.entry(param))
736-
Text(assocs, "\n")
737-
}
738-
val orderingText =
739-
" ordering: " ~ {
740-
val deps =
741-
for {
742-
param <- c.domainParams
743-
ups = c.minUpper(param)
744-
if ups.nonEmpty
745-
}
746-
yield
747-
(" " * indent) ~ toText(param) ~ " <: " ~
748-
Text(ups.map(toText), ", ")
749-
Text(deps, "\n")
750-
}
751-
val depsText = if Config.showConstraintDeps then c.depsToString else ""
720+
721+
def depsText(k: TypeParamRef, vs: List[TypeParamRef]): Text =
722+
(toText(k) ~ " --> ") ~: ("[" ~ toText(vs, ", ") ~ "]").provided(vs.nonEmpty && Config.showConstraintDeps).close
723+
724+
def orderText(p: TypeParamRef, op: String, doms: List[TypeParamRef], sep: String) =
725+
(toText(p) ~ op) ~: Text(doms.map(toText), sep)
726+
752727
//Printer.debugPrintUnique = false
753-
Text.lines(List(uninstVarsText, constrainedText, boundsText, orderingText, depsText))
728+
val parts = List(
729+
"uninsts: " ~: Text(c.uninstVars.map(toText), ", "),
730+
"lambdas: " ~: Text(c.domainLambdas.map(toText), ", "),
731+
"bounds: " ~: Text(c.domainParams.map(p => toText(p) ~: toText(TypeComparer.fullBounds(p))), ", "),
732+
"uppers: " ~: Text(c.domainParams.map(p => orderText(p, " <: ", c.minUpper(p), " & ")), ", "),
733+
"lowers: " ~: Text(c.domainParams.map(p => orderText(p, " >: ", c.minLower(p), " | ")), ", "),
734+
"covdeps: " ~: Text(c.covDeps.map2((k, vs) => depsText(k, vs.toList)), ", "),
735+
"condeps: " ~: Text(c.conDeps.map2((k, vs) => depsText(k, vs.toList)), ", "),
736+
).filter(!_.isEmpty).map(_.close)
737+
738+
if parts.sizeIs > 1
739+
then "Constr:" ~ Text(parts, "\n")
740+
else "Constr(" ~ Text(parts, ", ") ~ ")"
754741
finally
755742
ctx.typerState.constraint = savedConstraint
756743

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ object Texts {
155155
case Fluid(relems2) if !that.isClosed => Fluid(relems2 :+ this)
156156
case _ => Fluid(that :: this :: Nil)
157157

158+
def ~: (lhs: Text): Text = (lhs ~ this).provided(!isEmpty)
159+
158160
def ~~ (that: Text): Text =
159161
if (this.isEmpty) that
160162
else if (that.isEmpty) this

0 commit comments

Comments
 (0)