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)))

tests/pos/t4812.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
trait Test1 {
2-
def m1(sym: Symbol = 'TestSym): Unit
2+
def m1(sym: Symbol = Symbol("TestSym")): Unit
33
def m2(s: String = "TestString"): Unit
44
}

tests/run/arrays.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ object Test {
206206
val a2: Int = 0;
207207
val a3: Null = null;
208208
val a4: String = "a-z";
209-
val a5: Symbol = 'token;
209+
val a5: Symbol = Symbol("token");
210210
val a6: HashMap = new HashMap();
211211
val a7: TreeMap = scala.collection.immutable.TreeMap.empty[Int, Any];
212212
val a8: Strings = List("a", "z");

tests/run/fors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
object Test extends dotty.runtime.LegacyApp {
88
val xs = List(1, 2, 3)
9-
val ys = List('a, 'b, 'c)
9+
val ys = List(Symbol("a"), Symbol("b"), Symbol("c"))
1010

1111
def it = 0 until 10
1212

tests/run/t4601.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ trait B {
44
self: A =>
55

66
def test: Unit = {
7-
println('blubber)
7+
println(Symbol("blubber"))
88
}
99
}
1010

tests/run/t6632.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
object Test extends dotty.runtime.LegacyApp {
22
import collection.mutable.ListBuffer
33

4-
def newLB = ListBuffer('a, 'b, 'c, 'd, 'e)
4+
def newLB = ListBuffer(Symbol("a"), Symbol("b"), Symbol("c"), Symbol("d"), Symbol("e"))
55

66
def iiobe[A](f: => A) =
77
try { f }
88
catch { case ex: IndexOutOfBoundsException => println(ex) }
99

1010
val lb0 = newLB
11-
iiobe( lb0.insert(-1, 'x) )
11+
iiobe( lb0.insert(-1, Symbol("x")) )
1212

1313
val lb1 = newLB
14-
iiobe( lb1.insertAll(-2, Array('x, 'y, 'z)) )
14+
iiobe( lb1.insertAll(-2, Array(Symbol("x"), Symbol("y"), Symbol("z"))) )
1515

1616
val lb2 = newLB
17-
iiobe( lb2.update(-3, 'u) )
17+
iiobe( lb2.update(-3, Symbol("u")) )
1818

1919
val lb3 = newLB
20-
iiobe( lb3.updated(-1, 'u) )
21-
iiobe( lb3.updated(5, 'u) )
20+
iiobe( lb3.updated(-1, Symbol("u")) )
21+
iiobe( lb3.updated(5, Symbol("u")) )
2222
}

tests/run/t6633.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
object Test extends dotty.runtime.LegacyApp {
22
import collection.mutable.ListBuffer
33

4-
def newLB = ListBuffer('a, 'b, 'c, 'd, 'e)
4+
def newLB = ListBuffer(Symbol("a"), Symbol("b"), Symbol("c"), Symbol("d"), Symbol("e"))
55

66
val lb0 = newLB
77

88
try {
9-
lb0.insert(9, 'x)
9+
lb0.insert(9, Symbol("x"))
1010
} catch {
1111
case ex: IndexOutOfBoundsException => println(ex)
1212
}
1313

1414
val lb1 = newLB
1515

1616
try {
17-
lb1.insert(9, 'x)
17+
lb1.insert(9, Symbol("x"))
1818
} catch {
1919
case ex: IndexOutOfBoundsException =>
2020
}

tests/run/t6634.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import collection.mutable.ListBuffer
22

33
//object Test extends dotty.runtime.LegacyApp {
44
object Test extends App {
5-
def newLB = ListBuffer('a, 'b, 'c, 'd, 'e)
5+
def newLB = ListBuffer(Symbol("a"), Symbol("b"), Symbol("c"), Symbol("d"), Symbol("e"))
66

77
val lb0 = newLB
88
println("Trying lb0 ...")

tests/run/t8933b/A.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
trait MixinWithSymbol {
22
self: MotherClass =>
3-
def symbolFromTrait: Any = 'traitSymbol
3+
def symbolFromTrait: Any = Symbol("traitSymbol")
44
}

tests/run/t8933b/Test.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class MotherClass extends MixinWithSymbol {
2-
def foo = 'sym1
2+
def foo = Symbol("sym1")
33
}
44

55
object Test {

0 commit comments

Comments
 (0)