Skip to content

Commit e74e9d4

Browse files
committed
Handle empty blocks in the printer
Avoid printing extra blocks and inline nodes
1 parent 9a9aa31 commit e74e9d4

18 files changed

+112
-154
lines changed

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
404404
case SeqLiteral(elems, elemtpt) =>
405405
"[" ~ toTextGlobal(elems, ",") ~ " : " ~ toText(elemtpt) ~ "]"
406406
case tree @ Inlined(call, bindings, body) =>
407-
(("/* inlined from " ~ toText(call) ~ " */ ") `provided`
408-
!call.isEmpty && !homogenizedView && !ctx.settings.YshowNoInline.value) ~
407+
(("/* inlined from " ~ (if (call.isEmpty) "outside" else toText(call)) ~ " */ ") `provided`
408+
!homogenizedView && !ctx.settings.YshowNoInline.value) ~
409409
blockText(bindings :+ body)
410410
case tpt: untpd.DerivedTypeTree =>
411411
"<derived typetree watching " ~ summarized(toText(tpt.watched)) ~ ">"

compiler/test/dotc/pos-recompilation.whitelist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ CoderTrait
3838
collectGenericCC
3939
collections_1
4040
comp-rec-test
41-
companions
4241
compile1
4342
conforms
4443
conformsWild
@@ -317,7 +316,6 @@ i831
317316
i864
318317
i877
319318
i878
320-
i880
321319
i903
322320
i938
323321
i939
@@ -438,7 +436,6 @@ propagate
438436
quote-1
439437
quote-lift-inline-params-b
440438
quote-non-static-macro
441-
quote-this
442439
range
443440
rangepos
444441
rangepos-anonapply
@@ -883,7 +880,6 @@ t613
883880
t6145
884881
t6146
885882
t615
886-
t6157
887883
t616
888884
t6184
889885
t6201

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import scala.annotation.switch
66
import scala.tasty.util.SyntaxHighlightUtils._
77
import scala.tasty.util.Chars
88

9+
import scala.collection.mutable.Builder
10+
911
trait Printers
1012
extends Core
1113
with CaseDefOps
@@ -646,6 +648,17 @@ trait Printers
646648
printTree(body)
647649
}
648650

651+
case IsDefDef(ddef @ DefDef(name, targs, argss, _, rhsOpt)) if name.startsWith("$anonfun") =>
652+
// Decompile lambda definition
653+
assert(targs.isEmpty)
654+
val args :: Nil = argss
655+
val Some(rhs) = rhsOpt
656+
inParens {
657+
printArgsDefs(args)
658+
this += " => "
659+
printTree(rhs)
660+
}
661+
649662
case IsDefDef(ddef @ DefDef(name, targs, argss, tpt, rhs)) =>
650663
printDefAnnotations(ddef)
651664

@@ -777,34 +790,13 @@ trait Printers
777790
case IsValDef(tree) => !tree.symbol.flags.isObject
778791
case _ => true
779792
}
793+
printFlatBlock(stats, expr)
780794

781-
expr match {
782-
case Term.Lambda(_, _) =>
783-
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
784-
assert(stats.size == 1)
785-
val DefDef(_, _, args :: Nil, _, Some(rhs)) :: Nil = stats
786-
inParens {
787-
printArgsDefs(args)
788-
this += " => "
789-
printTree(rhs)
790-
}
791-
case _ =>
792-
this += "{"
793-
indented {
794-
printStats(stats, expr)
795-
}
796-
this += lineBreak() += "}"
797-
}
798-
799-
case Term.Inlined(call, bindings, expansion) => // FIXME: Don't print Inlined with empty calls?
800-
this += "{ // inlined"
801-
indented {
802-
printStats(bindings, expansion)
803-
}
804-
this += lineBreak() += "}"
795+
case Term.Inlined(_, bindings, expansion) =>
796+
printFlatBlock(bindings, expansion)
805797

806798
case Term.Lambda(meth, tpt) =>
807-
// Printed in Term.Block branch
799+
// Printed in by it's DefDef
808800
this
809801

