Skip to content

Commit

Permalink
Systematically use -Yprint-debug for low-level printing
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed May 27, 2019
1 parent 3774406 commit 5ffb902
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import scala.annotation.switch

class PlainPrinter(_ctx: Context) extends Printer {
protected[this] implicit def ctx: Context = _ctx.addMode(Mode.Printing)
protected[this] def printDebug = ctx.settings.YprintDebug.value

private[this] var openRecs: List[RecType] = Nil

Expand Down Expand Up @@ -204,13 +205,13 @@ class PlainPrinter(_ctx: Context) extends Printer {
toTextLocal(tpe) ~ " " ~ toText(annot)
case tp: TypeVar =>
if (tp.isInstantiated)
toTextLocal(tp.instanceOpt) ~ (Str("^") provided ctx.settings.YprintDebug.value)
toTextLocal(tp.instanceOpt) ~ (Str("^") provided printDebug)
else {
val constr = ctx.typerState.constraint
val bounds =
if (constr.contains(tp)) ctx.addMode(Mode.Printing).typeComparer.fullBounds(tp.origin)
else TypeBounds.empty
if (bounds.isTypeAlias) toText(bounds.lo) ~ (Str("^") provided ctx.settings.YprintDebug.value)
if (bounds.isTypeAlias) toText(bounds.lo) ~ (Str("^") provided printDebug)
else if (ctx.settings.YshowVarBounds.value) "(" ~ toText(tp.origin) ~ "?" ~ toText(bounds) ~ ")"
else toText(tp.origin)
}
Expand Down Expand Up @@ -507,7 +508,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
else
Text()

nodeName ~ "(" ~ elems ~ tpSuffix ~ ")" ~ (Str(tree.sourcePos.toString) provided ctx.settings.YprintPos.value)
nodeName ~ "(" ~ elems ~ tpSuffix ~ ")" ~ (Str(tree.sourcePos.toString) provided printDebug)
}.close // todo: override in refined printer

def toText(pos: SourcePosition): Text = {
Expand Down
29 changes: 15 additions & 14 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {

override def toTextRef(tp: SingletonType): Text = controlled {
tp match {
case tp: ThisType =>
case tp: ThisType if !printDebug =>
if (tp.cls.isAnonymousClass) return keywordStr("this")
if (tp.cls is ModuleClass) return fullNameString(tp.cls.sourceModule)
case _ =>
Expand All @@ -101,7 +101,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {

override def toTextPrefix(tp: Type): Text = controlled {
def isOmittable(sym: Symbol) =
if (ctx.settings.verbose.value) false
if (printDebug) false
else if (homogenizedView) isEmptyPrefix(sym) // drop <root> and anonymous classes, but not scala, Predef.
else isOmittablePrefix(sym)
tp match {
Expand Down Expand Up @@ -182,7 +182,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
val cls = tycon.typeSymbol
if (tycon.isRepeatedParam) return toTextLocal(args.head) ~ "*"
if (defn.isFunctionClass(cls)) return toTextFunction(args, cls.name.isImplicitFunction, cls.name.isErasedFunction)
if (tp.tupleArity >= 2 && !ctx.settings.YprintDebug.value) return toTextTuple(tp.tupleElementTypes)
if (tp.tupleArity >= 2 && !printDebug) return toTextTuple(tp.tupleElementTypes)
if (isInfixType(tp)) {
val l :: r :: Nil = args
val opName = tyconName(tycon)
Expand All @@ -197,7 +197,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case OrType(tp1, tp2) =>
return toTextInfixType(tpnme.raw.BAR, tp1, tp2) { toText(tpnme.raw.BAR) }

case EtaExpansion(tycon) if !ctx.settings.YprintDebug.value =>
case EtaExpansion(tycon) if !printDebug =>
return toText(tycon)
case tp: RefinedType if defn.isFunctionType(tp) =>
return toTextDependentFunction(tp.refinedInfo.asInstanceOf[MethodType])
Expand Down Expand Up @@ -237,7 +237,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
}
return "[applied to " ~ (Str("given ") provided tp.isContextualMethod) ~ (Str("erased ") provided tp.isErasedMethod) ~ "(" ~ argsText ~ ") returning " ~ toText(resultType) ~ "]"
case IgnoredProto(ignored) =>
return "?" ~ (("(ignored: " ~ toText(ignored) ~ ")") provided ctx.settings.verbose.value)
return "?" ~ (("(ignored: " ~ toText(ignored) ~ ")") provided printDebug)
case tp @ PolyProto(targs, resType) =>
return "[applied to [" ~ toTextGlobal(targs, ", ") ~ "] returning " ~ toText(resType)
case _ =>
Expand All @@ -255,7 +255,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
("{" ~ toText(trees, "\n") ~ "}").close

protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text = {
val isQuote = !ctx.settings.YprintDebug.value && tree.fun.hasType && tree.fun.symbol == defn.InternalQuoted_typeQuote
val isQuote = !printDebug && tree.fun.hasType && tree.fun.symbol == defn.InternalQuoted_typeQuote
val (open, close) = if (isQuote) (keywordStr("'["), keywordStr("]")) else ("[", "]")
val funText = toTextLocal(tree.fun).provided(!isQuote)
tree.fun match {
Expand Down Expand Up @@ -337,9 +337,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
if (name.isTypeName) typeText(txt)
else txt
case tree @ Select(qual, name) =>
if (!ctx.settings.YprintDebug.value && tree.hasType && tree.symbol == defn.QuotedType_splice) typeText("${") ~ toTextLocal(qual) ~ typeText("}")
if (!printDebug && tree.hasType && tree.symbol == defn.QuotedType_splice) typeText("${") ~ toTextLocal(qual) ~ typeText("}")
else if (qual.isType) toTextLocal(qual) ~ "#" ~ typeText(toText(name))
else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided (name != nme.CONSTRUCTOR || ctx.settings.YprintDebug.value))
else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided (name != nme.CONSTRUCTOR || printDebug))
case tree: This =>
optDotPrefix(tree) ~ keywordStr("this") ~ idText(tree)
case Super(qual: This, mix) =>
Expand All @@ -349,9 +349,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
changePrec (GlobalPrec) {
keywordStr("throw ") ~ toText(args.head)
}
else if (!ctx.settings.YprintDebug.value && fun.hasType && fun.symbol == defn.InternalQuoted_exprQuote)
else if (!printDebug && fun.hasType && fun.symbol == defn.InternalQuoted_exprQuote)
keywordStr("'{") ~ toTextGlobal(args, ", ") ~ keywordStr("}")
else if (!ctx.settings.YprintDebug.value && fun.hasType && fun.symbol == defn.InternalQuoted_exprSplice)
else if (!printDebug && fun.hasType && fun.symbol == defn.InternalQuoted_exprSplice)
keywordStr("${") ~ toTextGlobal(args, ", ") ~ keywordStr("}")
else if (app.isGivenApply && !homogenizedView)
changePrec(InfixPrec) {
Expand Down Expand Up @@ -518,7 +518,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case EmptyTree =>
"<empty>"
case TypedSplice(t) =>
if (ctx.settings.YprintDebug.value) "[" ~ toText(t) ~ "]#TS#"
if (printDebug) "[" ~ toText(t) ~ "]#TS#"
else toText(t)
case tree @ ModuleDef(name, impl) =>
withEnclosingDef(tree) {
Expand Down Expand Up @@ -597,7 +597,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case Splice(tree) =>
keywordStr("${") ~ toTextGlobal(dropBlock(tree)) ~ keywordStr("}")
case tree: Applications.IntegratedTypeArgs =>
toText(tree.app) ~ Str("(with integrated type args)").provided(ctx.settings.YprintDebug.value)
toText(tree.app) ~ Str("(with integrated type args)").provided(printDebug)
case Thicket(trees) =>
"Thicket {" ~~ toTextGlobal(trees, "\n") ~~ "}"
case _ =>
Expand Down Expand Up @@ -775,8 +775,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {

protected def templateText(tree: TypeDef, impl: Template): Text = {
val decl = modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"), isType = true)
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
(if (tree.hasType && ctx.settings.verbose.value) i"[decls = ${tree.symbol.info.decls}]" else "")
( decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) }
// ~ (if (tree.hasType && printDebug) i"[decls = ${tree.symbol.info.decls}]" else "") // uncomment to enable
)
}

protected def toTextPackageId[T >: Untyped](pid: Tree[T]): Text =
Expand Down

0 comments on commit 5ffb902

Please sign in to comment.