Skip to content

Commit 0db9f58

Browse files
mboveljmesyoudiogocanut
committed
Warn when calling synchronized on AnyVal
Co-Authored-By: James You <[email protected]> Co-Authored-By: diogocanut <[email protected]>
1 parent 5da12b7 commit 0db9f58

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
196196
case AmbiguousExtensionMethodID // errorNumber 180
197197
case UnqualifiedCallToAnyRefMethodID // errorNumber: 181
198198
case NotConstantID // errorNumber: 182
199+
case SynchronizedCallOnBoxedClassID // errorNumber: 183
199200

200201
def errorNumber = ordinal - 1
201202

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,15 @@ class UnqualifiedCallToAnyRefMethod(stat: untpd.Tree, method: Symbol)(using Cont
22982298
|you intended."""
22992299
}
23002300

2301+
class SynchronizedCallOnBoxedClass(stat: untpd.Tree)(using Context)
2302+
extends Message(SynchronizedCallOnBoxedClassID) {
2303+
def kind = MessageKind.PotentialIssue
2304+
def msg(using Context) = i"Suspicious ${hl("synchronized")} call on boxed class"
2305+
def explain(using Context) =
2306+
i"""|You called the ${hl("synchronized")} method on a boxed primitive. This might not be what
2307+
|you intended."""
2308+
}
2309+
23012310
class TraitCompanionWithMutableStatic()(using Context)
23022311
extends SyntaxMsg(TraitCompanionWithMutableStaticID) {
23032312
def msg(using Context) = i"Companion of traits cannot define mutable @static fields"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,10 @@ class RefChecks extends MiniPhase { thisPhase =>
11791179
checkAnyRefMethodCall(tree)
11801180
tree
11811181

1182+
override def transformSelect(tree: tpd.Select)(using Context): tpd.Tree =
1183+
if defn.ScalaBoxedClasses().contains(tree.qualifier.tpe.typeSymbol) then
1184+
report.warning(SynchronizedCallOnBoxedClass(tree), tree.srcPos)
1185+
tree
11821186
}
11831187

11841188
/* todo: rewrite and re-enable

tests/neg/17284.check

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- [E183] Potential Issue Error: tests/neg/17284.scala:4:6 -------------------------------------------------------------
2+
4 | 451.synchronized {} // error
3+
| ^^^^^^^^^^^^^^^^
4+
| Suspicious synchronized call on boxed class
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| You called the synchronized method on a boxed primitive. This might not be what
9+
| you intended.
10+
---------------------------------------------------------------------------------------------------------------------
11+
-- [E183] Potential Issue Error: tests/neg/17284.scala:8:4 -------------------------------------------------------------
12+
8 | x.synchronized {} // error
13+
| ^^^^^^^^^^^^^^
14+
| Suspicious synchronized call on boxed class
15+
|---------------------------------------------------------------------------------------------------------------------
16+
| Explanation (enabled by `-explain`)
17+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18+
| You called the synchronized method on a boxed primitive. This might not be what
19+
| you intended.
20+
---------------------------------------------------------------------------------------------------------------------
21+
-- [E183] Potential Issue Error: tests/neg/17284.scala:11:7 ------------------------------------------------------------
22+
11 | true.synchronized {} // error
23+
| ^^^^^^^^^^^^^^^^^
24+
| Suspicious synchronized call on boxed class
25+
|--------------------------------------------------------------------------------------------------------------------
26+
| Explanation (enabled by `-explain`)
27+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
28+
| You called the synchronized method on a boxed primitive. This might not be what
29+
| you intended.
30+
--------------------------------------------------------------------------------------------------------------------

tests/neg/17284.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// scalac: -Werror -explain
2+
3+
def test =
4+
451.synchronized {} // error
5+
6+
def test2 =
7+
val x: Integer = 451
8+
x.synchronized {} // error
9+
10+
def test3 =
11+
true.synchronized {} // error

0 commit comments

Comments
 (0)