From eb12bfe3f20ca9ad0e395ba20e52d64f084b8c4b Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Fri, 25 Oct 2024 18:03:21 +0200 Subject: [PATCH] Pretty-print lambdas --- .../tools/dotc/printing/RefinedPrinter.scala | 4 +++ tests/printing/annot-19846b.check | 18 ++--------- tests/printing/lambdas.check | 30 +++++++++++++++++++ tests/printing/lambdas.scala | 14 +++++++++ 4 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 tests/printing/lambdas.check create mode 100644 tests/printing/lambdas.scala diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index b229c7ec29d9..95df9f84c723 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -17,6 +17,7 @@ import Denotations.* import SymDenotations.* import StdNames.{nme, tpnme} import ast.{Trees, tpd, untpd} +import tpd.closureDef import typer.{Implicits, Namer, Applications} import typer.ProtoTypes.* import Trees.* @@ -510,6 +511,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { toText(name) ~ (if name.isTermName && arg.isType then " : " else " = ") ~ toText(arg) case Assign(lhs, rhs) => changePrec(GlobalPrec) { toTextLocal(lhs) ~ " = " ~ toText(rhs) } + case closureDef(meth) if !printDebug => + withEnclosingDef(meth): + meth.paramss.map(paramsText).foldRight(toText(meth.rhs))(_ ~ " => " ~ _) case block: Block => blockToText(block) case If(cond, thenp, elsep) => diff --git a/tests/printing/annot-19846b.check b/tests/printing/annot-19846b.check index 3f63a46c4286..3633b1c9e9ad 100644 --- a/tests/printing/annot-19846b.check +++ b/tests/printing/annot-19846b.check @@ -7,27 +7,13 @@ package { final lazy module val Test: Test = new Test() final module class Test() extends Object() { this: Test.type => val y: Int = ??? - val z: - Int @lambdaAnnot( - { - def $anonfun(): Int = Test.y - closure($anonfun) - } - ) - = f(Test.y) + val z: Int @lambdaAnnot(() => Test.y) = f(Test.y) } final lazy module val annot-19846b$package: annot-19846b$package = new annot-19846b$package() final module class annot-19846b$package() extends Object() { this: annot-19846b$package.type => - def f(x: Int): - Int @lambdaAnnot( - { - def $anonfun(): Int = x - closure($anonfun) - } - ) - = x + def f(x: Int): Int @lambdaAnnot(() => x) = x } } diff --git a/tests/printing/lambdas.check b/tests/printing/lambdas.check new file mode 100644 index 000000000000..887a8f2ded35 --- /dev/null +++ b/tests/printing/lambdas.check @@ -0,0 +1,30 @@ +[[syntax trees at end of typer]] // tests/printing/lambdas.scala +package { + final lazy module val Main: Main = new Main() + final module class Main() extends Object() { this: Main.type => + val f1: Int => Int = (x: Int) => x.+(1) + val f2: (Int, Int) => Int = (x: Int, y: Int) => x.+(y) + val f3: Int => Int => Int = (x: Int) => (y: Int) => x.+(y) + val f4: [T] => (x: Int) => Int = [T >: Nothing <: Any] => (x: Int) => x.+(1) + val f5: [T] => (x: Int) => Int => Int = [T >: Nothing <: Any] => (x: Int) + => (y: Int) => x.+(y) + val f6: Int => Int = (x: Int) => + { + val x2: Int = x.+(1) + x2.+(1) + } + def f7(x: Int): Int = x.+(1) + val f8: Int => Int = (x: Int) => Main.f7(x) + val l: List[Int] = List.apply[Int]([1,2,3 : Int]*) + Main.l.map[Int]((_$1: Int) => _$1.+(1)) + Main.l.map[Int]((x: Int) => x.+(1)) + Main.l.map[Int]((x: Int) => + { + val x2: Int = x.+(1) + x2.+(1) + } + ) + Main.l.map[Int]((x: Int) => Main.f7(x)) + } +} + diff --git a/tests/printing/lambdas.scala b/tests/printing/lambdas.scala new file mode 100644 index 000000000000..8df7db3e6dda --- /dev/null +++ b/tests/printing/lambdas.scala @@ -0,0 +1,14 @@ +object Main: + val f1 = (x: Int) => x + 1 + val f2 = (x: Int, y: Int) => x + y + val f3 = (x: Int) => (y: Int) => x + y + val f4 = [T] => (x: Int) => x + 1 + val f5 = [T] => (x: Int) => (y: Int) => x + y + val f6 = (x: Int) => { val x2 = x + 1; x2 + 1 } + def f7(x: Int) = x + 1 + val f8 = f7 + val l = List(1,2,3) + l.map(_ + 1) + l.map(x => x + 1) + l.map(x => { val x2 = x + 1; x2 + 1 }) + l.map(f7)