Skip to content

Commit

Permalink
Backport "Warn when @volatile is used on vals" to LTS (#21051)
Browse files Browse the repository at this point in the history
Backports #19462 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 5, 2024
2 parents 550497c + c3749ed commit 5e54866
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case VarArgsParamCannotBeGivenID // errorNumber: 188
case ExtractorNotFoundID // errorNumber: 189
case PureUnitExpressionID // errorNumber: 190
case MatchTypeLegacyPatternID // errorNumber: 191 - unused in LTS
case UnstableInlineAccessorID // errorNumber: 192 - unused in LTS
case VolatileOnValID // errorNumber: 193

def errorNumber = ordinal - 1

Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3068,3 +3068,8 @@ class MatchTypeScrutineeCannotBeHigherKinded(tp: Type)(using Context)
extends TypeMsg(MatchTypeScrutineeCannotBeHigherKindedID) :
def msg(using Context) = i"the scrutinee of a match type cannot be higher-kinded"
def explain(using Context) = ""

class VolatileOnVal()(using Context)
extends SyntaxMsg(VolatileOnValID):
protected def msg(using Context): String = "values cannot be volatile"
protected def explain(using Context): String = ""
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,10 @@ object RefChecks {
report.error(em"private $sym cannot override ${other.showLocated}", sym.srcPos)
end checkNoPrivateOverrides

def checkVolatile(sym: Symbol)(using Context): Unit =
if sym.isVolatile && !sym.is(Mutable) then
report.warning(VolatileOnVal(), sym.srcPos)

/** Check that unary method definition do not receive parameters.
* They can only receive inferred parameters such as type parameters and implicit parameters.
*/
Expand Down Expand Up @@ -1148,6 +1152,7 @@ class RefChecks extends MiniPhase { thisPhase =>
if tree.symbol.exists then
checkNoPrivateOverrides(tree)
val sym = tree.symbol
checkVolatile(sym)
if (sym.exists && sym.owner.isTerm) {
tree.rhs match {
case Ident(nme.WILDCARD) => report.error(UnboundPlaceholderParameter(), sym.srcPos)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/io/ZipArchive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ final class FileZipArchive(jpath: JPath, release: Option[String]) extends ZipArc
override def sizeOption: Option[Int] = Some(zipEntry.getSize.toInt)
}

@volatile lazy val (root, allDirs): (DirEntry, collection.Map[String, DirEntry]) = {
lazy val (root, allDirs): (DirEntry, collection.Map[String, DirEntry]) = {
val root = new DirEntry("/", null)
val dirs = mutable.HashMap[String, DirEntry]("/" -> root)
val zipFile = openZipFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object AutoParamTupling {
* In order to get thread safety, you need to put @volatile before lazy vals.
* https://dotty.epfl.ch/docs/reference/changed-features/lazy-vals.html
*/
@volatile lazy val xs: List[String] = List("d", "o", "t", "t", "y")
lazy val xs: List[String] = List("d", "o", "t", "t", "y")

/**
* Current behaviour in Scala 2.12.2 :
Expand Down
4 changes: 4 additions & 0 deletions tests/warn/i19416.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- [E193] Syntax Warning: tests/warn/i19416.scala:2:18 -----------------------------------------------------------------
2 | @volatile val x: Int = ??? // warn
| ^
| values cannot be volatile
2 changes: 2 additions & 0 deletions tests/warn/i19416.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Foo:
@volatile val x: Int = ??? // warn

0 comments on commit 5e54866

Please sign in to comment.