Skip to content

Fix handling of && and || in TailRec #5185

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 1 commit into from
Oct 1, 2018
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class TailRec extends MiniPhase {
case tree@Apply(fun, args) =>
val meth = fun.symbol
if (meth == defn.Boolean_|| || meth == defn.Boolean_&&)
tpd.cpy.Apply(tree)(fun, transform(args))
tpd.cpy.Apply(tree)(noTailTransform(fun), transform(args))
else
rewriteApply(tree)

Expand Down
21 changes: 21 additions & 0 deletions tests/neg-tailcall/tailrec-and-or.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import annotation.tailrec

class Test {
def cond: Boolean = ???

@tailrec final def tailCall1(x: Int): Boolean =
if (x < 0) tailCall1(0)
else tailCall1(x - 1) || cond // error

@tailrec final def tailCall2(x: Int): Boolean =
if (x < 0) tailCall2(0)
else tailCall2(x - 1) && cond // error

@tailrec final def tailCall3(x: Int): Boolean =
if (x < 0) tailCall3(0)
else cond || tailCall3(x - 1)

@tailrec final def tailCall4(x: Int): Boolean =
if (x < 0) tailCall4(0)
else cond && tailCall4(x - 1)
}