Skip to content

Simplify decompiler printer for lambda #4255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef}
import dotty.tools.dotc.ast.{Trees, untpd}
import dotty.tools.dotc.printing.Texts._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.StdNames.nme
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.StdNames._
Expand All @@ -16,15 +17,19 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
override protected def filterModTextAnnots(annots: List[untpd.Tree]): List[untpd.Tree] =
annots.filter(_.tpe != defn.SourceFileAnnotType)

override protected def blockText[T >: Untyped](trees: List[Trees.Tree[T]]): Text = {
trees match {
case DefDef(_, _, _, _, Trees.If(cond, Trees.Block(body :: Nil, _), _)) :: y :: Nil if y.symbol.name == nme.WHILE_PREFIX =>
override protected def blockToText[T >: Untyped](block: Block[T]): Text =
block match {
case Block(DefDef(_, _, _, _, Trees.If(cond, Trees.Block(body :: Nil, _), _)) :: Nil, y) if y.symbol.name == nme.WHILE_PREFIX =>
keywordText("while") ~ " (" ~ toText(cond) ~ ")" ~ toText(body)
case DefDef(_, _, _, _, Trees.Block(body :: Nil, Trees.If(cond, _, _))) :: y :: Nil if y.symbol.name == nme.DO_WHILE_PREFIX =>
case Block(DefDef(_, _, _, _, Trees.Block(body :: Nil, Trees.If(cond, _, _))) :: Nil, y) if y.symbol.name == nme.DO_WHILE_PREFIX =>
keywordText("do") ~ toText(body) ~ keywordText("while") ~ " (" ~ toText(cond) ~ ")"
case _ => super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
case Block((meth @ DefDef(nme.ANON_FUN, _, _, _, _)) :: Nil, _: Closure[T]) =>
withEnclosingDef(meth) {
addVparamssText("", meth.vparamss) ~ " => " ~ toText(meth.rhs)
}
case _ =>
super.blockToText(block)
}
}

override protected def packageDefText(tree: PackageDef): Text = {
val stats = tree.stats.filter {
Expand Down Expand Up @@ -53,16 +58,4 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
val impl1 = impl.copy(parents = impl.parents.filterNot(_.symbol.maybeOwner == defn.ObjectClass))
super.toTextTemplate(impl1, ofNew)
}

override protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
import untpd.{modsDeco => _, _}
dclTextOr(tree) {
val printLambda = tree.symbol.isAnonymousFunction
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda)
withEnclosingDef(tree) {
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt).provided(!printLambda) ~
optText(tree.rhs)((if (printLambda) " => " else " = ") ~ _)
}
}
}
}
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
protected def exprToText(tp: ExprType): Text =
"=> " ~ toText(tp.resType)

protected def blockToText[T >: Untyped](block: Block[T]): Text =
blockText(block.stats :+ block.expr)

protected def blockText[T >: Untyped](trees: List[Tree[T]]): Text =
("{" ~ toText(trees, "\n") ~ "}").close

Expand Down Expand Up @@ -332,8 +335,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
toText(name) ~ " = " ~ toText(arg)
case Assign(lhs, rhs) =>
changePrec(GlobalPrec) { toTextLocal(lhs) ~ " = " ~ toText(rhs) }
case Block(stats, expr) =>
blockText(stats :+ expr)
case block: Block =>
blockToText(block)
case If(cond, thenp, elsep) =>
changePrec(GlobalPrec) {
keywordStr("if ") ~ toText(cond) ~ (keywordText(" then") provided !cond.isInstanceOf[Parens]) ~~ toText(thenp) ~ optText(elsep)(keywordStr(" else ") ~ _)
Expand Down
11 changes: 7 additions & 4 deletions tests/pos/lambda.decompiled
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ out/posTestFromTasty/pos/lambda/foo/Foo.class
--------------------------------------------------------------------------------
package foo {
class Foo() {
val a: Int => Int =
{
(x: Int) => x.*(x)
}
{
(x: Int) =>
{
2
}
}
val a: Int => Int = (x: Int) => x.*(x)
}
}
--------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions tests/pos/lambda.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package foo
class Foo {
{
(x: Int) => 2
}
val a = (x: Int) => x * x
}
5 changes: 1 addition & 4 deletions tests/run-with-compiler/i3876-c.check
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
{
def apply(x: Int): Int
}
=
{
(x: Int) => x.+(x)
}
= (x: Int) => x.+(x)
(f: (x: Int) => Int).apply(x$1)
}
4 changes: 1 addition & 3 deletions tests/run-with-compiler/quote-run-staged-interpreter.check
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
(x: Int) => 2.+(x).+(4)
}
(x: Int) => 2.+(x).+(4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the result appear inside a "template" (class/trait)? Because then you sometimes do need parentheses around the closure to avoid (x: Int) => being parsed as a self-type declaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, if one writes

class Test {
  ((x: Int) => x)
}

It will be pretty printed as

class Test() extends Object() { 
  (x: Int) => x
}

which will be parsed as a self type. But I do think the lightweight syntax is worth the newly introduced bug. One can fix the issue in a separate PR

6
8
9
Expand Down