Skip to content

Commit 7a9102a

Browse files
committed
Generalize checkAnyRefMethodCall
1 parent 751cc2f commit 7a9102a

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,14 +1236,12 @@ object RefChecks {
12361236

12371237
end checkImplicitNotFoundAnnotation
12381238

1239-
def checkAnyRefMethodCall(tree: Tree)(using Context) =
1240-
if tree.symbol.exists
1241-
&& defn.topClasses.contains(tree.symbol.owner)
1242-
&& (!ctx.owner.enclosingClass.exists
1243-
|| ctx.owner.enclosingClass.isPackageObject
1244-
|| ctx.owner.enclosingClass.isValueClass) then
1245-
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)
1246-
1239+
def checkAnyRefMethodCall(tree: Tree)(using Context): Unit =
1240+
if tree.symbol.exists && defn.topClasses.contains(tree.symbol.owner) then
1241+
tree.tpe match
1242+
case tp: NamedType if tp.prefix.typeSymbol != ctx.owner.enclosingClass =>
1243+
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)
1244+
case _ => ()
12471245
}
12481246
import RefChecks.*
12491247

tests/warn/i17266.check

Lines changed: 11 additions & 0 deletions
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

Lines changed: 15 additions & 9 deletions
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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
-- [E181] Potential Issue Warning: tests/warn/i17493.scala:3:11 --------------------------------------------------------
2-
3 | def g = synchronized { println("hello, world") } // warn
3-
| ^^^^^^^^^^^^
4-
| Suspicious top-level unqualified call to synchronized
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
55
|---------------------------------------------------------------------------------------------------------------------
66
| Explanation (enabled by `-explain`)
77
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

tests/warn/i17493.scala

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

0 commit comments

Comments
 (0)