diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index cdfd137e5661..2bf4b959ebca 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -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.* diff --git a/tests/warn/i17266.check b/tests/warn/i17266.check index 716cd531dd0a..ce8626b14225 100644 --- a/tests/warn/i17266.check +++ b/tests/warn/i17266.check @@ -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. + ------------------------------------------------------------------------------------------------------------------- diff --git a/tests/warn/i17266.scala b/tests/warn/i17266.scala index 7e0c9f1b653b..f5d3d02b3661 100644 --- a/tests/warn/i17266.scala +++ b/tests/warn/i17266.scala @@ -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") @@ -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") } @@ -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") } @@ -141,4 +141,10 @@ def test26 = hashCode() // warn def test27 = - 1.hashCode()// not an error (should be? probably not) \ No newline at end of file + 1.hashCode()// not an error (should be? probably not) + +def test28 = + import MyLib.* + synchronized { // warn + println("hello") + } diff --git a/tests/warn/i17493.check b/tests/warn/i17493.check new file mode 100644 index 000000000000..8a4c102980fe --- /dev/null +++ b/tests/warn/i17493.check @@ -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. + --------------------------------------------------------------------------------------------------------------------- diff --git a/tests/warn/i17493.scala b/tests/warn/i17493.scala new file mode 100644 index 000000000000..f76f3aeb02af --- /dev/null +++ b/tests/warn/i17493.scala @@ -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 +}