File tree 5 files changed +48
-15
lines changed
compiler/src/dotty/tools/dotc/typer
5 files changed +48
-15
lines changed Original file line number Diff line number Diff line change @@ -1154,12 +1154,12 @@ object RefChecks {
1154
1154
1155
1155
end checkImplicitNotFoundAnnotation
1156
1156
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 _ => ()
1163
1163
}
1164
1164
import RefChecks .*
1165
1165
Original file line number Diff line number Diff line change 96
96
| resolved to calls on Predef or on imported methods. This might not be what
97
97
| you intended.
98
98
-------------------------------------------------------------------------------------------------------------------
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:
43
43
object Test7 :
44
44
import MyLib .*
45
45
def test7 =
46
- synchronized { // not an error
46
+ synchronized { // not an error; resolves to `Test7.synchronized`
47
47
println(" hello" )
48
48
}
49
49
50
50
/*
51
51
object Test7b:
52
- def test8 =
52
+ def test7b =
53
53
import MyLib.*
54
54
synchronized { // already an error: Reference to synchronized is ambiguous.
55
55
println("hello")
@@ -62,21 +62,21 @@ class Test8:
62
62
}
63
63
64
64
class Test9 :
65
- def test5 =
65
+ def test9 =
66
66
synchronized { // not an error
67
67
println(" hello" )
68
68
}
69
69
70
70
class Test10 :
71
71
import MyLib .*
72
- synchronized { // not an error
72
+ synchronized { // not an error; resolves to `this.synchronized`
73
73
println(" hello" )
74
74
}
75
75
76
76
class Test11 :
77
77
import MyLib .*
78
- def test7 =
79
- synchronized { // not an error
78
+ def test11 =
79
+ synchronized { // not an error; resolves to `this.synchronized`
80
80
println(" hello" )
81
81
}
82
82
@@ -86,14 +86,14 @@ trait Test12:
86
86
}
87
87
88
88
trait Test13 :
89
- def test5 =
89
+ def test13 =
90
90
synchronized { // not an error
91
91
println(" hello" )
92
92
}
93
93
94
94
trait Test14 :
95
95
import MyLib .*
96
- synchronized { // not an error
96
+ synchronized { // not an error; resolves to `this.synchronized`
97
97
println(" hello" )
98
98
}
99
99
@@ -141,4 +141,10 @@ def test26 =
141
141
hashCode() // warn
142
142
143
143
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
+ }
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