Skip to content

Commit

Permalink
Fix #20145: bugfix when a return tailrec is called inside a val def. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrd authored Jun 20, 2024
2 parents 24efe7d + 036b86b commit 56e6f9f
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 56e6f9f

Please sign in to comment.