Skip to content

Commit

Permalink
Allow \" in single-quoted string interpolations
Browse files Browse the repository at this point in the history
Changing `"Hello, \"World\""` to `s"Hello, \"$who\""` no longer breaks.

Before this change, `\"` terminated single-quoted interpolated string
literals, now the string remains open. The scanner doesn't interpret
the escape sequence, string interpolators can do so (`s` and `f` do).

Breaking changes:
  - `raw"c:\"` no longer compiles, it's now an unclosed string
  - `raw"c:\" // uh"` used to evaluate to `"""c:\""", now it's
    """c:\" // uh"""
  • Loading branch information
eed3si9n authored and lrytz committed Mar 17, 2021
1 parent ac73ea3 commit 081c0fe
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 11 deletions.
30 changes: 23 additions & 7 deletions src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,12 @@ trait Scanners extends ScannersCommon {
} else unclosedStringLit()
}

private def unclosedStringLit(): Unit = syntaxError("unclosed string literal")
private def unclosedStringLit(seenEscapedQuoteInInterpolation: Boolean = false): Unit = {
val note =
if (seenEscapedQuoteInInterpolation) "; note that `\\\"` no longer closes single-quoted interpolated string literals since 2.13.6, you can use a triple-quoted string instead"
else ""
syntaxError(s"unclosed string literal$note")
}

private def replaceUnicodeEscapesInTriple(): Unit =
if(strVal != null) {
Expand Down Expand Up @@ -890,7 +895,8 @@ trait Scanners extends ScannersCommon {
}
}

@tailrec private def getStringPart(multiLine: Boolean): Unit = {
// for interpolated strings
@tailrec private def getStringPart(multiLine: Boolean, seenEscapedQuote: Boolean = false): Unit = {
def finishStringPart() = {
setStrVal()
token = STRINGPART
Expand All @@ -904,18 +910,27 @@ trait Scanners extends ScannersCommon {
setStrVal()
token = STRINGLIT
} else
getStringPart(multiLine)
getStringPart(multiLine, seenEscapedQuote)
} else {
nextChar()
setStrVal()
token = STRINGLIT
}
} else if (ch == '\\' && !multiLine) {
putChar(ch)
nextRawChar()
val q = ch == '"'
if (q || ch == '\\') {
putChar(ch)
nextRawChar()
}
getStringPart(multiLine, seenEscapedQuote || q)
} else if (ch == '$') {
nextRawChar()
if (ch == '$' || ch == '"') {
putChar(ch)
nextRawChar()
getStringPart(multiLine)
getStringPart(multiLine, seenEscapedQuote)
} else if (ch == '{') {
finishStringPart()
nextRawChar()
Expand Down Expand Up @@ -946,13 +961,14 @@ trait Scanners extends ScannersCommon {
if (isUnclosedLiteral) {
if (multiLine)
incompleteInputError("unclosed multi-line string literal")
else
unclosedStringLit()
else {
unclosedStringLit(seenEscapedQuote)
}
}
else {
putChar(ch)
nextRawChar()
getStringPart(multiLine)
getStringPart(multiLine, seenEscapedQuote)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/files/neg/t6476.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
t6476.scala:8: error: unclosed string literal; note that `\"` no longer closes single-quoted interpolated string literals since 2.13.6, you can use a triple-quoted string instead
mimi"\"
^
1 error
9 changes: 9 additions & 0 deletions test/files/neg/t6476.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// only the last one doesn't parse
class C {
mimi"""\ """
mimi"""\\"""
mimi"""\"""
mimi"\ "
mimi"\\"
mimi"\"
}
7 changes: 7 additions & 0 deletions test/files/neg/t6476b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
t6476b.scala:2: error: invalid escape at terminal index 0 in "\". Use \\ for literal \.
val sa = s"""\"""
^
t6476b.scala:4: error: invalid escape '\ ' not one of [\b, \t, \n, \f, \r, \\, \", \', \uxxxx] at index 0 in "\ ". Use \\ for literal \.
val sc = s"""\ """
^
2 errors
8 changes: 8 additions & 0 deletions test/files/neg/t6476b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class C {
val sa = s"""\"""
val sb = s"""\\"""
val sc = s"""\ """
val ra = raw"""\"""
val rb = raw"""\\"""
val rc = raw"""\ """
}
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\"""
}
12 changes: 12 additions & 0 deletions test/files/run/interpolation-repl.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

scala> raw"\""
val res0: String = \"

scala> raw"\" // this used to be a comment, but after scala/pull#8830 it's part of the string! "
val res1: String = "\" // this used to be a comment, but after scala/pull#8830 it's part of the string! "

scala> raw"\" // this used to compile, now it's unclosed
^
error: unclosed string literal; note that `\"` no longer closes single-quoted interpolated string literals since 2.13.6, you can use a triple-quoted string instead

scala> :quit
9 changes: 9 additions & 0 deletions test/files/run/interpolation-repl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.tools.partest.ReplTest

object Test extends ReplTest {
def code = """
raw"\""
raw"\" // this used to be a comment, but after scala/pull#8830 it's part of the string! "
raw"\" // this used to compile, now it's unclosed
"""
}
13 changes: 13 additions & 0 deletions test/files/run/t6476.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"Hello", Alice
"Hello", Alice
"Hello", Alice
"Hello", Alice
\"Hello\", Alice
\"Hello\", Alice
\TILT\
\TILT\
\\TILT\\
\TILT\
\TILT\
\\TILT\\
\TILT\
23 changes: 23 additions & 0 deletions test/files/run/t6476.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
object Test {
def main(args: Array[String]): Unit = {
val person = "Alice"
println(s"\"Hello\", $person")
println(s"""\"Hello\", $person""")

println(f"\"Hello\", $person")
println(f"""\"Hello\", $person""")

println(raw"\"Hello\", $person")
println(raw"""\"Hello\", $person""")

println(s"\\TILT\\")
println(f"\\TILT\\")
println(raw"\\TILT\\")

println(s"""\\TILT\\""")
println(f"""\\TILT\\""")
println(raw"""\\TILT\\""")

println(raw"""\TILT\""")
}
}

0 comments on commit 081c0fe

Please sign in to comment.