-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add warning for synchronized on value classes #20301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1155,6 +1155,11 @@ object RefChecks { | |
then report.warning(ExtensionNullifiedByMember(sym, target.typeSymbol), sym.srcPos) | ||
end checkExtensionMethods | ||
|
||
def checkSynchronizedCallOnValue(tree: Tree)(using Context): Unit = | ||
if tree.symbol == defn.Object_synchronized | ||
&& ctx.owner.enclosingClass.isValueClass then | ||
report.warning(SynchronizedCallOnValue(tree), tree) | ||
|
||
/** Verify that references in the user-defined `@implicitNotFound` message are valid. | ||
* (i.e. they refer to a type variable that really occurs in the signature of the annotated symbol.) | ||
*/ | ||
|
@@ -1326,11 +1331,12 @@ class RefChecks extends MiniPhase { thisPhase => | |
|
||
override def transformIdent(tree: Ident)(using Context): Tree = | ||
checkAnyRefMethodCall(tree) | ||
checkSynchronizedCallOnValue(tree) | ||
tree | ||
|
||
override def transformSelect(tree: tpd.Select)(using Context): tpd.Tree = | ||
if defn.ScalaBoxedClasses().contains(tree.qualifier.tpe.typeSymbol) && tree.name == nme.synchronized_ then | ||
report.warning(SynchronizedCallOnBoxedClass(tree), tree.srcPos) | ||
report.warning(SynchronizedCallOnValue(tree), tree.srcPos) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably reuse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We tried the following during the Spree: //> using options -explain
class A(val s: String) extends AnyVal
def Test =
val a = A("hello")
a.synchronized { println("hello, world") } // warn And this is already throws an error:
Is this the case you have in mind? Anyway, I guess it might be nice to use the same logic both for |
||
tree | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
-- [E187] Potential Issue Warning: tests/warn/17284.scala:4:6 ---------------------------------------------------------- | ||
4 | 451.synchronized {} // warn | ||
| ^^^^^^^^^^^^^^^^ | ||
| Suspicious synchronized call on boxed class | ||
| Suspicious synchronized call on value | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| You called the synchronized method on a boxed primitive. This might not be what | ||
| you intended. | ||
| You called the synchronized method on a boxed primitive or on a class extending AnyVal. | ||
| This might not be what you intended. | ||
--------------------------------------------------------------------------------------------------------------------- | ||
-- [E187] Potential Issue Warning: tests/warn/17284.scala:8:4 ---------------------------------------------------------- | ||
8 | x.synchronized {} // warn | ||
| ^^^^^^^^^^^^^^ | ||
| Suspicious synchronized call on boxed class | ||
| Suspicious synchronized call on value | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| You called the synchronized method on a boxed primitive. This might not be what | ||
| you intended. | ||
| You called the synchronized method on a boxed primitive or on a class extending AnyVal. | ||
| This might not be what you intended. | ||
--------------------------------------------------------------------------------------------------------------------- | ||
-- [E187] Potential Issue Warning: tests/warn/17284.scala:11:7 --------------------------------------------------------- | ||
11 | true.synchronized {} // warn | ||
| ^^^^^^^^^^^^^^^^^ | ||
| Suspicious synchronized call on boxed class | ||
| Suspicious synchronized call on value | ||
|-------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| You called the synchronized method on a boxed primitive. This might not be what | ||
| you intended. | ||
| You called the synchronized method on a boxed primitive or on a class extending AnyVal. | ||
| This might not be what you intended. | ||
-------------------------------------------------------------------------------------------------------------------- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- [E187] Potential Issue Warning: tests/warn/i17493.scala:3:10 -------------------------------------------------------- | ||
3 | def g = synchronized { println("hello, world") } // warn | ||
| ^^^^^^^^^^^^ | ||
| Suspicious synchronized call on value | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| You called the synchronized method on a boxed primitive or on a class extending AnyVal. | ||
| This might not be what you intended. | ||
--------------------------------------------------------------------------------------------------------------------- |
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't do the right thing for an
import
edsynchronized
.You shouldn't look at the context owner, but at the
prefix
oftree.tpe
.