Skip to content

Commit 9f0003c

Browse files
AnotherMedombovel
authored andcommitted
Warn when @volatile is used on vals
Co-authored-by: Mehdi Alaoui <[email protected]> Co-authored-by: Matt Bovel <[email protected]>
1 parent 2945fd1 commit 9f0003c

File tree

7 files changed

+19
-2
lines changed

7 files changed

+19
-2
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
206206
case PureUnitExpressionID // errorNumber: 190
207207
case MatchTypeLegacyPatternID // errorNumber: 191
208208
case UnstableInlineAccessorID // errorNumber: 192
209+
case VolatileOnValID // errorNumber: 193
209210

210211
def errorNumber = ordinal - 1
211212

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

+5
Original file line numberDiff line numberDiff line change
@@ -3146,3 +3146,8 @@ class UnstableInlineAccessor(accessed: Symbol, accessorTree: tpd.Tree)(using Con
31463146
if accessor.owner.name.isPackageObjectName then accessor.owner.owner.name.stripModuleClassSuffix
31473147
else accessor.owner.name.stripModuleClassSuffix
31483148
}
3149+
3150+
class VolatileOnVal()(using Context)
3151+
extends SyntaxMsg(VolatileOnValID):
3152+
protected def msg(using Context): String = "values cannot be volatile"
3153+
protected def explain(using Context): String = ""

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

+5
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,10 @@ object RefChecks {
10011001
report.error(em"private $sym cannot override ${other.showLocated}", sym.srcPos)
10021002
end checkNoPrivateOverrides
10031003

1004+
def checkVolatile(sym: Symbol)(using Context): Unit =
1005+
if sym.isVolatile && !sym.is(Mutable) then
1006+
report.warning(VolatileOnVal(), sym.srcPos)
1007+
10041008
/** Check that unary method definition do not receive parameters.
10051009
* They can only receive inferred parameters such as type parameters and implicit parameters.
10061010
*/
@@ -1183,6 +1187,7 @@ class RefChecks extends MiniPhase { thisPhase =>
11831187
if tree.symbol.exists then
11841188
checkNoPrivateOverrides(tree)
11851189
val sym = tree.symbol
1190+
checkVolatile(sym)
11861191
if (sym.exists && sym.owner.isTerm) {
11871192
tree.rhs match {
11881193
case Ident(nme.WILDCARD) => report.error(UnboundPlaceholderParameter(), sym.srcPos)

compiler/src/dotty/tools/io/ZipArchive.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ final class FileZipArchive(jpath: JPath, release: Option[String]) extends ZipArc
160160
override def sizeOption: Option[Int] = Some(zipEntry.getSize.toInt)
161161
}
162162

163-
@volatile lazy val (root, allDirs): (DirEntry, collection.Map[String, DirEntry]) = {
163+
lazy val (root, allDirs): (DirEntry, collection.Map[String, DirEntry]) = {
164164
val root = new DirEntry("/", null)
165165
val dirs = mutable.HashMap[String, DirEntry]("/" -> root)
166166
val zipFile = openZipFile()

sbt-test/sbt-dotty/scaladoc/src/main/scala/AutoParamTupling.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object AutoParamTupling {
1111
* In order to get thread safety, you need to put @volatile before lazy vals.
1212
* https://dotty.epfl.ch/docs/reference/changed-features/lazy-vals.html
1313
*/
14-
@volatile lazy val xs: List[String] = List("d", "o", "t", "t", "y")
14+
lazy val xs: List[String] = List("d", "o", "t", "t", "y")
1515

1616
/**
1717
* Current behaviour in Scala 2.12.2 :

tests/warn/i19416.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- [E193] Syntax Warning: tests/warn/i19416.scala:2:18 -----------------------------------------------------------------
2+
2 | @volatile val x: Int = ??? // warn
3+
| ^
4+
| values cannot be volatile

tests/warn/i19416.scala

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Foo:
2+
@volatile val x: Int = ??? // warn

0 commit comments

Comments
 (0)