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 committed Mar 27, 2020
1 parent bc1cad4 commit 3e100a5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
Expand Up @@ -907,6 +907,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
12 changes: 3 additions & 9 deletions test/files/neg/t8266-invalid-interp.check
@@ -1,10 +1,4 @@
t8266-invalid-interp.scala:4: error: Trailing '\' escapes nothing.
t8266-invalid-interp.scala:4: error: unclosed string literal
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",
^
t8266-invalid-interp.scala:7: error: invalid escape '\v' not one of [\b, \t, \n, \f, \r, \\, \", \', \uxxxx] at index 1 in "a\vc". Use \\ for literal \.
f"a\vc"
^
3 errors
^
1 error
1 change: 1 addition & 0 deletions test/files/run/string-interpolator.check
@@ -0,0 +1 @@
"Hello", Alice
6 changes: 6 additions & 0 deletions test/files/run/string-interpolator.scala
@@ -0,0 +1,6 @@
object Test {
def main(args: Array[String]): Unit = {
val person = "Alice"
println(s"\"Hello\", $person")
}
}

0 comments on commit 3e100a5

Please sign in to comment.