Skip to content

Commit c2534bf

Browse files
committed
SI-6900 Fix tailrec for dependent method types
Uncurry's info transformer could generate a MethodType with cloned parameter symbols. This type was used for the LabelDef generated in the TailCalls phase. But, the RHS of the method still contains types that refer to the original parmameter symbol. Spurious type errors ensued. I've spent a good chunk of time pursuing a more principled fix, in which we keep the symbols in the tree in sync with those in the MethodType. You can relive the procession of false dawns: scala#2248 Ultimately that scheme was derailed by a mismatch between the type parameter `T` and the skolem `T&` in the example below. trait Endo[A] { def apply(a: => A): A } class Test { def foo[T] = new Endo[(T, Unit)] { def apply(v1: => (T, Unit)) = v1 // no bridge created } } Interestingly, by removing the caching in SingleType, I got past that problem. But I didn't characterize it further. This commit sets asides the noble goal of operating in the world of types, and sledgehammers past the crash by casting the arguments to and the result of the label jump generated in TailCalls.
1 parent d7545ec commit c2534bf

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/compiler/scala/tools/nsc/transform/TailCalls.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ abstract class TailCalls extends Transform {
220220
debuglog("Rewriting tail recursive call: " + fun.pos.lineContent.trim)
221221

222222
accessed += ctx.label
223-
typedPos(fun.pos)(Apply(Ident(ctx.label), noTailTransform(recv) :: transformArgs))
223+
typedPos(fun.pos) {
224+
val args = mapWithIndex(transformArgs)((arg, i) => mkAttributedCastHack(arg, ctx.label.info.params(i + 1).tpe))
225+
Apply(Ident(ctx.label), noTailTransform(recv) :: args)
226+
}
224227
}
225228

226229
if (!ctx.isEligible) fail("it is neither private nor final so can be overridden")
@@ -280,7 +283,7 @@ abstract class TailCalls extends Transform {
280283

281284
typedPos(tree.pos)(Block(
282285
List(ValDef(newThis, This(currentClass))),
283-
LabelDef(newCtx.label, newThis :: vpSyms, newRHS)
286+
LabelDef(newCtx.label, newThis :: vpSyms, mkAttributedCastHack(newRHS, newCtx.label.tpe.resultType))
284287
))
285288
}
286289
else {
@@ -377,6 +380,13 @@ abstract class TailCalls extends Transform {
377380
super.transform(tree)
378381
}
379382
}
383+
384+
// Workaround for SI-6900. Uncurry installs an InfoTransformer and a tree Transformer.
385+
// These leave us with conflicting view on method signatures; the parameter symbols in
386+
// the MethodType can be clones of the ones originally found on the parameter ValDef, and
387+
// consequently appearing in the typechecked RHS of the method.
388+
private def mkAttributedCastHack(tree: Tree, tpe: Type) =
389+
gen.mkAttributedCast(tree, tpe)
380390
}
381391

382392
// collect the LabelDefs (generated by the pattern matcher) in a DefDef that are in tail position

test/files/run/t6900.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import annotation.tailrec
2+
3+
trait Universe {
4+
type T <: AnyRef
5+
}
6+
7+
final class Bug {
8+
var i = 1
9+
def stop() = { i -= 1; i < 0 }
10+
// the alias bypasses the fast path in erasures InfoTransformer
11+
// predicated on `TypeMap.noChangeToSymbols`
12+
type Alias = Any
13+
14+
@tailrec
15+
// So we get two symbols for `universe`, the original on the ValDef
16+
// and a clone in the MethodType of `f`.
17+
def f(universe: Universe, l: Alias): universe.T = {
18+
if (stop()) null.asInstanceOf[universe.T] else f(universe, null)
19+
}
20+
21+
@tailrec
22+
def g(universe: Universe)(l: Alias): universe.T = {
23+
if (stop()) null.asInstanceOf[universe.T] else g(universe)(l)
24+
}
25+
26+
@tailrec
27+
def h(universe: Universe)(l: List[universe.T]): List[universe.T] = {
28+
if (stop()) Nil else h(universe)(l)
29+
}
30+
}
31+
32+
object Test extends App {
33+
assert(new Bug().f(null, null) == null)
34+
assert(new Bug().g(null)(null) == null)
35+
assert(new Bug().h(null)(null) == Nil)
36+
}

0 commit comments

Comments
 (0)