Skip to content

Commit

Permalink
#20145 - bugfix when a return tail recurse is called inside a val def…
Browse files Browse the repository at this point in the history
…inition, previously this would neither optimise nor fail.
  • Loading branch information
Lucy Martin committed Jun 20, 2024
1 parent eb2ea26 commit 036b86b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ class TailRec extends MiniPhase {
tree

case tree: ValDef =>
if (isMandatory) noTailTransform(tree.rhs)
tree
// This could contain a return statement in a code block, so we do have to go into it.
cpy.ValDef(tree)(rhs = noTailTransform(tree.rhs))

case tree: DefDef =>
if (isMandatory)
Expand Down
1 change: 1 addition & 0 deletions tests/run/i20145.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10000001
15 changes: 15 additions & 0 deletions tests/run/i20145.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.annotation.tailrec
@tailrec
def foo(i: Int): Int = {
if (i > 10000000) {
i
} else {
val bar: String = {
return foo(i + 1)
"foo"
}
-1
}
}
@main def Test =
println(foo(0))

0 comments on commit 036b86b

Please sign in to comment.