Skip to content
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

Warn when @volatile is used on vals #19462

Merged
merged 1 commit into from
Feb 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case PureUnitExpressionID // errorNumber: 190
case MatchTypeLegacyPatternID // errorNumber: 191
case UnstableInlineAccessorID // errorNumber: 192
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 @@ -3146,3 +3146,8 @@ class UnstableInlineAccessor(accessed: Symbol, accessorTree: tpd.Tree)(using Con
if accessor.owner.name.isPackageObjectName then accessor.owner.owner.name.stripModuleClassSuffix
else accessor.owner.name.stripModuleClassSuffix
}

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 @@ -1001,6 +1001,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 @@ -1183,6 +1187,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 @@ -160,7 +160,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]) = {
nicolasstucki marked this conversation as resolved.
Show resolved Hide resolved
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
Loading