-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tailcalls is more selective when reporting self-recursion
- Loading branch information
Showing
4 changed files
with
118 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
t4649.scala:12: error: could not optimize @tailrec annotated method remove: @tailrec annotated method contains no recursive calls | ||
@tailrec final def remove(idx: Int, count: Int): Unit = | ||
^ | ||
1 error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
import annotation.tailrec | ||
|
||
object Test { | ||
|
||
var sz = 3 | ||
def remove(idx: Int) = | ||
if (idx >= 0 && idx < sz) | ||
sz -= 1 | ||
else throw new IndexOutOfBoundsException(s"$idx is out of bounds (min 0, max ${sz-1})") | ||
|
||
@tailrec final def remove(idx: Int, count: Int): Unit = | ||
if (count > 0) { | ||
remove(idx) // at a glance, looks like a tailrec candidate, but must error in the end | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,37 @@ | ||
//> abusing options -Vlog:tailcalls -Vdebug -Vprint:~tailcalls | ||
|
||
import annotation.tailrec | ||
|
||
// scalac: -Xfatal-warnings | ||
// | ||
object Test { | ||
// @annotation.tailrec | ||
@tailrec | ||
def lazyFilter[E](s: LazyList[E], p: E => Boolean): LazyList[E] = s match { | ||
case h #:: t => if (p(h)) h #:: lazyFilter(t, p) else lazyFilter(t, p) | ||
case _ => LazyList.empty[E] | ||
} | ||
|
||
@tailrec | ||
def f(i: Int): Int = | ||
if (i <= 0) i | ||
/* not optimized | ||
else if (i == 27) { | ||
val x = f(i - 1) | ||
x | ||
} | ||
*/ | ||
else if (i == 42) { | ||
val g: Int => Int = f(_) | ||
f(i - 1) | ||
} | ||
else f(i - 1) | ||
|
||
var sz = 3 | ||
def remove(idx: Int) = | ||
if (idx >= 0 && idx < sz) | ||
sz -= 1 | ||
else throw new IndexOutOfBoundsException(s"$idx is out of bounds (min 0, max ${sz-1})") | ||
|
||
@tailrec final def remove(idx: Int, count: Int): Unit = | ||
if (count > 0) { | ||
remove(idx) // after rewrite, don't flag me as a leftover tailrec | ||
remove(idx, count-1) | ||
} | ||
} |