Skip to content

Commit

Permalink
Fixes String iterpolator with \"
Browse files Browse the repository at this point in the history
Fixes scala/bug#6476

SIP-11 specification says

```
interpolatedString ::= alphaid `"` {printableChar \ (`"` | `$`) | escape} `"`
```

My reading of this is that the scanner should support the character sequence `\"`, but pass them as is. The Standard (s) interpolator would take care of the evaluation of `\"` as `"`.
This would fix the long standing issue of people trying to write `s"a=\"$a\""` and running into mysterious `';' expected but string literal found.`.

This change should be relatively safe since neither `s"\"foo\""` nor `s"foo\"` previously compiled.
  • Loading branch information
eed3si9n authored and lrytz committed Mar 8, 2021
1 parent 262f6e9 commit 47680cb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,20 @@ trait Scanners extends ScannersCommon {
setStrVal()
token = STRINGLIT
}
} else if (ch == '\\') {
if (multiLine) {
putChar(ch)
nextRawChar()
getStringPart(multiLine)
} else {
putChar(ch)
nextRawChar()
if (ch == '"') {
putChar(ch)
nextRawChar()
}
getStringPart(multiLine)
}
} else if (ch == '$') {
nextRawChar()
if (ch == '$') {
Expand Down
4 changes: 2 additions & 2 deletions test/files/neg/t8266-invalid-interp.check
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
t8266-invalid-interp.scala:4: error: Trailing '\' escapes nothing.
f"a\",
^
f"""a\""",
^
t8266-invalid-interp.scala:5: error: invalid escape '\x' not one of [\b, \t, \n, \f, \r, \\, \", \', \uxxxx] at index 1 in "a\xc". Use \\ for literal \.
f"a\xc",
^
Expand Down
2 changes: 1 addition & 1 deletion test/files/neg/t8266-invalid-interp.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

trait X {
def f = Seq(
f"a\",
f"""a\""",
f"a\xc",
// following could suggest \u000b for vertical tab, similar for \a alert
f"a\vc"
Expand Down
2 changes: 1 addition & 1 deletion test/files/pos/t11966.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
object Test {
val original = """\/ \/ /\"""
val minimal = """\1234\"""
val alternative = raw"\1234\"
val alternative = raw"""\1234\"""
}
1 change: 1 addition & 0 deletions test/files/run/string-interpolator.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Hello", Alice
6 changes: 6 additions & 0 deletions test/files/run/string-interpolator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def main(args: Array[String]): Unit = {
val person = "Alice"
println(s"\"Hello\", $person")
}
}

0 comments on commit 47680cb

Please sign in to comment.