Skip to content

Commit dea5b00

Browse files
author
Lucy Martin
committed
Changes from PR.
1 parent 73aae73 commit dea5b00

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -1914,10 +1914,10 @@ class TailrecNestedCall(definition: Symbol, innerDef: Symbol)(using Context)
19141914
}
19151915

19161916
def explain(using Context) =
1917-
"""Tail recursion is only validated and optimised directly in the definition
1918-
|any calls to the recursive method via an inner def cannot be validated as
1917+
"""Tail recursion is only validated and optimised directly in the definition.
1918+
|Any calls to the recursive method via an inner def cannot be validated as
19191919
|tail recursive, nor optimised if they are. To enable tail recursion from
1920-
|inner calls, mark the inner def as inline
1920+
|inner calls, mark the inner def as inline.
19211921
|""".stripMargin
19221922
}
19231923

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ class TailRec extends MiniPhase {
438438
if (tree.symbol.is(Synthetic))
439439
noTailTransform(tree.rhs)
440440
else
441-
// We cant tail recurse through nested definitions, so dont want to propagate to child nodes
442-
// We dont want to fail if there is a call that would recurse (as this would be a non self recurse), so dont
441+
// We can't tail recurse through nested definitions, so don't want to propagate to child nodes
442+
// We don't want to fail if there is a call that would recurse (as this would be a non self recurse), so don't
443443
// want to call noTailTransform
444444
// We can however warn in this case, as its likely in this situation that someone would expect a tail
445445
// recursion optimization and enabling this to optimise would be a simple case of inlining the inner method
@@ -459,8 +459,8 @@ class TailRec extends MiniPhase {
459459

460460
case Return(expr, from) =>
461461
val fromSym = from.symbol
462-
val inTailPosition = (!fromSym.is(Label) || tailPositionLabeledSyms.contains(fromSym)) // Label returns are only tail if the label is in tail position
463-
&& (!fromSym.is(Method) || (fromSym eq method)) // Method returns are only tail if we are looking at the original method
462+
val inTailPosition = tailPositionLabeledSyms.contains(fromSym) // Label returns are only tail if the label is in tail position
463+
|| (fromSym eq method) // Method returns are only tail if we are looking at the original method
464464
cpy.Return(tree)(transform(expr, inTailPosition), from)
465465

466466
case _ =>

tests/neg/i20105.check

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- [E195] Syntax Warning: tests\neg\i20105.scala:6:9 -------------------------------------------------------------------
2+
6 | foo()
3+
| ^^^^^
4+
| The tail recursive def foo contains a recursive call inside the non-inlined inner def bar
5+
|
6+
| longer explanation available when compiling with `-explain`
7+
-- [E097] Syntax Error: tests\neg\i20105.scala:3:4 ---------------------------------------------------------------------
8+
3 |def foo(): Unit = // error
9+
| ^
10+
| TailRec optimisation not applicable, method foo contains no recursive calls

tests/warn/i20105.check

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E195] Syntax Warning: tests\warn\i20105.scala:6:9 ------------------------------------------------------------------
2+
6 | foo() // warn
3+
| ^^^^^
4+
| The tail recursive def foo contains a recursive call inside the non-inlined inner def bar
5+
|
6+
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)