Skip to content

Fix #1687: postpone computations in tailrec until they are needed. #1909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 39 additions & 34 deletions compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,13 @@ class TailRec extends MiniPhaseTransform with DenotTransformer with FullParamete

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

val targs = typeArguments.map(noTailTransform)
val argumentss = arguments.map(noTailTransforms)

val recvWiden = recv.tpe.widenDealias

val receiverIsSame = enclosingClass.typeRef.widenDealias =:= recvWiden
val receiverIsSuper = (method.name eq sym) && enclosingClass.typeRef.widen <:< recvWiden
val receiverIsThis = recv.tpe =:= thisType || recv.tpe.widen =:= thisType

val isRecursiveCall = (method eq sym)
val recvWiden = prefix.tpe.widenDealias


def continue = {
val method = noTailTransform(call)
Expand All @@ -244,40 +239,50 @@ class TailRec extends MiniPhaseTransform with DenotTransformer with FullParamete
continue
}

def rewriteTailCall(recv: Tree): Tree = {
c.debuglog("Rewriting tail recursive call: " + tree.pos)
rewrote = true
val receiver = noTailTransform(recv)

val callTargs: List[tpd.Tree] =
if (abstractOverClass) {
val classTypeArgs = recv.tpe.baseTypeWithArgs(enclosingClass).argInfos
targs ::: classTypeArgs.map(x => ref(x.typeSymbol))
} else targs

val method = if (callTargs.nonEmpty) TypeApply(Ident(label.termRef), callTargs) else Ident(label.termRef)
val thisPassed =
if (this.method.owner.isClass)
method.appliedTo(receiver.ensureConforms(method.tpe.widen.firstParamTypes.head))
else method

val res =
if (thisPassed.tpe.widen.isParameterless) thisPassed
else argumentss.foldLeft(thisPassed) {
(met, ar) => Apply(met, ar) // Dotty deviation no auto-detupling yet.
}
res
}


if (isRecursiveCall) {
if (ctx.tailPos) {
val receiverIsSame = enclosingClass.typeRef.widenDealias =:= recvWiden
val receiverIsThis = prefix.tpe =:= thisType || prefix.tpe.widen =:= thisType

def rewriteTailCall(recv: Tree): Tree = {
c.debuglog("Rewriting tail recursive call: " + tree.pos)
rewrote = true
val receiver = noTailTransform(recv)

val callTargs: List[tpd.Tree] =
if (abstractOverClass) {
val classTypeArgs = recv.tpe.baseTypeWithArgs(enclosingClass).argInfos
targs ::: classTypeArgs.map(x => ref(x.typeSymbol))
} else targs

val method = if (callTargs.nonEmpty) TypeApply(Ident(label.termRef), callTargs) else Ident(label.termRef)
val thisPassed =
if (this.method.owner.isClass)
method.appliedTo(receiver.ensureConforms(method.tpe.widen.firstParamTypes.head))
else method

val res =
if (thisPassed.tpe.widen.isParameterless) thisPassed
else argumentss.foldLeft(thisPassed) {
(met, ar) => Apply(met, ar) // Dotty deviation no auto-detupling yet.
}
res
}

if (!hasConformingTargs) fail("it changes type arguments on a polymorphic recursive call")
else if (recv eq EmptyTree) rewriteTailCall(This(enclosingClass.asClass))
else if (receiverIsSame || receiverIsThis) rewriteTailCall(recv)
else fail("it changes type of 'this' on a polymorphic recursive call")
else {
val recv = noTailTransform(prefix)
if (recv eq EmptyTree) rewriteTailCall(This(enclosingClass.asClass))
else if (receiverIsSame || receiverIsThis) rewriteTailCall(recv)
else fail("it changes type of 'this' on a polymorphic recursive call")
}
}
else fail(defaultReason)
} else {
val receiverIsSuper = (method.name eq sym) && enclosingClass.typeRef.widen <:< recvWiden

if (receiverIsSuper) fail("it contains a recursive call targeting a supertype")
else continue
}
Expand Down
10 changes: 10 additions & 0 deletions tests/pos/i1687.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object O {
def f: String = {
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1
}
}
19 changes: 19 additions & 0 deletions tests/pos/i1687b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object O {
def f: String = {
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1 +
"1" + 1 + "1" + 1
}
}