Skip to content

Commit 3ac6aad

Browse files
committed
wip: neg/t6276.scala
vals no longer have rhs after fields, so must check the assign node it was lifted out to
1 parent 892e13c commit 3ac6aad

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/compiler/scala/tools/nsc/typechecker/RefChecks.scala

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,22 +1127,16 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
11271127
case _ =>
11281128
}
11291129

1130-
// SI-6276 warn for `def foo = foo` or `val bar: X = bar`, which come up more frequently than you might think.
1131-
def checkInfiniteLoop(valOrDef: ValOrDefDef) {
1132-
def callsSelf = valOrDef.rhs match {
1133-
case t @ (Ident(_) | Select(This(_), _)) =>
1134-
t hasSymbolWhich (_.accessedOrSelf == valOrDef.symbol)
1135-
case _ => false
1130+
// SI-6276 warn for trivial recursion, such as `def foo = foo` or `val bar: X = bar`, which come up more frequently than you might think.
1131+
// TODO: Move to abide rule. Also, this does not check that the def is final or not overridden, for example
1132+
def checkInfiniteLoop(sym: Symbol, rhs: Tree): Unit =
1133+
if (!sym.isValueParameter && sym.paramss.isEmpty) {
1134+
rhs match {
1135+
case t@(Ident(_) | Select(This(_), _)) if t hasSymbolWhich (_.accessedOrSelf == sym) =>
1136+
reporter.warning(rhs.pos, s"${sym.fullLocationString} does nothing other than call itself recursively")
1137+
case _ =>
1138+
}
11361139
}
1137-
val trivialInfiniteLoop = (
1138-
!valOrDef.isErroneous
1139-
&& !valOrDef.symbol.isValueParameter
1140-
&& valOrDef.symbol.paramss.isEmpty
1141-
&& callsSelf
1142-
)
1143-
if (trivialInfiniteLoop)
1144-
reporter.warning(valOrDef.rhs.pos, s"${valOrDef.symbol.fullLocationString} does nothing other than call itself recursively")
1145-
}
11461140

11471141
// Transformation ------------------------------------------------------------
11481142

@@ -1621,9 +1615,18 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
16211615
sym resetFlag DEFERRED
16221616
transform(deriveDefDef(tree)(_ => typed(gen.mkSysErrorCall("native method stub"))))
16231617

1624-
case ValDef(_, _, _, _) | DefDef(_, _, _, _, _, _) =>
1618+
case Assign(tgt, rhs) =>
1619+
if (!tree.isErroneous)
1620+
checkInfiniteLoop(tgt.symbol, rhs)
1621+
tree
1622+
1623+
// NOTE: The fields phase has moved the RHS of ValDefs in classes to an initialization statement in the template.
1624+
case tree: ValOrDefDef =>
16251625
checkDeprecatedOvers(tree)
1626-
checkInfiniteLoop(tree.asInstanceOf[ValOrDefDef])
1626+
1627+
if (!tree.isErroneous)
1628+
checkInfiniteLoop(tree.symbol, tree.rhs)
1629+
16271630
if (settings.warnNullaryUnit)
16281631
checkNullaryMethodReturnType(sym)
16291632
if (settings.warnInaccessible) {

0 commit comments

Comments
 (0)