Skip to content

Commit 56e6f9f

Browse files
authored
Fix #20145: bugfix when a return tailrec is called inside a val def. (#20652)
2 parents 24efe7d + 036b86b commit 56e6f9f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

compiler/src/dotty/tools/dotc/transform/TailRec.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ class TailRec extends MiniPhase {
430430
tree
431431

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

436436
case tree: DefDef =>
437437
if (isMandatory)

tests/run/i20145.check

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10000001

tests/run/i20145.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.annotation.tailrec
2+
@tailrec
3+
def foo(i: Int): Int = {
4+
if (i > 10000000) {
5+
i
6+
} else {
7+
val bar: String = {
8+
return foo(i + 1)
9+
"foo"
10+
}
11+
-1
12+
}
13+
}
14+
@main def Test =
15+
println(foo(0))

0 commit comments

Comments
 (0)