Skip to content

Commit 80791ef

Browse files
Backport "Generalize warnings for top-level calls to Any or AnyRef methods" to LTS (#21091)
Backports #20312 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 2492513 + 58a14e8 commit 80791ef

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -1154,12 +1154,12 @@ object RefChecks {
11541154

11551155
end checkImplicitNotFoundAnnotation
11561156

1157-
def checkAnyRefMethodCall(tree: Tree)(using Context) =
1158-
if tree.symbol.exists
1159-
&& defn.topClasses.contains(tree.symbol.owner)
1160-
&& (!ctx.owner.enclosingClass.exists || ctx.owner.enclosingClass.isPackageObject) then
1161-
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)
1162-
1157+
def checkAnyRefMethodCall(tree: Tree)(using Context): Unit =
1158+
if tree.symbol.exists && defn.topClasses.contains(tree.symbol.owner) then
1159+
tree.tpe match
1160+
case tp: NamedType if tp.prefix.typeSymbol != ctx.owner.enclosingClass =>
1161+
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)
1162+
case _ => ()
11631163
}
11641164
import RefChecks.*
11651165

tests/warn/i17266.check

+11
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,14 @@
9696
| resolved to calls on Predef or on imported methods. This might not be what
9797
| you intended.
9898
-------------------------------------------------------------------------------------------------------------------
99+
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:148:2 -------------------------------------------------------
100+
148 | synchronized { // warn
101+
| ^^^^^^^^^^^^
102+
| Suspicious top-level unqualified call to synchronized
103+
|-------------------------------------------------------------------------------------------------------------------
104+
| Explanation (enabled by `-explain`)
105+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106+
| Top-level unqualified calls to AnyRef or Any methods such as synchronized are
107+
| resolved to calls on Predef or on imported methods. This might not be what
108+
| you intended.
109+
-------------------------------------------------------------------------------------------------------------------

tests/warn/i17266.scala

+15-9
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ object Test6:
4343
object Test7:
4444
import MyLib.*
4545
def test7 =
46-
synchronized { // not an error
46+
synchronized { // not an error; resolves to `Test7.synchronized`
4747
println("hello")
4848
}
4949

5050
/*
5151
object Test7b:
52-
def test8 =
52+
def test7b =
5353
import MyLib.*
5454
synchronized { // already an error: Reference to synchronized is ambiguous.
5555
println("hello")
@@ -62,21 +62,21 @@ class Test8:
6262
}
6363

6464
class Test9:
65-
def test5 =
65+
def test9 =
6666
synchronized { // not an error
6767
println("hello")
6868
}
6969

7070
class Test10:
7171
import MyLib.*
72-
synchronized { // not an error
72+
synchronized { // not an error; resolves to `this.synchronized`
7373
println("hello")
7474
}
7575

7676
class Test11:
7777
import MyLib.*
78-
def test7 =
79-
synchronized { // not an error
78+
def test11 =
79+
synchronized { // not an error; resolves to `this.synchronized`
8080
println("hello")
8181
}
8282

@@ -86,14 +86,14 @@ trait Test12:
8686
}
8787

8888
trait Test13:
89-
def test5 =
89+
def test13 =
9090
synchronized { // not an error
9191
println("hello")
9292
}
9393

9494
trait Test14:
9595
import MyLib.*
96-
synchronized { // not an error
96+
synchronized { // not an error; resolves to `this.synchronized`
9797
println("hello")
9898
}
9999

@@ -141,4 +141,10 @@ def test26 =
141141
hashCode() // warn
142142

143143
def test27 =
144-
1.hashCode()// not an error (should be? probably not)
144+
1.hashCode()// not an error (should be? probably not)
145+
146+
def test28 =
147+
import MyLib.*
148+
synchronized { // warn
149+
println("hello")
150+
}

tests/warn/i17493.check

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- [E181] Potential Issue Warning: tests/warn/i17493.scala:4:10 --------------------------------------------------------
2+
4 | def g = synchronized { println("hello, world") } // warn
3+
| ^^^^^^^^^^^^
4+
| Suspicious top-level unqualified call to synchronized
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| Top-level unqualified calls to AnyRef or Any methods such as synchronized are
9+
| resolved to calls on Predef or on imported methods. This might not be what
10+
| you intended.
11+
---------------------------------------------------------------------------------------------------------------------

tests/warn/i17493.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//> using options -explain
2+
class A(val s: String) extends AnyVal {
3+
// def f = eq("hello, world") // no warning for now because `eq` is inlined
4+
def g = synchronized { println("hello, world") } // warn
5+
}

0 commit comments

Comments
 (0)