Skip to content

Commit

Permalink
Add warning for synchronized calls in value classes
Browse files Browse the repository at this point in the history
Co-Authored-By: Yoonjae Jeon <[email protected]>
  • Loading branch information
mbovel and nox213 committed May 1, 2024
1 parent 48aac2c commit 751cc2f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,9 @@ object RefChecks {
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
&& (!ctx.owner.enclosingClass.exists
|| ctx.owner.enclosingClass.isPackageObject
|| ctx.owner.enclosingClass.isValueClass) then
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)

}
Expand Down
11 changes: 11 additions & 0 deletions tests/warn/i17493.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- [E181] Potential Issue Warning: tests/warn/i17493.scala:3:11 --------------------------------------------------------
3 | 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.
---------------------------------------------------------------------------------------------------------------------
4 changes: 4 additions & 0 deletions tests/warn/i17493.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//> using options -explain
class A(val s: String) extends AnyVal {
def g = synchronized { println("hello, world") } // warn
}

0 comments on commit 751cc2f

Please sign in to comment.