Skip to content

Commit 909f10a

Browse files
Merge pull request #5510 from dotty-staging/avoid-printing-nested-empty-blocks
Avoid printing extra blocks and inline nodes
2 parents 72904b5 + 0dc0587 commit 909f10a

18 files changed

+110
-154
lines changed

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

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

compiler/test/dotc/pos-recompilation.whitelist

-4
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

+85-30
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,17 @@ trait Printers
646646
printTree(body)
647647
}
648648

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

@@ -777,34 +788,13 @@ trait Printers
777788
case IsValDef(tree) => !tree.symbol.flags.isObject
778789
case _ => true
779790
}
791+
printFlatBlock(stats, expr)
780792

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() += "}"
793+
case Term.Inlined(_, bindings, expansion) =>
794+
printFlatBlock(bindings, expansion)
805795

806796
case Term.Lambda(meth, tpt) =>
807-
// Printed in Term.Block branch
797+
// Printed in by it's DefDef
808798
this
809799

810800
case Term.If(cond, thenp, elsep) =>
@@ -847,15 +837,80 @@ trait Printers
847837

848838
}
849839

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

tests/pos/i2104b.decompiled

+4-6
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

+5-7
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

+1-5
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

+1-3
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

+1-3
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

+4-6
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

+1-3
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

+1-3
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

+2-5
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 */
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

-1
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

-36
This file was deleted.

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

-25
This file was deleted.

0 commit comments

Comments
 (0)