diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-17 18:55:35 +0200 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-18 10:32:34 +0200 |
commit | fa1dc5afea96d5917bc8a4883e3ee2d23db4744e (patch) | |
tree | 0cb4e3198dc529d5288f8ed5a93497fffebc81e2 /src | |
parent | 0cfd858a38ddf0ac83d9bbefe85110f88dc707c0 (diff) | |
download | scala-fa1dc5afea96d5917bc8a4883e3ee2d23db4744e.tar.gz scala-fa1dc5afea96d5917bc8a4883e3ee2d23db4744e.tar.bz2 scala-fa1dc5afea96d5917bc8a4883e3ee2d23db4744e.zip |
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)
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/TailCalls.scala | 16 |
1 files changed, 11 insertions, 5 deletions
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) } } |