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)