Skip to content

Commit a3adc99

Browse files
hamzaremmalAnotherMedombovel
authored
Warn when @volatile is used on vals (#19462)
Fixes #19416 Co-authored-by: Mehdi Alaoui <[email protected]> Co-authored-by: Matt Bovel <[email protected]>
2 parents 5e1f546 + 9f0003c commit a3adc99

File tree

7 files changed

+19
-2
lines changed

7 files changed

+19
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,3 +3150,8 @@ class UnstableInlineAccessor(accessed: Symbol, accessorTree: tpd.Tree)(using Con
31503150
if accessor.owner.name.isPackageObjectName then accessor.owner.owner.name.stripModuleClassSuffix
31513151
else accessor.owner.name.stripModuleClassSuffix
31523152
}
3153+
3154+
class VolatileOnVal()(using Context)
3155+
extends SyntaxMsg(VolatileOnValID):
3156+
protected def msg(using Context): String = "values cannot be volatile"
3157+
protected def explain(using Context): String = ""

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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)