Skip to content

Commit 429b9e5

Browse files
committed
Add -XprintUser and pretty print lambdas with it
Enable flag by default for the decompiler
1 parent b61a193 commit 429b9e5

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class ScalaSettings extends Settings.SettingGroup {
6060
val XprintTypes = BooleanSetting("-Xprint-types", "Print tree types (debugging option).")
6161
val XprintDiff = BooleanSetting("-Xprint-diff", "Print changed parts of the tree since last print.")
6262
val XprintDiffDel = BooleanSetting("-Xprint-diff-del", "Print chaged parts of the tree since last print including deleted parts.")
63+
val XprintUser = BooleanSetting("-Xprint-user", "Print the program using user friendly abstractions.")
6364
val Xprompt = BooleanSetting("-Xprompt", "Display a prompt after each error (debugging option).")
6465
val XmainClass = StringSetting("-Xmain-class", "path", "Class for manifest's Main-Class entry (only useful with -d <jar>)", "")
6566
val XnoValueClasses = BooleanSetting("-Xno-value-classes", "Do not use value classes. Helps debugging.")

compiler/src/dotty/tools/dotc/decompiler/Main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ object Main extends dotc.Driver {
1616
override def setup(args0: Array[String], rootCtx: Context): (List[String], Context) = {
1717
var args = args0.filter(a => a != "-decompile")
1818
args = if (args.contains("-from-tasty")) args else "-from-tasty" +: args
19-
super.setup(args, rootCtx)
19+
super.setup("-Xprint-user" +: args, rootCtx)
2020
}
2121
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
415415
keywordStr("if ") ~ toText(cond) ~ (keywordText(" then") provided !cond.isInstanceOf[Parens]) ~~ toText(thenp) ~ optText(elsep)(keywordStr(" else ") ~ _)
416416
}
417417
case Closure(env, ref, target) =>
418-
"closure(" ~ (toTextGlobal(env, ", ") ~ " | " provided env.nonEmpty) ~
419-
toTextGlobal(ref) ~ (":" ~ toText(target) provided !target.isEmpty) ~ ")"
418+
("closure(" ~ (toTextGlobal(env, ", ") ~ " | " provided env.nonEmpty) ~
419+
toTextGlobal(ref) ~ (":" ~ toText(target) provided !target.isEmpty) ~ ")").provided(!ctx.settings.XprintUser.value)
420420
case Match(sel, cases) =>
421421
if (sel.isEmpty) blockText(cases)
422422
else changePrec(GlobalPrec) { toText(sel) ~ keywordStr(" match ") ~ blockText(cases) }
@@ -480,10 +480,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
480480
}
481481
case tree @ DefDef(name, tparams, vparamss, tpt, _) =>
482482
dclTextOr {
483-
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree))
483+
val printLambda = tree.symbol.isAnonymousFunction && ctx.settings.XprintUser.value
484+
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda)
484485
withEnclosingDef(tree) {
485-
addVparamssText(prefix ~ tparamsText(tparams), vparamss) ~ optAscription(tpt) ~
486-
optText(tree.rhs)(" = " ~ _)
486+
addVparamssText(prefix ~ tparamsText(tparams), vparamss) ~ optAscription(tpt).provided(!printLambda) ~
487+
optText(tree.rhs)((if (printLambda) " => " else " = ") ~ _)
487488
}
488489
}
489490
case tree @ TypeDef(name, rhs) =>

0 commit comments

Comments
 (0)