aboutsummaryrefslogtreecommitdiff
path: root/tests/untried/neg/tailrec.scala
diff options
context:
space:
mode:
authorSamuel Gruetter <samuel.gruetter@epfl.ch>2014-03-12 22:44:33 +0100
committerSamuel Gruetter <samuel.gruetter@epfl.ch>2014-03-12 22:44:33 +0100
commit9ef5f6817688f814a3450126aa7383b0928e80a0 (patch)
tree5727a2f7f7fd665cefdb312af2785c692f04377c /tests/untried/neg/tailrec.scala
parent194be919664447631ba55446eb4874979c908d27 (diff)
downloaddotty-9ef5f6817688f814a3450126aa7383b0928e80a0.tar.gz
dotty-9ef5f6817688f814a3450126aa7383b0928e80a0.tar.bz2
dotty-9ef5f6817688f814a3450126aa7383b0928e80a0.zip
add tests from scala/test/files/{pos,neg}
with explicit Unit return type
Diffstat (limited to 'tests/untried/neg/tailrec.scala')
-rw-r--r--tests/untried/neg/tailrec.scala65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/untried/neg/tailrec.scala b/tests/untried/neg/tailrec.scala
new file mode 100644
index 000000000..83a0c1a9e
--- /dev/null
+++ b/tests/untried/neg/tailrec.scala
@@ -0,0 +1,65 @@
+import scala.annotation.tailrec
+
+// putting @tailrec through the paces
+object Winners {
+ @tailrec
+ def facsucc(n: Int, acc: Int): Int =
+ if (n == 0) acc
+ else facsucc(n - 1, n * acc)
+
+ @tailrec def loopsucc1(x: Int): Int = loopsucc1(x - 1)
+ @tailrec def loopsucc2[T](x: Int): Int = loopsucc2[T](x - 1)
+
+ def ding(): Unit = {
+ object dong {
+ @tailrec def loopsucc3(x: Int): Int = loopsucc3(x)
+ }
+ ()
+ }
+
+ def inner(q: Int) = {
+ @tailrec
+ def loopsucc4(x: Int): Int = loopsucc4(x + 1)
+
+ loopsucc4(q)
+ }
+
+ object innerBob {
+ @tailrec def loopsucc5(x: Int): Int = loopsucc5(x)
+ }
+}
+
+class Winners {
+ @tailrec private def succ1(x: Int): Int = succ1(x)
+ @tailrec final def succ2(x: Int): Int = succ2(x)
+ @tailrec final def succ3[T](in: List[T], acc: List[T]): List[T] = in match {
+ case Nil => Nil
+ case x :: xs => succ3(xs, x :: acc)
+ }
+}
+
+object Failures {
+ @tailrec
+ def facfail(n: Int): Int =
+ if (n == 0) 1
+ else n * facfail(n - 1)
+}
+
+class Failures {
+ // not private, not final
+ @tailrec def fail1(x: Int): Int = fail1(x)
+
+ // a typical between-chair-and-keyboard error
+ @tailrec final def fail2[T](xs: List[T]): List[T] = xs match {
+ case Nil => Nil
+ case x :: xs => x :: fail2[T](xs)
+ }
+
+ // unsafe
+ @tailrec final def fail3[T](x: Int): Int = fail3(x - 1)
+
+ // unsafe
+ class Tom[T](x: Int) {
+ @tailrec final def fail4[U](other: Tom[U], x: Int): Int = other.fail4[U](other, x - 1)
+ }
+}