Skip to content

Fix #7821: Warn on simple infinite tailrec loops #7924

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 3 commits into from
Jan 9, 2020
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
25 changes: 25 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.ast.{TreeTypeMap, tpd}
import dotty.tools.dotc.config.Printers.tailrec
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Constants.Constant
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.NameKinds.{TailLabelName, TailLocalName, TailTempName}
Expand Down Expand Up @@ -174,6 +175,30 @@ class TailRec extends MiniPhase {
).transform(rhsSemiTransformed)
}

/** Is the RHS a direct recursive tailcall, possibly with swapped arguments or modified pure arguments.
* ```
* def f(<params>): T = f(<args>)
* ```
* where `<args>` are pure arguments or references to parameters in `<params>`.
*/
def isInfiniteRecCall(tree: Tree): Boolean = {
def tailArgOrPureExpr(stat: Tree): Boolean = stat match {
case stat: ValDef if stat.name.is(TailTempName) || !stat.symbol.is(Mutable) => tailArgOrPureExpr(stat.rhs)
case Assign(lhs: Ident, rhs) if lhs.symbol.name.is(TailLocalName) => tailArgOrPureExpr(rhs)
case stat: Ident if stat.symbol.name.is(TailLocalName) => true
case _ => tpd.isPureExpr(stat)
}
tree match {
case Typed(expr, _) => isInfiniteRecCall(expr)
case Return(Literal(Constant(())), label) => label.symbol == transformer.continueLabel
case Block(stats, expr) => stats.forall(tailArgOrPureExpr) && isInfiniteRecCall(expr)
case _ => false
}
}

if isInfiniteRecCall(rhsFullyTransformed) then
ctx.warning("Infinite recursive call", tree.sourcePos)

cpy.DefDef(tree)(rhs =
Block(
initialVarDefs,
Expand Down
24 changes: 24 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i7821.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
object XObject {
opaque type X = Int

def anX: X = 5

given ops: Object {
def (x: X) + (y: X): X = x + y
}
}

object MyXObject {
opaque type MyX = XObject.X

def anX: MyX = XObject.anX

given ops: Object {
def (x: MyX) + (y: MyX): MyX = x + y // error: warring: Infinite recursive call
}
}

object Main extends App {
println(XObject.anX + XObject.anX) // prints 10
println(MyXObject.anX + MyXObject.anX) // infinite loop
}
11 changes: 11 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i7821b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Test {

{ def f(x: Int, y: Int): Int = f(x, y) } // error
{ def f(x: Int, y: Int): Int = { f(x, y) } } // error
{ def f(x: Int, y: Int): Int = f(y, x) } // error
{ def f(x: Int, y: Int): Int = f(x, x) } // error
{ def f(x: Int, y: Int): Int = f(1, 1) } // error
{ def f(x: Int, y: Int): Int = { val a = 3; f(a, 1) } } // error
{ def f(x: Int): Int = f(1) } // error

}