Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adriaan Moors adriaanm authored
16 src/compiler/scala/tools/nsc/transform/TailCalls.scala
@@ -373,7 +373,7 @@ abstract class TailCalls extends Transform {
373 373 // the labels all look like: matchEnd(x) {x}
374 374 // 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)
375 375 class TailPosLabelsTraverser extends Traverser {
376   - val tailLabels = new collection.mutable.ListBuffer[Symbol]()
  376 + val tailLabels = new collection.mutable.HashSet[Symbol]()
377 377
378 378 private var maybeTail: Boolean = true // since we start in the rhs of a DefDef
379 379
@@ -388,9 +388,15 @@ abstract class TailCalls extends Transform {
388 388 def traverseTreesNoTail(trees: List[Tree]) = trees foreach traverseNoTail
389 389
390 390 override def traverse(tree: Tree) = tree match {
391   - 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)`
  391 + // we're looking for label(x){x} in tail position, since that means `a` is in tail position in a call `label(a)`
  392 + case LabelDef(_, List(arg), body@Ident(_)) if arg.symbol == body.symbol =>
392 393 if (maybeTail) tailLabels += tree.symbol
393 394
  395 + // jumps to matchEnd are transparent; need this case for nested matches
  396 + // (and the translated match case below does things in reverse for this case's sake)
  397 + case Apply(fun, arg :: Nil) if hasSynthCaseSymbol(fun) && tailLabels(fun.symbol) =>
  398 + traverse(arg)
  399 +
394 400 // a translated casedef
395 401 case LabelDef(_, _, body) if hasSynthCaseSymbol(tree) =>
396 402 traverse(body)
@@ -400,9 +406,9 @@ abstract class TailCalls extends Transform {
400 406 // the assumption is once we encounter a case, the remainder of the block will consist of cases
401 407 // the prologue may be empty, usually it is the valdef that stores the scrut
402 408 val (prologue, cases) = stats span (s => !s.isInstanceOf[LabelDef])
403   - traverseTreesNoTail(prologue) // selector (may be absent)
404   - traverseTrees(cases)
405 409 traverse(expr)
  410 + traverseTrees(cases.reverse) // reverse so that we enter the matchEnd LabelDef before we see jumps to it
  411 + traverseTreesNoTail(prologue) // selector (may be absent)
406 412
407 413 case CaseDef(pat, guard, body) =>
408 414 traverse(body)
@@ -426,7 +432,7 @@ abstract class TailCalls extends Transform {
426 432 traverseTreesNoTail(catches)
427 433 traverseNoTail(finalizer)
428 434
429   - case EmptyTree | Super(_, _) | This(_) | Select(_, _) | Ident(_) | Literal(_) | Function(_, _) | TypeTree() =>
  435 + case Apply(_, _) | EmptyTree | Super(_, _) | This(_) | Select(_, _) | Ident(_) | Literal(_) | Function(_, _) | TypeTree() =>
430 436 case _ => super.traverse(tree)
431 437 }
432 438 }
1  test/files/run/t6089.check
... ... @@ -0,0 +1 @@
  1 +scala.MatchError: Foo(0) (of class Foo)
13 test/files/run/t6089.scala
... ... @@ -0,0 +1,13 @@
  1 +case class Foo(x: Int)
  2 +
  3 +object Test {
  4 + def bippo(result: Boolean): Boolean = result
  5 + def bungus(m: Foo): Boolean =
  6 + bippo(m match { case Foo(2) => bungus(m) })
  7 +
  8 + def main(args: Array[String]): Unit = try {
  9 + bungus(Foo(0))
  10 + } catch {
  11 + case x: MatchError => println(x)
  12 + }
  13 +}

0 comments on commit 92716b5

Please sign in to comment.
Something went wrong with that request. Please try again.