-
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.
SI-6574 Support @tailrec for extension methods.
Currently, when the body of an extension method is transplanted to the companion object, recursive calls point back to the original instance method. That changes during erasure, but this is too late for tail call analysis/elimination. This commit eagerly updates the recursive calls to point to the extension method in the companion. It also removes the @tailrec annotation from the original method.
- Loading branch information
Showing
6 changed files
with
79 additions
and
4 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,7 @@ | ||
t6574.scala:4: error: could not optimize @tailrec annotated method notTailPos$extension: it contains a recursive call not in tail position | ||
println("tail") | ||
^ | ||
t6574.scala:8: error: could not optimize @tailrec annotated method differentTypeArgs$extension: it is called recursively with different type arguments | ||
{(); new Bad[String, Unit](0)}.differentTypeArgs | ||
^ | ||
two errors found |
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,10 @@ | ||
class Bad[X, Y](val v: Int) extends AnyVal { | ||
@annotation.tailrec final def notTailPos[Z](a: Int)(b: String) { | ||
this.notTailPos[Z](a)(b) | ||
println("tail") | ||
} | ||
|
||
@annotation.tailrec final def differentTypeArgs { | ||
{(); new Bad[String, Unit](0)}.differentTypeArgs | ||
} | ||
} |
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,19 @@ | ||
class Bad[X, Y](val v: Int) extends AnyVal { | ||
def vv = v | ||
@annotation.tailrec final def foo[Z](a: Int)(b: String) { | ||
this.foo[Z](a)(b) | ||
} | ||
|
||
@annotation.tailrec final def differentReceiver { | ||
{(); new Bad[X, Y](0)}.differentReceiver | ||
} | ||
|
||
@annotation.tailrec final def dependent[Z](a: Int)(b: String): b.type = { | ||
this.dependent[Z](a)(b) | ||
} | ||
} | ||
|
||
class HK[M[_]](val v: Int) extends AnyVal { | ||
def hk[N[_]]: Unit = if (false) hk[M] else () | ||
} | ||
|
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 @@ | ||
List(5, 4, 3, 2, 1) |
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,7 @@ | ||
object Test extends App { | ||
implicit class AnyOps(val i: Int) extends AnyVal { | ||
private def parentsOf(x: Int): List[Int] = if (x == 0) Nil else x :: parentsOf(x - 1) | ||
def parents: List[Int] = parentsOf(i) | ||
} | ||
println((5).parents) | ||
} |