Skip to content

Commit

Permalink
Allow return in tailrec position
Browse files Browse the repository at this point in the history
  • Loading branch information
mbovel committed Dec 7, 2021
1 parent fc7e8c3 commit 91053e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class TailRec extends MiniPhase {

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

case _ =>
Expand Down
2 changes: 2 additions & 0 deletions tests/run/tailrec-return.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
6
false
16 changes: 16 additions & 0 deletions tests/run/tailrec-return.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
object Test:

@annotation.tailrec
def sum(n: Int, acc: Int = 0): Int =
if n != 0 then return sum(n - 1, acc + n)
acc

@annotation.tailrec
def isEven(n: Int): Boolean =
if n != 0 && n != 1 then return isEven(n - 2)
if n == 1 then return false
true

def main(args: Array[String]): Unit =
println(sum(3))
println(isEven(5))

0 comments on commit 91053e9

Please sign in to comment.