Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Warn trivial recursion with module prefix #23278

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/TailRec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ class TailRec extends MiniPhase {
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 Assign(lhs: Ident, rhs) if lhs.symbol.name.is(TailLocalName) =>
tailArgOrPureExpr(rhs) || varForRewrittenThis.exists(_ == lhs.symbol && rhs.tpe.isStable)
case Assign(lhs: Ident, rhs: Ident) => lhs.symbol == rhs.symbol
case stat: Ident if stat.symbol.name.is(TailLocalName) => true
case _ => tpd.isPureExpr(stat)
Expand Down Expand Up @@ -345,6 +346,9 @@ class TailRec extends MiniPhase {
case prefix: This if prefix.symbol == enclosingClass =>
// Avoid assigning `this = this`
assignParamPairs
case prefix if prefix.symbol.is(Module) && prefix.symbol.moduleClass == enclosingClass =>
// Avoid assigning `this = MyObject`
assignParamPairs
case _ =>
(getVarForRewrittenThis(), noTailTransform(prefix)) :: assignParamPairs

Expand Down
22 changes: 22 additions & 0 deletions tests/warn/i23277.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

enum Test:
case One
case Two(i: Int)

object Test:
object Two:
def apply(i: Int): Test.Two = Test.Two(i) // warn
def apply(i: Int, j: Int): Test.Two = new Test.Two(i+j)
def apply(i: Int, s: String): Two = Two(i, s) // warn because unprefixed Two is deemed pure
def apply(): Test.Two = other.apply() // nowarn prefix is method call
def other: this.type = this

object R extends Runnable:
def r: this.type = this
override def run() = r.run()

final class C(c: C):
def f(i: Int): Int = c.f(i)

@main def main = println:
Test.Two(1)
Loading