Skip to content

Commit accc8fb

Browse files
committed
Do not print inlined calls tag in Expr.show
Also move logic that flattens blocks to the decompiler and make it stricter
1 parent 84666e7 commit accc8fb

20 files changed

+106
-179
lines changed

compiler/src/dotty/tools/dotc/quoted/TreeCleaner.scala

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ class TreeCleaner extends tpd.TreeMap {
3030
}
3131

3232
super.transform(tree0) match {
33-
case Block(Nil, expr1) => expr1
34-
case Block(stats1, expr1) =>
35-
val flatStats = stats1.flatMap {
36-
case Block(stats2, expr2) => stats2 ::: expr2 :: Nil
37-
case Literal(Constant(())) => Nil
38-
case stat => stat :: Nil
39-
}
40-
expr1 match {
41-
case Block(stats3, expr3) => Block(flatStats ::: stats3, expr3)
42-
case expr3 => Block(flatStats, expr3)
43-
}
4433
case tree1: TypeTree => TypeTree(tree1.tpe.subst(aliasesSyms, aliasesTypes))
4534
case tree1: Ident => aliases.get(tree1.symbol).getOrElse(tree1)
4635
case tree1 => tree1

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
237237
printTree(body) += " while "
238238
inParens(printTree(cond))
239239

240+
case IsDefDef(ddef @ DefDef(name, targs, argss, _, rhsOpt)) if name.startsWith("$anonfun") =>
241+
// Decompile lambda definition
242+
assert(targs.isEmpty)
243+
val args :: Nil = argss
244+
val Some(rhs) = rhsOpt
245+
inParens {
246+
printArgsDefs(args)
247+
this += " => "
248+
printTree(rhs)
249+
}
250+
240251
case IsDefDef(ddef @ DefDef(name, targs, argss, tpt, rhs)) =>
241252
printDefAnnotations(ddef)
242253

@@ -363,34 +374,13 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
363374
case IsValDef(tree) => !tree.symbol.flags.isObject
364375
case _ => true
365376
}
377+
printFlatBlock(stats, expr)
366378

367-
expr match {
368-
case Term.Lambda(_, _) =>
369-
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
370-
assert(stats.size == 1)
371-
val DefDef(_, _, args :: Nil, _, Some(rhs)) :: Nil = stats
372-
inParens {
373-
printArgsDefs(args)
374-
this += " => "
375-
printTree(rhs)
376-
}
377-
case _ =>
378-
this += "{"
379-
indented {
380-
printStats(stats, expr)
381-
}
382-
this += lineBreak() += "}"
383-
}
384-
385-
case Term.Inlined(call, bindings, expansion) => // FIXME: Don't print Inlined with empty calls?
386-
this += "{ // inlined"
387-
indented {
388-
printStats(bindings, expansion)
389-
}
390-
this += lineBreak() += "}"
379+
case Term.Inlined(_, bindings, expansion) =>
380+
printFlatBlock(bindings, expansion)
391381

392382
case Term.Lambda(meth, tpt) =>
393-
// Printed in Term.Block branch
383+
// Printed in by it's DefDef
394384
this
395385

396386
case Term.If(cond, thenp, elsep) =>
@@ -433,15 +423,72 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
433423

434424
}
435425