810802
case Term.If(cond, thenp, elsep) =>
@@ -847,15 +839,80 @@ trait Printers
847839

848840
}
849841

842+
def flatBlock(stats: List[Statement], expr: Term): (List[Statement], Term) = {
843+
val flatStats = List.newBuilder[Statement]
844+
def extractFlatStats(stat: Statement): Unit = stat match {
845+
case Term.Block(stats1, expr1) =>
846+
val it = stats1.iterator
847+
while (it.hasNext)
848+
extractFlatStats(it.next())
849+
extractFlatStats(expr1)
850+
case Term.Inlined(_, bindings, expansion) =>
851+
val it = bindings.iterator
852+
while (it.hasNext)
853+
extractFlatStats(it.next())
854+
extractFlatStats(expansion)
855+
case Term.Literal(Constant.Unit()) => // ignore
856+
case stat => flatStats += stat
857+
}
858+
def extractFlatExpr(term: Term): Term = term match {
859+
case Term.Block(stats1, expr1) =>
860+
val it = stats1.iterator
861+
while (it.hasNext)
862+
extractFlatStats(it.next())
863+
extractFlatExpr(expr1)
864+
case Term.Inlined(_, bindings, expansion) =>
865+
val it = bindings.iterator
866+
while (it.hasNext)
867+
extractFlatStats(it.next())
868+
extractFlatExpr(expansion)
869+
case term => term
870+
}
871+
val it = stats.iterator
872+
while (it.hasNext)
873+
extractFlatStats(it.next())
874+
val flatExpr = extractFlatExpr(expr)
875+
(flatStats.result(), flatExpr)
876+
}
877+
878+
def printFlatBlock(stats: List[Statement], expr: Term): Buffer = {
879+
val (stats1, expr1) = flatBlock(stats, expr)
880+
// Remove Term.Lambda nodes, lambdas are printed by their definition
881+
val stats2 = stats1.filter { case Term.Lambda(_, _) => false; case _ => true }
882+
val (stats3, expr3) = expr1 match {
883+
case Term.Lambda(_, _) =>
884+
val init :+ last = stats2
885+
(init, last)
886+
case _ => (stats2, expr1)
887+
}
888+
if (stats3.isEmpty) {
889+
printTree(expr3)
890+
} else {
891+
this += "{"
892+
indented {
893+
printStats(stats3, expr3)
894+
}
895+
this += lineBreak() += "}"
896+
}
897+
}
898+
850899
def printStats(stats: List[Tree], expr: Tree): Unit = {
851900
def printSeparator(next: Tree): Unit = {
852901
// Avoid accidental application of opening `{` on next line with a double break
902+
def rec(next: Tree): Unit = next match {
903+
case Term.Block(stats, _) if stats.nonEmpty => this += doubleLineBreak()
904+
case Term.Inlined(_, bindings, _) if bindings.nonEmpty => this += doubleLineBreak()
905+
case Term.Select(qual, _, _) => rec(qual)
906+
case Term.Apply(fn, _) => rec(fn)
907+
case Term.TypeApply(fn, _) => rec(fn)
908+
case _ => this += lineBreak()
909+
}
853910
next match {
854-
case Term.Block(_, _) => this += doubleLineBreak()
855-
case Term.Inlined(_, _, _) => this += doubleLineBreak()
856-
case Term.Select(qual, _, _) => printSeparator(qual)
857-
case Term.Apply(fn, _) => printSeparator(fn)
858-
case Term.TypeApply(fn, _) => printSeparator(fn)
911+
case IsTerm(term) =>
912+
flatBlock(Nil, term) match {
913+
case (next :: _, _) => rec(next)
914+
case (Nil, next) => rec(next)
915+
}
859916
case _ => this += lineBreak()
860917
}
861918
}

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
inline def foo: scala.Int = (9: scala.Int)
4-
def bar: scala.Int = { // inlined
5-
(9: scala.Int)
6-
}
4+
def bar: scala.Int = (9: scala.Int)
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 */
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.

0 commit comments

Comments
 (0)