Skip to content
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

Pretty-print lambdas #21846

Merged
merged 1 commit into from
Oct 27, 2024
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
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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) =>
Expand Down
18 changes: 2 additions & 16 deletions tests/printing/annot-19846b.check
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,13 @@ package <empty> {
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
}
}

30 changes: 30 additions & 0 deletions tests/printing/lambdas.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[[syntax trees at end of typer]] // tests/printing/lambdas.scala
package <empty> {
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))
}
}

14 changes: 14 additions & 0 deletions tests/printing/lambdas.scala
Original file line number Diff line number Diff line change
@@ -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)
Loading