Skip to content

Commit 41aba4b

Browse files
committed
Avoid stack overflows in TreeAccumulator
The previously optimized apply function was not tail recursive since it was not final.
1 parent 66967f9 commit 41aba4b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,11 +1379,11 @@ object Trees {
13791379
// Ties the knot of the traversal: call `foldOver(x, tree))` to dive in the `tree` node.
13801380
def apply(x: X, tree: Tree)(using Context): X
13811381

1382-
def apply(x: X, trees: List[Tree])(using Context): X = trees match
1383-
case tree :: rest =>
1384-
apply(apply(x, tree), rest)
1385-
case Nil =>
1386-
x
1382+
def apply(x: X, trees: List[Tree])(using Context): X =
1383+
def fold(x: X, trees: List[Tree]): X = trees match
1384+
case tree :: rest => fold(apply(x, tree), rest)
1385+
case Nil => x
1386+
fold(x, trees)
13871387

13881388
def foldOver(x: X, tree: Tree)(using Context): X =
13891389
if (tree.source != ctx.source && tree.source.exists)

0 commit comments

Comments
 (0)