Skip to content

Commit

Permalink
Generalize warnings for top-level calls to Any or AnyRef methods (#20312
Browse files Browse the repository at this point in the history
)

751cc2f is a more direct way to fix
#17493 than #20301.

7a9102a further generalizes
`checkAnyRefMethodCall`, as suggested by @odersky.
  • Loading branch information
odersky authored May 1, 2024
2 parents 40050c3 + 27cc89d commit 9348ae7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1236,12 +1236,12 @@ object RefChecks {

end checkImplicitNotFoundAnnotation

def checkAnyRefMethodCall(tree: Tree)(using Context) =
if tree.symbol.exists
&& defn.topClasses.contains(tree.symbol.owner)
&& (!ctx.owner.enclosingClass.exists || ctx.owner.enclosingClass.isPackageObject) then
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)

def checkAnyRefMethodCall(tree: Tree)(using Context): Unit =
if tree.symbol.exists && defn.topClasses.contains(tree.symbol.owner) then
tree.tpe match
case tp: NamedType if tp.prefix.typeSymbol != ctx.owner.enclosingClass =>
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)
case _ => ()
}
import RefChecks.*

Expand Down
11 changes: 11 additions & 0 deletions tests/warn/i17266.check
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,14 @@
| resolved to calls on Predef or on imported methods. This might not be what
| you intended.
-------------------------------------------------------------------------------------------------------------------
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:148:2 -------------------------------------------------------
148 | synchronized { // warn
| ^^^^^^^^^^^^
| Suspicious top-level unqualified call to synchronized
|-------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Top-level unqualified calls to AnyRef or Any methods such as synchronized are
| resolved to calls on Predef or on imported methods. This might not be what
| you intended.
-------------------------------------------------------------------------------------------------------------------
24 changes: 15 additions & 9 deletions tests/warn/i17266.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ object Test6:
object Test7:
import MyLib.*
def test7 =
synchronized { // not an error
synchronized { // not an error; resolves to `Test7.synchronized`
println("hello")
}

/*
object Test7b:
def test8 =
def test7b =
import MyLib.*
synchronized { // already an error: Reference to synchronized is ambiguous.
println("hello")
Expand All @@ -62,21 +62,21 @@ class Test8:
}

class Test9:
def test5 =
def test9 =
synchronized { // not an error
println("hello")
}

class Test10:
import MyLib.*
synchronized { // not an error
synchronized { // not an error; resolves to `this.synchronized`
println("hello")
}

class Test11:
import MyLib.*
def test7 =
synchronized { // not an error
def test11 =
synchronized { // not an error; resolves to `this.synchronized`
println("hello")
}

Expand All @@ -86,14 +86,14 @@ trait Test12:
}

trait Test13:
def test5 =
def test13 =
synchronized { // not an error
println("hello")
}

trait Test14:
import MyLib.*
synchronized { // not an error
synchronized { // not an error; resolves to `this.synchronized`
println("hello")
}

Expand Down Expand Up @@ -141,4 +141,10 @@ def test26 =
hashCode() // warn

def test27 =
1.hashCode()// not an error (should be? probably not)
1.hashCode()// not an error (should be? probably not)

def test28 =
import MyLib.*
synchronized { // warn
println("hello")
}
11 changes: 11 additions & 0 deletions tests/warn/i17493.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- [E181] Potential Issue Warning: tests/warn/i17493.scala:4:10 --------------------------------------------------------
4 | def g = synchronized { println("hello, world") } // warn
| ^^^^^^^^^^^^
| Suspicious top-level unqualified call to synchronized
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Top-level unqualified calls to AnyRef or Any methods such as synchronized are
| resolved to calls on Predef or on imported methods. This might not be what
| you intended.
---------------------------------------------------------------------------------------------------------------------
5 changes: 5 additions & 0 deletions tests/warn/i17493.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//> using options -explain
class A(val s: String) extends AnyVal {
// def f = eq("hello, world") // no warning for now because `eq` is inlined
def g = synchronized { println("hello, world") } // warn
}

0 comments on commit 9348ae7

Please sign in to comment.