Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #939 from adriaanm/ticket-6089

SI-6089 better tail position analysis for matches
  • Loading branch information...
commit 92716b54847418565526dc6ec57c6521d1c0c807 2 parents a4b7928 + fa1dc5a
@adriaanm adriaanm authored
View
16 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)
}
}
View
1  test/files/run/t6089.check
@@ -0,0 +1 @@
+scala.MatchError: Foo(0) (of class Foo)
View
13 test/files/run/t6089.scala
@@ -0,0 +1,13 @@
+case class Foo(x: Int)
+
+object Test {
+ def bippo(result: Boolean): Boolean = result
+ def bungus(m: Foo): Boolean =
+ bippo(m match { case Foo(2) => bungus(m) })
+
+ def main(args: Array[String]): Unit = try {
+ bungus(Foo(0))
+ } catch {
+ case x: MatchError => println(x)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.