426+
def flatBlock(stats: List[Statement], expr: Term): (List[Statement], Term) = {
427+
val flatStats = List.newBuilder[Statement]
428+
def extractFlatStats(stat: Statement): Unit = stat match {
429+
case Term.Block(stats1, expr1) =>
430+
stats1.foreach(extractFlatStats)
431+
extractFlatStats(expr1)
432+
case Term.Inlined(_, bindings, expansion) =>
433+
bindings.foreach(extractFlatStats)
434+
extractFlatStats(expansion)
435+
case Term.Literal(Constant.Unit()) => // ignore
436+
case stat => flatStats += stat
437+
}
438+
def extractFlatExpr(term: Term): Term = term match {
439+
case Term.Block(stats1, expr1) =>
440+
stats1.foreach(extractFlatStats)
441+
extractFlatExpr(expr1)
442+
case Term.Inlined(_, bindings, expansion) =>
443+
bindings.foreach(extractFlatStats)
444+
extractFlatExpr(expansion)
445+
case term => term
446+
}
447+
stats.foreach(extractFlatStats)
448+
val flatExpr = extractFlatExpr(expr)
449+
(flatStats.result(), flatExpr)
450+
}
451+
452+
def printFlatBlock(stats: List[Statement], expr: Term): Buffer = {
453+
val (stats1, expr1) = flatBlock(stats, expr)
454+
455+
// Remove Term.Lambda nodes, lambdas are printed by their definition
456+
val stats2 = stats1.filter { case Term.Lambda(_, _) => false; case _ => true }
457+
val (stats3, expr3) = expr1 match {
458+
case Term.Lambda(_, _) =>
459+
val init :+ last = stats2
460+
(init, last)
461+
case _ => (stats2, expr1)
462+
}
463+
464+
if (stats3.isEmpty) {
465+
printTree(expr3)
466+
} else {
467+
this += "{"
468+
indented {
469+
printStats(stats3, expr3)
470+
}
471+
this += lineBreak() += "}"
472+
}
473+
}
474+
436475
def printStats(stats: List[Tree], expr: Tree): Unit = {
437476
def printSeparator(next: Tree): Unit = {
438477
// Avoid accidental application of opening `{` on next line with a double break
478+
def rec(next: Tree): Unit = next match {
479+
case Term.Block(stats, _) if stats.nonEmpty => this += doubleLineBreak()
480+
case Term.Inlined(_, bindings, _) if bindings.nonEmpty => this += doubleLineBreak()
481+
case Term.Select(qual, _, _) => rec(qual)
482+
case Term.Apply(fn, _) => rec(fn)
483+
case Term.TypeApply(fn, _) => rec(fn)
484+
case _ => this += lineBreak()
485+
}
439486
next match {
440-
case Term.Block(_, _) => this += doubleLineBreak()
441-
case Term.Inlined(_, _, _) => this += doubleLineBreak()
442-
case Term.Select(qual, _, _) => printSeparator(qual)
443-
case Term.Apply(fn, _) => printSeparator(fn)
444-
case Term.TypeApply(fn, _) => printSeparator(fn)
487+
case IsTerm(term) =>
488+
flatBlock(Nil, term) match {
489+
case (next :: _, _) => rec(next)
490+
case (Nil, next) => rec(next)
491+
}
445492
case _ => this += lineBreak()
446493
}
447494
}

tests/pos/i2104b.decompiled

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ case class Pair[A, B](_1: A, _2: B) {
3434
object Pair extends scala.AnyRef()
3535
/** Decompiled from out/posTestFromTasty/pos/i2104b/Test.class */
3636
object Test {
37-
def main(args: scala.Array[scala.Predef.String]): scala.Unit = {
38-
Cons.apply[scala.Option[scala.Int], scala.None.type](scala.Option.apply[scala.Int](1), scala.None) match {
39-
case Cons(scala.Some(i), scala.None) =>
40-
()
41-
}
37+
def main(args: scala.Array[scala.Predef.String]): scala.Unit = Cons.apply[scala.Option[scala.Int], scala.None.type](scala.Option.apply[scala.Int](1), scala.None) match {
38+
case Cons(scala.Some(i), scala.None) =>
39+
()
4240
}
43-
}
41+
}

tests/pos/i4526b.decompiled

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/** Decompiled from out/posTestFromTasty/pos/i4526b/Foo.class */
22
class Foo() {
3-
def justdoit(f: scala.Either[scala.Int, scala.Predef.String]): scala.Predef.String = {
4-
f match {
5-
case scala.Left(i) =>
6-
i.toString()
7-
case scala.Right(s) =>
8-
(s: java.lang.String)
9-
}
3+
def justdoit(f: scala.Either[scala.Int, scala.Predef.String]): scala.Predef.String = f match {
4+
case scala.Left(i) =>
5+
i.toString()
6+
case scala.Right(s) =>
7+
(s: java.lang.String)
108
}
119
}

tests/pos/lambda.decompiled

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
/** Decompiled from out/posTestFromTasty/pos/lambda/foo/Foo.class */
22
package foo {
33
class Foo() {
4-
{
5-
((x: scala.Int) => {
6-
2
7-
})
8-
}
4+
((x: scala.Int) => 2)
95
val a: scala.Function1[scala.Int, scala.Int] = ((x: scala.Int) => x.*(x))
106
}
117
}

tests/pos/simpleDoWhile.decompiled

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
class Foo() {
33
def foo: scala.Unit = {
44
var i: scala.Int = 1
5-
do {
6-
i = 0
7-
} while (i.!=(0))
5+
do i = 0 while (i.!=(0))
86
}
97
}

tests/pos/simpleInline.decompiled

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/** Decompiled from out/posTestFromTasty/pos/simpleInline/Foo.class */
22
class Foo() {
33
rewrite def foo: scala.Int = 9
4-
def bar: scala.Int = { // inlined
5-
9
6-
}
4+
def bar: scala.Int = 9
75
}

tests/pos/simpleMatchCase.decompiled

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/** Decompiled from out/posTestFromTasty/pos/simpleMatchCase/Foo.class */
22
class Foo() {
3-
def foo: scala.Unit = {
4-
"c" match {
5-
case x =>
6-
scala.Predef.println("a")
7-
scala.Predef.println("b")
8-
}
3+
def foo: scala.Unit = "c" match {
4+
case x =>
5+
scala.Predef.println("a")
6+
scala.Predef.println("b")
97
}
108
}

tests/pos/simpleWhile.decompiled

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
class Foo() {
33
def foo: scala.Unit = {
44
var i: scala.Int = 1
5-
while (i.!=(0)) {
6-
i = 0
7-
}
5+
while (i.!=(0)) i = 0
86
}
97
}

tests/pos/t3869.decompiled

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/** Decompiled from out/posTestFromTasty/pos/t3869/Test.class */
22
object Test {
33
def f: scala.Unit = try return () finally while (true) ()
4-
def main(args: scala.Array[scala.Predef.String]): scala.Unit = {
5-
Test.f
6-
}
4+
def main(args: scala.Array[scala.Predef.String]): scala.Unit = Test.f
75
}

tests/pos/t704.decompiled

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ trait E() extends java.lang.Object with D {
1616
val x1: scala.AnyRef = E.this.get_xxxx
1717
scala.Console.println(y)
1818
}
19-
20-
{
21-
yyyy
22-
()
23-
}
19+
yyyy
20+
()
2421
}
2522
}
2623
/** Decompiled from out/posTestFromTasty/pos/t704/Go.class */

tests/run-with-compiler/i3847-b.check

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
{
2-
new scala.Array[scala.List[scala.Int]](1)
3-
}
1+
new scala.Array[scala.List[scala.Int]](1)

tests/run-with-compiler/i4350.check

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
{
2-
null.asInstanceOf[lang.Object]
3-
}
4-
{
5-
null.asInstanceOf[scala.Predef.String]
6-
}
1+
null.asInstanceOf[lang.Object]
2+
null.asInstanceOf[scala.Predef.String]
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
1 + {{ // inlined
2-
Index.zero["bar", scala.Tuple2["baz", scala.Unit]]
3-
}}
1+
1 + {Index.zero["bar", scala.Tuple2["baz", scala.Unit]]}

tests/run-with-compiler/quote-nested-3.check

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
type T = scala.Predef.String
33
val x: java.lang.String = "foo"
44
val z: T = x
5-
()
65
(x: java.lang.String)
76
}

tests/run-with-compiler/quote-show-blocks-raw.check

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/run-with-compiler/quote-show-blocks-raw.scala

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/run-with-compiler/quote-unrolled-foreach.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
var i: scala.Int = 0
3434
while (i.<(size)) {
3535
val element: scala.Int = arr.apply(i)
36-
3736
((i: scala.Int) => java.lang.System.out.println(i)).apply(element)
3837
i = i.+(1)
3938
}
@@ -86,7 +85,6 @@
8685
var i: scala.Int = 0
8786
while (i.<(size)) {
8887
val element: scala.Int = arr1.apply(i)
89-
9088
((x: scala.Int) => scala.Predef.println(x)).apply(element)
9189
i = i.+(1)
9290
}

0 commit comments

Comments
 (0)