Skip to content

Commit

Permalink
Improve error position and recovery for bad dollar
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jan 23, 2022
1 parent 047779a commit 2d1f602
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,9 @@ object Scanners {
finishNamedToken(IDENTIFIER, target = next)
}
else
error("invalid string interpolation: `$$`, `$\"`, `$`ident or `$`BlockExpr expected")
error("invalid string interpolation: `$$`, `$\"`, `$`ident or `$`BlockExpr expected", off = charOffset - 2)
putChar('$')
getStringPart(multiLine)
}
else {
val isUnclosedLiteral = !isUnicodeEscape && (ch == SU || (!multiLine && (ch == CR || ch == LF)))
Expand Down
5 changes: 3 additions & 2 deletions compiler/test/dotty/tools/repl/ReplTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ extends ReplDriver(options, new PrintStream(out, true, StandardCharsets.UTF_8.na
case "" => Nil
case nonEmptyLine => nonEmptyLine :: Nil
}
def nonBlank(line: String): Boolean = line.exists(!Character.isWhitespace(_))

val expectedOutput = lines.flatMap(filterEmpties)
val expectedOutput = lines.filter(nonBlank)
val actualOutput = {
val opts = toolArgsParse(lines.take(1))
val (optsLine, inputLines) = if opts.isEmpty then ("", lines) else (lines.head, lines.drop(1))
Expand All @@ -78,7 +79,7 @@ extends ReplDriver(options, new PrintStream(out, true, StandardCharsets.UTF_8.na
out.linesIterator.foreach(buf.append)
nstate
}
(optsLine :: buf.toList).flatMap(filterEmpties)
(optsLine :: buf.toList).filter(nonBlank)
}

if !FileDiff.matches(actualOutput, expectedOutput) then
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/interpolator-dollar.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/interpolator-dollar.scala:5:20 ---------------------------------------------------------------------
5 | def oops = f"$s%s $<s" // error: invalid string interpolation
| ^
| invalid string interpolation: `$$`, `$"`, `$`ident or `$`BlockExpr expected
6 changes: 6 additions & 0 deletions tests/neg/interpolator-dollar.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

trait T {
def s = { println("SSS") ; "hello" }
def ok = String.format("%s, %<s", s)
def oops = f"$s%s $<s" // error: invalid string interpolation
}

0 comments on commit 2d1f602

Please sign in to comment.