Skip to content

Commit 0a1ac7e

Browse files
mbovelWojciechMazur
authored andcommitted
Add warning for synchronized calls in value classes
Co-Authored-By: Yoonjae Jeon <18438185+nox213@users.noreply.github.com> [Cherry-picked 751cc2f]
1 parent 49a97d7 commit 0a1ac7e

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,9 @@ object RefChecks {
11571157
def checkAnyRefMethodCall(tree: Tree)(using Context) =
11581158
if tree.symbol.exists
11591159
&& defn.topClasses.contains(tree.symbol.owner)
1160-
&& (!ctx.owner.enclosingClass.exists || ctx.owner.enclosingClass.isPackageObject) then
1160+
&& (!ctx.owner.enclosingClass.exists
1161+
|| ctx.owner.enclosingClass.isPackageObject
1162+
|| ctx.owner.enclosingClass.isValueClass) then
11611163
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)
11621164

11631165
}

tests/warn/i17493.check

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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
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+
---------------------------------------------------------------------------------------------------------------------

tests/warn/i17493.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//> using options -explain
2+
class A(val s: String) extends AnyVal {
3+
def g = synchronized { println("hello, world") } // warn
4+
}

0 commit comments

Comments
 (0)