Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 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,25 @@ class TailRec extends MiniPhase {
).transform(rhsSemiTransformed)
}

// Is a simple this a simple recursive tail call, possibly with swapped or modified pure arguments
def isInfiniteRecCall(tree: Tree): Boolean = {
def statOk(stat: Tree): Boolean = stat match {
case stat: ValDef if stat.name.is(TailTempName) || !stat.symbol.is(Mutable) => statOk(stat.rhs)
case Assign(lhs: Ident, rhs: Ident) if lhs.symbol.name.is(TailLocalName) => statOk(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(statOk) && 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
}
10 changes: 10 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i7821b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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

}