Skip to content

Commit 5e54866

Browse files
Backport "Warn when @volatile is used on vals" to LTS (#21051)
Backports #19462 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 550497c + c3749ed commit 5e54866

File tree

7 files changed

+21
-2
lines changed

7 files changed

+21
-2
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
204204
case VarArgsParamCannotBeGivenID // errorNumber: 188
205205
case ExtractorNotFoundID // errorNumber: 189
206206
case PureUnitExpressionID // errorNumber: 190
207+
case MatchTypeLegacyPatternID // errorNumber: 191 - unused in LTS
208+
case UnstableInlineAccessorID // errorNumber: 192 - unused in LTS
209+
case VolatileOnValID // errorNumber: 193
207210

208211
def errorNumber = ordinal - 1
209212

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

+5
Original file line numberDiff line numberDiff line change
@@ -3068,3 +3068,8 @@ class MatchTypeScrutineeCannotBeHigherKinded(tp: Type)(using Context)
30683068
extends TypeMsg(MatchTypeScrutineeCannotBeHigherKindedID) :
30693069
def msg(using Context) = i"the scrutinee of a match type cannot be higher-kinded"
30703070
def explain(using Context) = ""
3071+
3072+
class VolatileOnVal()(using Context)
3073+
extends SyntaxMsg(VolatileOnValID):
3074+
protected def msg(using Context): String = "values cannot be volatile"
3075+
protected def explain(using Context): String = ""

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

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

969+
def checkVolatile(sym: Symbol)(using Context): Unit =
970+
if sym.isVolatile && !sym.is(Mutable) then
971+
report.warning(VolatileOnVal(), sym.srcPos)
972+
969973
/** Check that unary method definition do not receive parameters.
970974
* They can only receive inferred parameters such as type parameters and implicit parameters.
971975
*/
@@ -1148,6 +1152,7 @@ class RefChecks extends MiniPhase { thisPhase =>
11481152
if tree.symbol.exists then
11491153
checkNoPrivateOverrides(tree)
11501154
val sym = tree.symbol
1155+
checkVolatile(sym)
11511156
if (sym.exists && sym.owner.isTerm) {
11521157
tree.rhs match {
11531158
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
@@ -159,7 +159,7 @@ final class FileZipArchive(jpath: JPath, release: Option[String]) extends ZipArc
159159
override def sizeOption: Option[Int] = Some(zipEntry.getSize.toInt)
160160
}
161161

162-
@volatile lazy val (root, allDirs): (DirEntry, collection.Map[String, DirEntry]) = {
162+
lazy val (root, allDirs): (DirEntry, collection.Map[String, DirEntry]) = {
163163
val root = new DirEntry("/", null)
164164
val dirs = mutable.HashMap[String, DirEntry]("/" -> root)
165165
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)