From c2534bf61b61b76cebaad09ba303234193709b60 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Fri, 22 Feb 2013 15:53:29 +0100 Subject: 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: https://github.com/scala/scala/pull/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. --- src/compiler/scala/tools/nsc/transform/TailCalls.scala | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/transform/TailCalls.scala b/src/compiler/scala/tools/nsc/transform/TailCalls.scala index a767850cba..938499261e 100644 --- a/src/compiler/scala/tools/nsc/transform/TailCalls.scala +++ b/src/compiler/scala/tools/nsc/transform/TailCalls.scala @@ -220,7 +220,10 @@ abstract class TailCalls extends Transform { debuglog("Rewriting tail recursive call: " + fun.pos.lineContent.trim) accessed += ctx.label - typedPos(fun.pos)(Apply(Ident(ctx.label), noTailTransform(recv) :: transformArgs)) + typedPos(fun.pos) { + val args = mapWithIndex(transformArgs)((arg, i) => mkAttributedCastHack(arg, ctx.label.info.params(i + 1).tpe)) + Apply(Ident(ctx.label), noTailTransform(recv) :: args) + } } if (!ctx.isEligible) fail("it is neither private nor final so can be overridden") @@ -280,7 +283,7 @@ abstract class TailCalls extends Transform { typedPos(tree.pos)(Block( List(ValDef(newThis, This(currentClass))), - LabelDef(newCtx.label, newThis :: vpSyms, newRHS) + LabelDef(newCtx.label, newThis :: vpSyms, mkAttributedCastHack(newRHS, newCtx.label.tpe.resultType)) )) } else { @@ -377,6 +380,13 @@ abstract class TailCalls extends Transform { super.transform(tree) } } + + // Workaround for SI-6900. Uncurry installs an InfoTransformer and a tree Transformer. + // These leave us with conflicting view on method signatures; the parameter symbols in + // the MethodType can be clones of the ones originally found on the parameter ValDef, and + // consequently appearing in the typechecked RHS of the method. + private def mkAttributedCastHack(tree: Tree, tpe: Type) = + gen.mkAttributedCast(tree, tpe) } // collect the LabelDefs (generated by the pattern matcher) in a DefDef that are in tail position -- cgit v1.2.3