Skip to content

Commit 1cf8d46

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 f429504 commit 1cf8d46

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
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
}

test/dotc/tests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class tests extends CompilerTest {
131131
val negTailcallDir = negDir + "tailcall/"
132132
@Test def neg_tailcall_t1672b = compileFile(negTailcallDir, "t1672b", xerrors = 5)
133133
@Test def neg_tailcall_t3275 = compileFile(negTailcallDir, "t3275", xerrors = 1)
134-
@Test def neg_tailcall_t6574 = compileFile(negTailcallDir, "t6574", xerrors = 2)
134+
@Test def neg_tailcall_t6574 = compileFile(negTailcallDir, "t6574", xerrors = 4)
135135
@Test def neg_tailcall = compileFile(negTailcallDir, "tailrec", xerrors = 7)
136136
@Test def neg_tailcall2 = compileFile(negTailcallDir, "tailrec-2", xerrors = 2)
137137
@Test def neg_tailcall3 = compileFile(negTailcallDir, "tailrec-3", xerrors = 2)

0 commit comments

Comments
 (0)