Skip to content

Backport "Warn when @volatile is used on vals" to LTS #21051

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

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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