Skip to content

Commit c91c182

Browse files
committed
Print double bracks after statements to avoid ambiguity
For example, the first snippet is `foo.apply(...)` and the second is eval `foo` and then eval the block. ```scala foo { ... } ``` ```scala foo { ... } ```
1 parent 1f523ea commit c91c182

File tree

9 files changed

+48
-29
lines changed

9 files changed

+48
-29
lines changed

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

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
191191
case stats =>
192192
this += "{"
193193
indented {
194-
this += lineBreak()
195-
printTrees(stats, lineBreak())
194+
printStats(stats.init, stats.last)
196195
}
197196
this += lineBreak() += "}"
198197
}
@@ -205,8 +204,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
205204
case stats =>
206205
this += "{"
207206
indented {
207+
printStats(stats.init, stats.last)
208208
this += lineBreak()
209-
printTrees(stats, lineBreak())
209+
printTree(stats.last)
210210
}
211211
this += lineBreak() += "}"
212212
}
@@ -327,28 +327,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
327327
this += ")"
328328
case _ =>
329329
this += "{"
330+
val (stats1, expr1) =
331+
if (isLoopEntryPoint(expr)) (stats.init, stats.last)
332+
else (stats, expr)
330333
indented {
331-
if (!stats.isEmpty) {
332-
this += lineBreak()
333-
printTrees(stats, lineBreak())
334-
}
335-
if (!isLoopEntryPoint(expr)) {
336-
this += lineBreak()
337-
printTree(expr)
338-
}
334+
printStats(stats1, expr1)
339335
}
340336
this += lineBreak() += "}"
341337
}
342338

343339
case Term.Inlined(call, bindings, expansion) =>
344-
sb.append("{ // inlined")
340+
this += "{ // inlined"
345341
indented {
346-
if (!bindings.isEmpty) {
347-
this += lineBreak()
348-
printTrees(bindings, lineBreak())
349-
}
350-
this += lineBreak()
351-
printTree(expansion)
342+
printStats(bindings, expansion)
352343
}
353344
this += lineBreak() += "}"
354345

@@ -400,6 +391,30 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
400391

401392
}
402393

394+
def printStats(stats: List[Tree], expr: Tree): Unit = {
395+
def printSeparator(nextStats: List[Tree]) = {
396+
this += lineBreak()
397+
// Avoid accidental application of opening `{` on next line with a double break
398+
val next = if (nextStats.isEmpty) expr else nextStats.head
399+
next match {
400+
case Term.Block(_, _) => this += lineBreak()
401+
case Term.Inlined(_, _, _) => this += lineBreak()
402+
case _ =>
403+
}
404+
}
405+
def printSeparated(list: List[Tree]): Unit = list match {
406+
case Nil =>
407+
printTree(expr)
408+
case x :: xs =>
409+
printTree(x)
410+
printSeparator(xs)
411+
printSeparated(xs)
412+
}
413+
414+
this += lineBreak()
415+
printSeparated(stats)
416+
}
417+
403418
def printTrees(trees: List[Tree], sep: String): Buffer = {
404419
def printSeparated(list: List[Tree]): Unit = list match {
405420
case Nil =>
@@ -603,14 +618,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
603618
}
604619
this += " =>"
605620
indented {
606-
this += lineBreak()
607621
body match {
608622
case Term.Block(stats, expr) =>
609-
printTrees(stats, lineBreak())
610-
if (stats.nonEmpty)
611-
this += lineBreak()
612-
printTree(expr)
623+
printStats(stats, expr)
613624
case body =>
625+
this += lineBreak()
614626
printTree(body)
615627
}
616628
}

tests/pos/simpleDoWhile.decompiled

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
class Foo() {
33
def foo: scala.Unit = {
44
var i: scala.Int = 1
5+
56
do {
67
i = 0
78
} while (i.!=(0))
89
}
9-
}
10+
}

tests/pos/simpleSingleton.decompiled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ class Foo() {
1111
}
1212
object Foo {
1313
object Bar
14-
}
14+
}

tests/pos/simpleWhile.decompiled

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
class Foo() {
33
def foo: scala.Unit = {
44
var i: scala.Int = 1
5+
56
while (i.!=(0)) {
67
i = 0
78
}
89
}
9-
}
10+
}

tests/run-with-compiler/i3876-d.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
6
22
{
33
val x$1: scala.Int = 3
4+
45
{ // inlined
56
x$1.+(x$1)
67
}

tests/run-with-compiler/i4044b.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
var x: scala.Int = 4
3-
x = 3
3+
x = 3;
44
x
55
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
22
scala.Predef.println(1)
3+
34
{
45
scala.Predef.println(2)
6+
57
{
68
scala.Predef.println(3)
9+
710
{
811
scala.Predef.println(4)
12+
913
{
1014
scala.Predef.println(5)
1115
()

tests/run/puzzle.decompiled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ object Test {
88
val y: scala.Float = scala.Long.long2float(z)
99
()
1010
}
11-
}
11+
}

tests/run/simpleClass.decompiled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ package foo {
1212
/** Decompiled from out/runTestFromTasty/run/simpleClass/foo/B.class */
1313
package foo {
1414
class B() extends foo.A()
15-
}
15+
}

0 commit comments

Comments
 (0)