From fa1dc5afea96d5917bc8a4883e3ee2d23db4744e Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Tue, 17 Jul 2012 18:55:35 +0200 Subject: SI-6089 better tail position analysis for matches we mistakenly went into apply nodes to look for matchEnd-labeldefs in tail positions -- only apply nodes that jump to labels in tailpos should be traversed (this arises for nested matches) --- src/compiler/scala/tools/nsc/transform/TailCalls.scala | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/transform/TailCalls.scala b/src/compiler/scala/tools/nsc/transform/TailCalls.scala index 4c9d855413..d5bbc578fc 100644 --- a/src/compiler/scala/tools/nsc/transform/TailCalls.scala +++ b/src/compiler/scala/tools/nsc/transform/TailCalls.scala @@ -373,7 +373,7 @@ abstract class TailCalls extends Transform { // the labels all look like: matchEnd(x) {x} // then, in a forward jump `matchEnd(expr)`, `expr` is considered in tail position (and the matchEnd jump is replaced by the jump generated by expr) class TailPosLabelsTraverser extends Traverser { - val tailLabels = new collection.mutable.ListBuffer[Symbol]() + val tailLabels = new collection.mutable.HashSet[Symbol]() private var maybeTail: Boolean = true // since we start in the rhs of a DefDef @@ -388,9 +388,15 @@ abstract class TailCalls extends Transform { def traverseTreesNoTail(trees: List[Tree]) = trees foreach traverseNoTail override def traverse(tree: Tree) = tree match { - case LabelDef(_, List(arg), body@Ident(_)) if arg.symbol == body.symbol => // we're looking for label(x){x} in tail position, since that means `a` is in tail position in a call `label(a)` + // we're looking for label(x){x} in tail position, since that means `a` is in tail position in a call `label(a)` + case LabelDef(_, List(arg), body@Ident(_)) if arg.symbol == body.symbol => if (maybeTail) tailLabels += tree.symbol + // jumps to matchEnd are transparent; need this case for nested matches + // (and the translated match case below does things in reverse for this case's sake) + case Apply(fun, arg :: Nil) if hasSynthCaseSymbol(fun) && tailLabels(fun.symbol) => + traverse(arg) + // a translated casedef case LabelDef(_, _, body) if hasSynthCaseSymbol(tree) => traverse(body) @@ -400,9 +406,9 @@ abstract class TailCalls extends Transform { // the assumption is once we encounter a case, the remainder of the block will consist of cases // the prologue may be empty, usually it is the valdef that stores the scrut val (prologue, cases) = stats span (s => !s.isInstanceOf[LabelDef]) - traverseTreesNoTail(prologue) // selector (may be absent) - traverseTrees(cases) traverse(expr) + traverseTrees(cases.reverse) // reverse so that we enter the matchEnd LabelDef before we see jumps to it + traverseTreesNoTail(prologue) // selector (may be absent) case CaseDef(pat, guard, body) => traverse(body) @@ -426,7 +432,7 @@ abstract class TailCalls extends Transform { traverseTreesNoTail(catches) traverseNoTail(finalizer) - case EmptyTree | Super(_, _) | This(_) | Select(_, _) | Ident(_) | Literal(_) | Function(_, _) | TypeTree() => + case Apply(_, _) | EmptyTree | Super(_, _) | This(_) | Select(_, _) | Ident(_) | Literal(_) | Function(_, _) | TypeTree() => case _ => super.traverse(tree) } } -- cgit v1.2.3