diff --git a/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala b/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala index 9e3c520985de..733cbc26b652 100644 --- a/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala @@ -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._ @@ -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 { @@ -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 " = ") ~ _) - } - } - } } diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 1c1e05e5404e..11d15ea9449f 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -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 @@ -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 ") ~ _) diff --git a/tests/pos/lambda.decompiled b/tests/pos/lambda.decompiled index b201feba55cc..f610dc4481a4 100644 --- a/tests/pos/lambda.decompiled +++ b/tests/pos/lambda.decompiled @@ -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) } } -------------------------------------------------------------------------------- \ No newline at end of file diff --git a/tests/pos/lambda.scala b/tests/pos/lambda.scala index 952e28826894..c85e8bf101ee 100644 --- a/tests/pos/lambda.scala +++ b/tests/pos/lambda.scala @@ -1,4 +1,7 @@ package foo class Foo { + { + (x: Int) => 2 + } val a = (x: Int) => x * x } diff --git a/tests/run-with-compiler/i3876-c.check b/tests/run-with-compiler/i3876-c.check index 60a13f645202..23220a84d93a 100644 --- a/tests/run-with-compiler/i3876-c.check +++ b/tests/run-with-compiler/i3876-c.check @@ -6,9 +6,6 @@ { def apply(x: Int): Int } - = - { - (x: Int) => x.+(x) - } + = (x: Int) => x.+(x) (f: (x: Int) => Int).apply(x$1) } diff --git a/tests/run-with-compiler/quote-run-staged-interpreter.check b/tests/run-with-compiler/quote-run-staged-interpreter.check index 1ecdf98f6e71..f65ddd9f2efa 100644 --- a/tests/run-with-compiler/quote-run-staged-interpreter.check +++ b/tests/run-with-compiler/quote-run-staged-interpreter.check @@ -1,6 +1,4 @@ -{ - (x: Int) => 2.+(x).+(4) -} +(x: Int) => 2.+(x).+(4) 6 8 9