Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2da566b

Browse files
committedNov 8, 2018
Properly handle while loops in Tailrec
A while loop condition or body is not in tail position.
1 parent 583a511 commit 2da566b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ class TailRec extends MiniPhase {
382382
case tree: Try =>
383383
rewriteTry(tree)
384384

385+
case tree @ WhileDo(cond, body) =>
386+
// we traverse the body and the condition for the purpose of reporting errors
387+
noTailTransform(cond)
388+
noTailTransform(body)
389+
tree
390+
385391
case _: Alternative | _: Bind =>
386392
assert(false, "We should never have gotten inside a pattern")
387393
tree

‎tests/neg-tailcall/while-loops.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import annotation.tailrec
2+
3+
object WhileLoops {
4+
def cond: Boolean = ???
5+
6+
@tailrec def rec1: Unit = { // error: tailrec not applicable
7+
while (cond) {
8+
rec1 // error: not in tail position
9+
}
10+
}
11+
12+
@tailrec def rec2: Boolean = { // error: tailrec not applicable
13+
while (rec2) { } // error: not in tail position
14+
true
15+
}
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.