Skip to content
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

#20145 - bugfix when a return tail rec is called inside a val #20632

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 5 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,11 @@ 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)(
tree.name,
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelatedly, you can assert(foo(0) == K) to avoid introducing a check file.

Also, a PR can be in "draft" mode to mean "not ready for review". If you accidentally push to your branch while a PR is closed, it doesn't let you re-open the PR (for some reason).

Loading