Skip to content

Generalize warnings for top-level calls to Any or AnyRef methods #20312

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

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
}
Loading