Skip to content

Commit

Permalink
Deprecate using Scala 3 hard keywords as identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
smarter committed Aug 10, 2021
1 parent 164b9c5 commit 64ea1d1
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,8 @@ self =>
def ident(skipIt: Boolean): Name = (
if (isIdent) {
val name = in.name.encode
if (in.token != BACKQUOTED_IDENT && scala3Keywords.contains(name))
deprecationWarning(in.offset, s"Wrap `$name` in backticks to use it as an identifier, it will become a keyword in Scala 3.", "2.13.7")
in.nextToken()
name
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,8 @@ trait Scanners extends ScannersCommon {

final val softModifierNames = Set(nme.open, nme.infix)

final val scala3Keywords = Set(nme.`enum`, nme.`export`, nme.`given`)

// Token representation ----------------------------------------------------

/** Returns the string representation of given token. */
Expand Down
5 changes: 5 additions & 0 deletions src/reflect/scala/reflect/internal/StdNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ trait StdNames {
// Scala 3 import syntax
val as: NameType = nameType("as")

// Scala 3 hard keywords
val `enum`: NameType = nameType("enum")
val `export`: NameType = nameType("export")
val `given`: NameType = nameType("given")

// Scala 3 soft keywords
val infix: NameType = nameType("infix")
val open: NameType = nameType("open")
Expand Down
6 changes: 3 additions & 3 deletions src/reflect/scala/reflect/io/ZipArchive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ final class FileZipArchive(file: JFile, release: Option[String]) extends ZipArch
val root = new DirEntry(RootEntry)
dirs.put(RootEntry, root)
val zipFile = openZipFile()
val enum = zipFile.entries()
val entries = zipFile.entries()

try {
while (enum.hasMoreElements) {
val zipEntry = enum.nextElement
while (entries.hasMoreElements) {
val zipEntry = entries.nextElement
if (!zipEntry.getName.startsWith("META-INF/versions/")) {
if (!zipEntry.isDirectory) {
val dir = getDir(dirs, zipEntry)
Expand Down
6 changes: 3 additions & 3 deletions src/reflect/scala/reflect/runtime/JavaMirrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ private[scala] trait JavaMirrors extends internal.SymbolTable with api.JavaUnive
object AnnotationClass { def unapply(x: jClass[_]) = x.isAnnotation }

object ConstantArg {
def enumToSymbol(enum: Enum[_]): Symbol = {
val staticPartOfEnum = classToScala(enum.getClass).companionSymbol
staticPartOfEnum.info.declaration(TermName(enum.name))
def enumToSymbol(`enum`: Enum[_]): Symbol = {
val staticPartOfEnum = classToScala(`enum`.getClass).companionSymbol
staticPartOfEnum.info.declaration(TermName(`enum`.name))
}

def unapply(schemaAndValue: (jClass[_], Any)): Option[Any] = schemaAndValue match {
Expand Down
21 changes: 21 additions & 0 deletions test/files/neg/scala3-keywords.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
scala3-keywords.scala:13: warning: Wrap `enum` in backticks to use it as an identifier, it will become a keyword in Scala 3.
val enum: Int = 1 // error
^
scala3-keywords.scala:14: warning: Wrap `export` in backticks to use it as an identifier, it will become a keyword in Scala 3.
val export: Int = 1 // error
^
scala3-keywords.scala:15: warning: Wrap `given` in backticks to use it as an identifier, it will become a keyword in Scala 3.
val given: Int = 1 // error
^
scala3-keywords.scala:16: warning: Wrap `given` in backticks to use it as an identifier, it will become a keyword in Scala 3.
def foo(given: Int) = {} // error
^
scala3-keywords.scala:17: warning: Wrap `export` in backticks to use it as an identifier, it will become a keyword in Scala 3.
def bla[export <: Int] = {} // error
^
scala3-keywords.scala:19: warning: Wrap `enum` in backticks to use it as an identifier, it will become a keyword in Scala 3.
class enum // error
^
error: No warnings can be incurred under -Werror.
6 warnings
1 error
19 changes: 19 additions & 0 deletions test/files/neg/scala3-keywords.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// scalac: -deprecation -Xfatal-warnings
//
class A {
val `enum`: Int = 1
val `export`: Int = 1
val `given`: Int = 1
def foo(`given`: Int) = {}
def bla[`export` <: Int] = {
class `enum`
}
}
class B {
val enum: Int = 1 // error
val export: Int = 1 // error
val given: Int = 1 // error
def foo(given: Int) = {} // error
def bla[export <: Int] = {} // error
}
class enum // error
4 changes: 2 additions & 2 deletions test/files/run/reflection-java-crtp/Main_2.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
object Test extends App {
import scala.reflect.runtime.universe._
val enum = typeOf[JavaSimpleEnumeration_1].baseClasses(1).asClass
val `enum` = typeOf[JavaSimpleEnumeration_1].baseClasses(1).asClass
// make sure that the E's in Enum<E extends Enum<E>> are represented by the same symbol
val e1 = enum.typeParams(0).asType
val e1 = `enum`.typeParams(0).asType
val TypeBounds(_, TypeRef(_, _, List(TypeRef(_, e2: TypeSymbol, _)))) = e1.info
println(e1, e2, e1 eq e2)
}
4 changes: 2 additions & 2 deletions test/junit/scala/collection/FactoriesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ class FactoriesTest {
im.BitSet(1, 2, 3)
)

object enum extends Enumeration {
object `enum` extends Enumeration {
val x, y, z = Value
}

val enumValues = enum.values
val enumValues = `enum`.values

sortedFactoryFromIterableOnceReturnsSameReference(SortedSet, im.SortedSet)(enumValues)

Expand Down

0 comments on commit 64ea1d1

Please sign in to comment.