Skip to content

Commit aab561c

Browse files
committed
Tailrec: drop support for polymorphic recursion.
If the method that recurses over a different type arguments, if this method is specialised, it would lead to method not being tail-rec anymore. Eg: def foo[@specialized A, @specialized B]: Unit = foo[B, A]
1 parent 07833e9 commit aab561c

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class TailRec extends MiniPhaseTransform with DenotTransformer with FullParamete
176176
}
177177

178178
val (prefix, call, arguments, typeArguments, symbol) = receiverArgumentsAndSymbol(tree)
179+
val hasConformingTargs = (typeArguments zip methTparams).forall{x => x._1.tpe <:< x._2.tpe}
179180
val recv = noTailTransform(prefix)
180181

181182
val targs = typeArguments.map(noTailTransform)
@@ -228,7 +229,8 @@ class TailRec extends MiniPhaseTransform with DenotTransformer with FullParamete
228229

229230
if (isRecursiveCall) {
230231
if (ctx.tailPos) {
231-
if (recv eq EmptyTree) rewriteTailCall(This(enclosingClass.asClass))
232+
if (!hasConformingTargs) fail("it changes type arguments on a polymorphic recursive call")
233+
else if (recv eq EmptyTree) rewriteTailCall(This(enclosingClass.asClass))
232234
else if (receiverIsSame || receiverIsThis) rewriteTailCall(recv)
233235
else fail("it changes type of 'this' on a polymorphic recursive call")
234236
}

tests/neg/tailcall/t6574.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Bad[X, Y](val v: Int) extends AnyVal {
44
println("tail")
55
}
66

7-
@annotation.tailrec final def differentTypeArgs : Unit = {
8-
{(); new Bad[String, Unit](0)}.differentTypeArgs
7+
@annotation.tailrec final def differentTypeArgs : Unit = { // error
8+
{(); new Bad[String, Unit](0)}.differentTypeArgs // error
99
}
1010
}

0 commit comments

Comments
 (0)