Skip to content

Revert "Allow return in tailrec position" #14067 #14736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.