Skip to content

Commit a812450

Browse files
committed
More refined printing options
When using RefinedPrinter we now have a choice whether when printing a definition tree such as def foo(x: T): U we print the parameter and result type info found in the tree or in the symbol. Previously, we printed the sym info when after typer and the tree info before. This turns out to be too inflexble. With the patch, we print the sym info if option -Yprint-syms is set, and the tree info otherwise. Also, align -Yno-deep-subtypes from camelCase to standard hyphenated option notation. Tweak where unique ids are printed.
1 parent 0876dee commit a812450

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ class ScalaSettings extends Settings.SettingGroup {
145145
val noSelfCheck = BooleanSetting("-Yno-self-type-checks", "Suppress check for self-type conformance among inherited members.")
146146
val YshowSuppressedErrors = BooleanSetting("-Yshow-suppressed-errors", "Also show follow-on errors and warnings that are normally supressed.")
147147
val Yheartbeat = BooleanSetting("-Yheartbeat", "show heartbeat stack trace of compiler operations.")
148-
val Yprintpos = BooleanSetting("-Yprintpos", "show tree positions")
149-
val YnoDeepSubtypes = BooleanSetting("-YnoDeepSubtypes", "throw an exception on deep subtyping call stacks")
148+
val Yprintpos = BooleanSetting("-Yprintpos", "show tree positions.")
149+
val YnoDeepSubtypes = BooleanSetting("-Yno-deep-subtypes", "throw an exception on deep subtyping call stacks.")
150+
val YprintSyms = BooleanSetting("-Yprint-syms", "when printing trees print info in symbols instead of corresponding info in trees.")
150151
def stop = YstopAfter
151152

152153
/** Area-specific debug output.

src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
180180

181181
def annotText(tree: untpd.Tree): Text = "@" ~ constrText(tree) // DD
182182

183-
def useSymbol = ctx.isAfterTyper(ctx.phase) && tree.hasType && tree.symbol.exists
183+
def useSymbol =
184+
tree.hasType && tree.symbol.exists && ctx.settings.YprintSyms.value
184185

185186
def modText(mods: untpd.Modifiers, kw: String): Text = { // DD
186187
val suppressKw = if (ownerIsClass) mods is ParamAndLocal else mods is Param
@@ -209,6 +210,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
209210
if (ctx.settings.uniqid.value && tree.hasType && tree.symbol.exists) s"#${tree.symbol.id}" else ""
210211
}
211212

213+
def nameIdText(tree: untpd.NameTree): Text =
214+
toText(tree.name) ~ idText(tree)
215+
212216
import untpd._
213217

214218
var txt: Text = tree match {
@@ -219,8 +223,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
219223
case tp: NamedType if name != nme.WILDCARD => toTextPrefix(tp.prefix) ~ selectionString(tp)
220224
case _ => toText(name)
221225
}
222-
case Select(qual, name) =>
223-
toTextLocal(qual) ~ ("." ~ toText(name) provided name != nme.CONSTRUCTOR)
226+
case tree @ Select(qual, name) =>
227+
toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR)
224228
case This(name) =>
225229
optDotPrefix(name) ~ "this" ~ idText(tree)
226230
case Super(This(name), mix) =>
@@ -297,15 +301,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
297301
toTextLocal(extractor) ~
298302
"(" ~ toTextGlobal(patterns, ", ") ~ ")" ~
299303
("(" ~ toTextGlobal(implicits, ", ") ~ ")" provided implicits.nonEmpty)
300-
case ValDef(mods, name, tpt, rhs) =>
304+
case tree @ ValDef(mods, name, tpt, rhs) =>
301305
dclTextOr {
302-
modText(mods, if (mods is Mutable) "var" else "val") ~~ toText(name) ~
306+
modText(mods, if (mods is Mutable) "var" else "val") ~~ nameIdText(tree) ~
303307
optAscription(tpt)
304308
} ~ optText(rhs)(" = " ~ _)
305-
case DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
309+
case tree @ DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
306310
atOwner(tree) {
307311
dclTextOr {
308-
val first = modText(mods, "def") ~~ toText(name) ~ tparamsText(tparams)
312+
val first = modText(mods, "def") ~~ nameIdText(tree) ~ tparamsText(tparams)
309313
addVparamssText(first, vparamss) ~ optAscription(tpt)
310314
} ~ optText(rhs)(" = " ~ _)
311315
}
@@ -314,11 +318,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
314318
def typeDefText(rhsText: Text) =
315319
dclTextOr {
316320
val rhsText1 = if (tree.hasType) toText(tree.symbol.info) else rhsText
317-
modText(mods, "type") ~~ toText(name) ~ tparamsText(tree.tparams) ~ rhsText1
321+
modText(mods, "type") ~~ nameIdText(tree) ~ tparamsText(tree.tparams) ~ rhsText1
318322
}
319323
rhs match {
320324
case impl: Template =>
321-
modText(mods, if (mods is Trait) "trait" else "class") ~~ toText(name) ~~ idText(tree) ~ toText(impl) ~
325+
modText(mods, if (mods is Trait) "trait" else "class") ~~ nameIdText(tree) ~ toText(impl) ~
322326
(if (tree.hasType && ctx.settings.verbose.value) s"[decls = ${tree.symbol.info.decls}]" else "")
323327
case rhs: TypeBoundsTree =>
324328
typeDefText(toText(rhs))
@@ -367,9 +371,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
367371
"<empty>"
368372
case TypedSplice(t) =>
369373
toText(t)
370-
case ModuleDef(mods, name, impl) =>
374+
case tree @ ModuleDef(mods, name, impl) =>
371375
atOwner(tree) {
372-
modText(mods, "object") ~~ toText(name) ~ toText(impl)
376+
modText(mods, "object") ~~ nameIdText(tree) ~ toText(impl)
373377
}
374378
case SymbolLit(str) =>
375379
"'" + str

test/dotc/tests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class tests extends CompilerTest {
1414
"-pagewidth", "160")
1515

1616
implicit val defaultOptions = noCheckOptions ++ List(
17-
"-YnoDeepSubtypes",
17+
"-Yno-deep-subtypes",
1818
"-Ycheck:patternMatcher,gettersSetters,constructors"
1919
)
2020

2121
val twice = List("#runs", "2", "-YnoDoubleBindings")
2222
val doErase = List("-Ystop-before:terminal")
23-
val allowDeepSubtypes = defaultOptions diff List("-YnoDeepSubtypes")
23+
val allowDeepSubtypes = defaultOptions diff List("-Yno-deep-subtypes")
2424

2525
val posDir = "./tests/pos/"
2626
val negDir = "./tests/neg/"

0 commit comments

Comments
 (0)