summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/RefChecks.scala7
-rw-r--r--test/files/neg/t6276.check16
-rw-r--r--test/files/neg/t6276.scala17
3 files changed, 33 insertions, 7 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
index f1f2794a95..d515934c58 100644
--- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
@@ -1208,11 +1208,16 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R
// SI-6276 warn for `def foo = foo` or `val bar: X = bar`, which come up more frequently than you might think.
def checkInfiniteLoop(valOrDef: ValOrDefDef) {
+ def callsSelf = valOrDef.rhs match {
+ case t @ (Ident(_) | Select(This(_), _)) =>
+ t hasSymbolWhich (_.accessedOrSelf == valOrDef.symbol)
+ case _ => false
+ }
val trivialInifiniteLoop = (
!valOrDef.isErroneous
&& !valOrDef.symbol.isValueParameter
&& valOrDef.symbol.paramss.isEmpty
- && valOrDef.rhs.hasSymbolWhich(_.accessedOrSelf == valOrDef.symbol)
+ && callsSelf
)
if (trivialInifiniteLoop)
unit.warning(valOrDef.rhs.pos, s"${valOrDef.symbol.fullLocationString} does nothing other than call itself recursively")
diff --git a/test/files/neg/t6276.check b/test/files/neg/t6276.check
index 8bd92cf293..0b3dfa5531 100644
--- a/test/files/neg/t6276.check
+++ b/test/files/neg/t6276.check
@@ -1,13 +1,19 @@
-t6276.scala:4: error: method a does nothing other than call itself recursively
+t6276.scala:4: error: method a in class C does nothing other than call itself recursively
def a: Any = a // warn
^
-t6276.scala:5: error: value b does nothing other than call itself recursively
+t6276.scala:5: error: value b in class C does nothing other than call itself recursively
val b: Any = b // warn
^
-t6276.scala:10: error: method a does nothing other than call itself recursively
+t6276.scala:7: error: method c in class C does nothing other than call itself recursively
+ def c: Any = this.c // warn
+ ^
+t6276.scala:8: error: method d in class C does nothing other than call itself recursively
+ def d: Any = C.this.d // warn
+ ^
+t6276.scala:13: error: method a does nothing other than call itself recursively
def a: Any = a // warn
^
-t6276.scala:19: error: method a does nothing other than call itself recursively
+t6276.scala:22: error: method a does nothing other than call itself recursively
def a = a // warn
^
-four errors found
+6 errors found
diff --git a/test/files/neg/t6276.scala b/test/files/neg/t6276.scala
index 8333618964..bd0a473f71 100644
--- a/test/files/neg/t6276.scala
+++ b/test/files/neg/t6276.scala
@@ -1,8 +1,11 @@
object Test {
def foo(a: Int, b: Int, c: Int) {
- new {
+ class C {
def a: Any = a // warn
val b: Any = b // warn
+
+ def c: Any = this.c // warn
+ def d: Any = C.this.d // warn
}
def method {
@@ -25,5 +28,17 @@ object Test {
val b: Any = {println(""); b}
def c(i: Int): Any = c(i - 0)
}
+
+ class D {
+ def other: D = null
+ def foo: Any = other.foo
+ }
+
+ class E {
+ def foo: Any = 0
+ class D extends E {
+ override def foo: Any = E.this.foo
+ }
+ }
}
}