File tree Expand file tree Collapse file tree 5 files changed +48
-15
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 5 files changed +48
-15
lines changed Original file line number Diff line number Diff line change @@ -1236,12 +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 || ctx.owner.enclosingClass.isPackageObject) then
1243- report.warning(UnqualifiedCallToAnyRefMethod (tree, tree.symbol), tree)
1244-
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 _ => ()
12451245}
12461246import RefChecks .*
12471247
Original file line number Diff line number Diff line change 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+ -------------------------------------------------------------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -43,13 +43,13 @@ object Test6:
4343object 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/*
5151object 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
6464class Test9 :
65- def test5 =
65+ def test9 =
6666 synchronized { // not an error
6767 println(" hello" )
6868 }
6969
7070class Test10 :
7171 import MyLib .*
72- synchronized { // not an error
72+ synchronized { // not an error; resolves to `this.synchronized`
7373 println(" hello" )
7474 }
7575
7676class 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
8888trait Test13 :
89- def test5 =
89+ def test13 =
9090 synchronized { // not an error
9191 println(" hello" )
9292 }
9393
9494trait 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
143143def 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+ }
Original file line number Diff line number Diff line change 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+ ---------------------------------------------------------------------------------------------------------------------
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments