Skip to content

Commit

Permalink
Potential options for different handling of defined and implicit inne…
Browse files Browse the repository at this point in the history
…r methods
  • Loading branch information
Lucy Martin committed Apr 10, 2024
1 parent 15056b5 commit f510492
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
15 changes: 9 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,15 @@ class TailRec extends MiniPhase {

case tree: DefDef =>
if (isMandatory)
// We cant tail recurse through nested definitions, so dont want to propagate to child nodes
// We dont want to fail if there is a call that would recurse (as this would be a non self recurse), so dont
// want to call noTailTransform
// We can however warn in this case, as its likely in this situation that someone would expect a tail
// recursion optimization and enabling this to optimise would be a simple case of inlining the inner method
new NestedTailRecAlerter(method, tree.symbol).traverse(tree)
if (tree.symbol.is(Synthetic))
noTailTransform(tree.rhs)
else
// We cant tail recurse through nested definitions, so dont want to propagate to child nodes
// We dont want to fail if there is a call that would recurse (as this would be a non self recurse), so dont
// want to call noTailTransform
// We can however warn in this case, as its likely in this situation that someone would expect a tail
// recursion optimization and enabling this to optimise would be a simple case of inlining the inner method
new NestedTailRecAlerter(method, tree.symbol).traverse(tree)
tree

case _: Super | _: This | _: Literal | _: TypeTree | _: TypeDef | EmptyTree =>
Expand Down
3 changes: 2 additions & 1 deletion tests/neg/i20105.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import scala.annotation.tailrec
@tailrec
def foo(): Unit =
def foo(): Unit = // error
def bar(): Unit =
if (???)
foo()
Expand Down
6 changes: 4 additions & 2 deletions tests/neg/i5397.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ object Test {
rec3 // error: not in tail position
})

@tailrec def rec4: Unit = {
def local = rec4 // error: not in tail position
// This is technically not breaching tail recursion as rec4 does not call itself, local does
// This instead fails due to having no tail recursion at all
@tailrec def rec4: Unit = { // error: no recursive calls
def local = rec4
}

@tailrec def rec5: Int = {
Expand Down
1 change: 1 addition & 0 deletions tests/warn/i20105.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import scala.annotation.tailrec
@tailrec
def foo(): Unit =
def bar(): Unit =
Expand Down

0 comments on commit f510492

Please sign in to comment.