Skip to content

Commit b37d9ff

Browse files
Merge pull request #4255 from dotty-staging/pretty-print-lambda
Simplify decompiler printer for lambda
2 parents 58fe948 + 2143728 commit b37d9ff

File tree

6 files changed

+28
-31
lines changed

6 files changed

+28
-31
lines changed

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef}
55
import dotty.tools.dotc.ast.{Trees, untpd}
66
import dotty.tools.dotc.printing.Texts._
77
import dotty.tools.dotc.core.Contexts._
8+
import dotty.tools.dotc.core.StdNames.nme
89
import dotty.tools.dotc.core.Flags._
910
import dotty.tools.dotc.core.Symbols._
1011
import dotty.tools.dotc.core.StdNames._
@@ -16,15 +17,19 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
1617
override protected def filterModTextAnnots(annots: List[untpd.Tree]): List[untpd.Tree] =
1718
annots.filter(_.tpe != defn.SourceFileAnnotType)
1819

19-
override protected def blockText[T >: Untyped](trees: List[Trees.Tree[T]]): Text = {
20-
trees match {
21-
case DefDef(_, _, _, _, Trees.If(cond, Trees.Block(body :: Nil, _), _)) :: y :: Nil if y.symbol.name == nme.WHILE_PREFIX =>
20+
override protected def blockToText[T >: Untyped](block: Block[T]): Text =
21+
block match {
22+
case Block(DefDef(_, _, _, _, Trees.If(cond, Trees.Block(body :: Nil, _), _)) :: Nil, y) if y.symbol.name == nme.WHILE_PREFIX =>
2223
keywordText("while") ~ " (" ~ toText(cond) ~ ")" ~ toText(body)
23-
case DefDef(_, _, _, _, Trees.Block(body :: Nil, Trees.If(cond, _, _))) :: y :: Nil if y.symbol.name == nme.DO_WHILE_PREFIX =>
24+
case Block(DefDef(_, _, _, _, Trees.Block(body :: Nil, Trees.If(cond, _, _))) :: Nil, y) if y.symbol.name == nme.DO_WHILE_PREFIX =>
2425
keywordText("do") ~ toText(body) ~ keywordText("while") ~ " (" ~ toText(cond) ~ ")"
25-
case _ => super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
26+
case Block((meth @ DefDef(nme.ANON_FUN, _, _, _, _)) :: Nil, _: Closure[T]) =>
27+
withEnclosingDef(meth) {
28+
addVparamssText("", meth.vparamss) ~ " => " ~ toText(meth.rhs)
29+
}
30+
case _ =>
31+
super.blockToText(block)
2632
}
27-
}
2833

2934
override protected def packageDefText(tree: PackageDef): Text = {
3035
val stats = tree.stats.filter {
@@ -53,16 +58,4 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
5358
val impl1 = impl.copy(parents = impl.parents.filterNot(_.symbol.maybeOwner == defn.ObjectClass))
5459
super.toTextTemplate(impl1, ofNew)
5560
}
56-
57-
override protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
58-
import untpd.{modsDeco => _, _}
59-
dclTextOr(tree) {
60-
val printLambda = tree.symbol.isAnonymousFunction
61-
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda)
62-
withEnclosingDef(tree) {
63-
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt).provided(!printLambda) ~
64-
optText(tree.rhs)((if (printLambda) " => " else " = ") ~ _)
65-
}
66-
}
67-
}
6861
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
226226
protected def exprToText(tp: ExprType): Text =
227227
"=> " ~ toText(tp.resType)
228228

229+
protected def blockToText[T >: Untyped](block: Block[T]): Text =
230+
blockText(block.stats :+ block.expr)
231+
229232
protected def blockText[T >: Untyped](trees: List[Tree[T]]): Text =
230233
("{" ~ toText(trees, "\n") ~ "}").close
231234

@@ -332,8 +335,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
332335
toText(name) ~ " = " ~ toText(arg)
333336
case Assign(lhs, rhs) =>
334337
changePrec(GlobalPrec) { toTextLocal(lhs) ~ " = " ~ toText(rhs) }
335-
case Block(stats, expr) =>
336-
blockText(stats :+ expr)
338+
case block: Block =>
339+
blockToText(block)
337340
case If(cond, thenp, elsep) =>
338341
changePrec(GlobalPrec) {
339342
keywordStr("if ") ~ toText(cond) ~ (keywordText(" then") provided !cond.isInstanceOf[Parens]) ~~ toText(thenp) ~ optText(elsep)(keywordStr(" else ") ~ _)

tests/pos/lambda.decompiled

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ out/posTestFromTasty/pos/lambda/foo/Foo.class
33
--------------------------------------------------------------------------------
44
package foo {
55
class Foo() {
6-
val a: Int => Int =
7-
{
8-
(x: Int) => x.*(x)
9-
}
6+
{
7+
(x: Int) =>
8+
{
9+
2
10+
}
11+
}
12+
val a: Int => Int = (x: Int) => x.*(x)
1013
}
1114
}
1215
--------------------------------------------------------------------------------

tests/pos/lambda.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package foo
22
class Foo {
3+
{
4+
(x: Int) => 2
5+
}
36
val a = (x: Int) => x * x
47
}

tests/run-with-compiler/i3876-c.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
{
77
def apply(x: Int): Int
88
}
9-
=
10-
{
11-
(x: Int) => x.+(x)
12-
}
9+
= (x: Int) => x.+(x)
1310
(f: (x: Int) => Int).apply(x$1)
1411
}

tests/run-with-compiler/quote-run-staged-interpreter.check

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
{
2-
(x: Int) => 2.+(x).+(4)
3-
}
1+
(x: Int) => 2.+(x).+(4)
42
6
53
8
64
9

0 commit comments

Comments
 (0)