Skip to content

Commit 29a6179

Browse files
authored
Merge pull request #5681 from dotty-staging/drop-symlits
Drop symbol literals
2 parents 3d7d15f + 927cbc7 commit 29a6179

File tree

19 files changed

+65
-47
lines changed

19 files changed

+65
-47
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,15 @@ object Parsers {
659659
}
660660
val isNegated = negOffset < in.offset
661661
atPos(negOffset) {
662-
if (in.token == SYMBOLLIT) atPos(in.skipToken()) { SymbolLit(in.strVal) }
662+
if (in.token == SYMBOLLIT) {
663+
migrationWarningOrError(em"""symbol literal '${in.name} is no longer supported,
664+
|use a string literal "${in.name}" or an application Symbol("${in.name}") instead.""")
665+
if (in.isScala2Mode) {
666+
patch(source, Position(in.offset, in.offset + 1), "Symbol(\"")
667+
patch(source, Position(in.charOffset - 1), "\")")
668+
}
669+
atPos(in.skipToken()) { SymbolLit(in.strVal) }
670+
}
663671
else if (in.token == INTERPOLATIONID) interpolatedString(inPattern)
664672
else finish(in.token match {
665673
case CHARLIT => in.charVal

compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Positions.Position
66
import core.Contexts.Context
77
import collection.mutable
88
import scala.annotation.tailrec
9+
import dotty.tools.dotc.reporting.Reporter
910

1011
/** Handles rewriting of Scala2 files to Dotty */
1112
object Rewrites {
@@ -51,9 +52,9 @@ object Rewrites {
5152
}
5253

5354
def writeBack(): Unit = {
54-
val out = source.file.output
5555
val chars = apply(source.underlying.content)
5656
val bytes = new String(chars).getBytes
57+
val out = source.file.output
5758
out.write(bytes)
5859
out.close()
5960
}
@@ -63,10 +64,11 @@ object Rewrites {
6364
* given by `pos` in `source` by `replacement`
6465
*/
6566
def patch(source: SourceFile, pos: Position, replacement: String)(implicit ctx: Context): Unit =
66-
for (rewrites <- ctx.settings.rewrite.value)
67-
rewrites.patched
68-
.getOrElseUpdate(source, new Patches(source))
69-
.addPatch(pos, replacement)
67+
if (ctx.reporter != Reporter.NoReporter) // NoReporter is used for syntax highlighting
68+
for (rewrites <- ctx.settings.rewrite.value)
69+
rewrites.patched
70+
.getOrElseUpdate(source, new Patches(source))
71+
.addPatch(pos, replacement)
7072

7173
/** Patch position in `ctx.compilationUnit.source`. */
7274
def patch(pos: Position, replacement: String)(implicit ctx: Context): Unit =
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: doc-page
3+
title: Dropped: Symbol Literals
4+
---
5+
6+
Symbol literals are no longer supported. The `scala.Symbol` class still exists, so a
7+
literal translation of the symbol literal `'xyz` is `Symbol("xyz")`. However, it is recommended to use a plain string literal `"xyz"` instead. (The `Symbol` class will be deprecated and removed in the future).

docs/sidebar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ sidebar:
109109
url: docs/reference/dropped-features/limit22.html
110110
- title: XML literals
111111
url: docs/reference/dropped-features/xml.html
112+
- title: Symbol Literals
113+
url: docs/reference/dropped-features/symlits.html
112114
- title: Auto-Application
113115
url: docs/reference/dropped-features/auto-apply.html
114116
- title: Weak Conformance
File renamed without changes.

tests/neg/sip23-symbols.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
object Test {
2-
val sym0 = 's
2+
val sym0 = 's // error: no longer supported
33
//sym0: Symbol
4-
sym0: 's // error
4+
sym0: 's // error // error: no longer supported
55

66
//val sym1: 's = 's
77
//sym1: Symbol
@@ -15,9 +15,9 @@ object Test {
1515
type Identity[T] = T
1616
def narrow[T <: Singleton](t: T): Identity[T] = t
1717

18-
final val sym3 = id('s)
18+
final val sym3 = id('s) // error: no longer supported
1919
//sym3: Symbol
20-
sym3: 's // error
20+
sym3: 's // error // error: no longer supported
2121

2222
//val sym4 = narrow('s)
2323
//sym4: Symbol

tests/pos/desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ object desugar {
7070
}
7171

7272
object misc {
73-
'hello
73+
Symbol("hello")
7474
s"this is a $x + ${x + y} string"
7575
type ~[X, Y] = Tuple2[X, Y]
7676
val pair: Int ~ String = 1 -> "abc"

tests/pos/literals.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ object Test {
55
val d: 1.0 = 1.0
66
val e: true = true
77
val f: '*' = '*'
8-
val g: 'a = 'a
98
}

tests/pos/t389.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
object Test {
2-
def a = 'a
3-
def b = 'B
4-
def c = '+
2+
def a = Symbol("a")
3+
def b = Symbol("B")
4+
def c = Symbol("+")
55
//def d = '`\n` //error: unclosed character literal
6-
def e = '\u0041
6+
def e = Symbol("\u0041")
77
}

tests/pos/t4579.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,17 @@ object LispAny extends Lisp {
303303
def asBoolean(x: Data): Boolean = x != 0
304304

305305
def normalize(x: Data): Data = x match {
306-
case 'and :: x :: y :: Nil =>
307-
normalize('if :: x :: y :: 0 :: Nil)
308-
case 'or :: x :: y :: Nil =>
309-
normalize('if :: x :: 1 :: y :: Nil)
310-
case 'def :: (name :: args) :: body :: expr :: Nil =>
311-
normalize('def :: name :: ('lambda :: args :: body :: Nil) :: expr :: Nil)
312-
case 'cond :: ('else :: expr :: Nil) :: rest =>
306+
case Symbol("and") :: x :: y :: Nil =>
307+
normalize(Symbol("if") :: x :: y :: 0 :: Nil)
308+
case Symbol("or") :: x :: y :: Nil =>
309+
normalize(Symbol("if") :: x :: 1 :: y :: Nil)
310+
case Symbol("def") :: (name :: args) :: body :: expr :: Nil =>
311+
normalize(Symbol("def") :: name :: (Symbol("lambda") :: args :: body :: Nil) :: expr :: Nil)
312+
case Symbol("cond") :: (Symbol("else") :: expr :: Nil) :: rest =>
313313
normalize(expr);
314-
case 'cond :: (test :: expr :: Nil) :: rest =>
315-
normalize('if :: test :: expr :: ('cond :: rest) :: Nil)
316-
case 'cond :: 'else :: expr :: Nil =>
314+
case Symbol("cond") :: (test :: expr :: Nil) :: rest =>
315+
normalize(Symbol("if") :: test :: expr :: (Symbol("cond") :: rest) :: Nil)
316+
case Symbol("cond") :: Symbol("else") :: expr :: Nil =>
317317
normalize(expr)
318318
case h :: t =>
319319
normalize(h) :: asList(normalize(t))
@@ -342,15 +342,15 @@ object LispAny extends Lisp {
342342
def eval1(x: Data, env: Environment): Data = x match {
343343
case Symbol(name) =>
344344
env lookup name
345-
case 'def :: Symbol(name) :: y :: z :: Nil =>
345+
case Symbol("def") :: Symbol(name) :: y :: z :: Nil =>
346346
eval(z, env.extendRec(name, (env1 => eval(y, env1))))
347-
case 'val :: Symbol(name) :: y :: z :: Nil =>
347+
case Symbol("val") :: Symbol(name) :: y :: z :: Nil =>
348348
eval(z, env.extend(name, eval(y, env)))
349-
case 'lambda :: params :: y :: Nil =>
349+
case Symbol("lambda") :: params :: y :: Nil =>
350350
mkLambda(params, y, env)
351-
case 'if :: c :: y :: z :: Nil =>
351+
case Symbol("if") :: c :: y :: z :: Nil =>
352352
if (asBoolean(eval(c, env))) eval(y, env) else eval(z, env)
353-
case 'quote :: y :: Nil =>
353+
case Symbol("quote") :: y :: Nil =>
354354
y
355355
case y :: z =>
356356
apply(eval(y, env), z map (x => eval(x, env)))

0 commit comments

Comments
 (0)