Skip to content

Commit 06bd76e

Browse files
committed
Rework Trees.flatten
- Handle the case where Thickets can contain themselves Thickets - Make it tail recursive instead of using an outer while loop
1 parent 5252b15 commit 06bd76e

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -769,28 +769,26 @@ object Trees {
769769
def genericEmptyTree[T >: Untyped]: Thicket[T] = theEmptyTree.asInstanceOf[Thicket[T]]
770770

771771
def flatten[T >: Untyped](trees: List[Tree[T]]): List[Tree[T]] = {
772-
var buf: ListBuffer[Tree[T]] = null
773-
var xs = trees
774-
while (!xs.isEmpty) {
775-
xs.head match {
776-
case Thicket(elems) =>
772+
def recur(buf: ListBuffer[Tree[T]], remaining: List[Tree[T]]): ListBuffer[Tree[T]] =
773+
remaining match {
774+
case Thicket(elems) :: remaining1 =>
777775
if (buf == null) {
778-
buf = new ListBuffer
779-
var ys = trees
780-
while (ys ne xs) {
781-
buf += ys.head
782-
ys = ys.tail
776+
val buf = new ListBuffer[Tree[T]]
777+
var scanned = trees
778+
while (scanned `ne` remaining) {
779+
buf += scanned.head
780+
scanned = scanned.tail
783781
}
782+
recur(buf, remaining)
784783
}
785-
for (elem <- elems) {
786-
assert(!elem.isInstanceOf[Thicket[_]])
787-
buf += elem
788-
}
789-
case tree =>
784+
else recur(recur(buf, elems), remaining1)
785+
case tree :: remaining1 =>
790786
if (buf != null) buf += tree
787+
recur(buf, remaining1)
788+
case nil =>
789+
buf
791790
}
792-
xs = xs.tail
793-
}
791+
val buf = recur(null, trees)
794792
if (buf != null) buf.toList else trees
795793
}
796794

tests/neg/parser-stability-11.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package x0 {
2-
case class x0
2+
case class x0 // error // error
33
}
44
package x0
5-
class x0
5+
class x0 // error // error

tests/neg/parser-stability-12.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
trait x0[]
1+
trait x0[] // error
22
trait x1[x1 <:x0]
3-
extends x1[
3+
extends x1[ // error

tests/neg/parser-stability-13.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
object x0 { // error
2+
def this (x1:x2 = // error

0 commit comments

Comments
 (0)