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 26, 2020
1 parent bc1cad4 commit ead814e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 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 @@ -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
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 ead814e

Please sign in to comment.