Skip to content

Commit

Permalink
Revert "Allow return in tailrec position" scala#14067
Browse files Browse the repository at this point in the history
This reverts commit 4368847.
This reverts commit a349775.
  • Loading branch information
Kordyjan committed Mar 21, 2022
1 parent 0b4e96c commit 9c04cfe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 76 deletions.
18 changes: 15 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,23 @@ class TailRec extends MiniPhase {
def yesTailTransform(tree: Tree)(using Context): Tree =
transform(tree, tailPosition = true)

/** If not in tail position a tree traversal may not be needed.
*
* A recursive call may still be in tail position if within the return
* expression of a labeled block.
* A tree traversal may also be needed to report a failure to transform
* a recursive call of a @tailrec annotated method (i.e. `isMandatory`).
*/
private def isTraversalNeeded =
isMandatory || tailPositionLabeledSyms.size > 0

def noTailTransform(tree: Tree)(using Context): Tree =
transform(tree, tailPosition = false)
if (isTraversalNeeded) transform(tree, tailPosition = false)
else tree

def noTailTransforms[Tr <: Tree](trees: List[Tr])(using Context): List[Tr] =
trees.mapConserve(noTailTransform).asInstanceOf[List[Tr]]
if (isTraversalNeeded) trees.mapConserve(noTailTransform).asInstanceOf[List[Tr]]
else trees

override def transform(tree: Tree)(using Context): Tree = {
/* Rewrite an Apply to be considered for tail call transformation. */
Expand Down Expand Up @@ -441,7 +453,7 @@ class TailRec extends MiniPhase {

case Return(expr, from) =>
val fromSym = from.symbol
val inTailPosition = !fromSym.is(Label) || tailPositionLabeledSyms.contains(fromSym)
val inTailPosition = fromSym.is(Label) && tailPositionLabeledSyms.contains(fromSym)
cpy.Return(tree)(transform(expr, inTailPosition), from)

case _ =>
Expand Down
7 changes: 0 additions & 7 deletions tests/run/tailrec-return.check

This file was deleted.

66 changes: 0 additions & 66 deletions tests/run/tailrec-return.scala

This file was deleted.

0 comments on commit 9c04cfe

Please sign in to comment.