Skip to content

Commit a262341

Browse files
committed
Improve error position and recovery for bad dollar
1 parent a5aaeed commit a262341

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,9 @@ object Scanners {
11681168
finishNamedToken(IDENTIFIER, target = next)
11691169
}
11701170
else
1171-
error("invalid string interpolation: `$$`, `$\"`, `$`ident or `$`BlockExpr expected")
1171+
error("invalid string interpolation: `$$`, `$\"`, `$`ident or `$`BlockExpr expected", off = charOffset - 2)
1172+
putChar('$')
1173+
getStringPart(multiLine)
11721174
}
11731175
else {
11741176
val isUnclosedLiteral = !isUnicodeEscape && (ch == SU || (!multiLine && (ch == CR || ch == LF)))

compiler/test/dotty/tools/repl/ReplTest.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ extends ReplDriver(options, new PrintStream(out, true, StandardCharsets.UTF_8.na
6363
case "" => Nil
6464
case nonEmptyLine => nonEmptyLine :: Nil
6565
}
66+
def nonBlank(line: String): Boolean = line.exists(!Character.isWhitespace(_))
6667

67-
val expectedOutput = lines.flatMap(filterEmpties)
68+
val expectedOutput = lines.filter(nonBlank)
6869
val actualOutput = {
6970
val opts = toolArgsParse(lines.take(1))
7071
val (optsLine, inputLines) = if opts.isEmpty then ("", lines) else (lines.head, lines.drop(1))
@@ -80,7 +81,7 @@ extends ReplDriver(options, new PrintStream(out, true, StandardCharsets.UTF_8.na
8081
out.linesIterator.foreach(buf.append)
8182
nstate
8283
}
83-
(optsLine :: buf.toList).flatMap(filterEmpties)
84+
(optsLine :: buf.toList).filter(nonBlank)
8485
}
8586

8687
if !FileDiff.matches(actualOutput, expectedOutput) then

tests/neg/interpolator-dollar.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/interpolator-dollar.scala:5:20 ---------------------------------------------------------------------
2+
5 | def oops = f"$s%s $<s" // error: invalid string interpolation
3+
| ^
4+
| invalid string interpolation: `$$`, `$"`, `$`ident or `$`BlockExpr expected

tests/neg/interpolator-dollar.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
trait T {
3+
def s = { println("SSS") ; "hello" }
4+
def ok = String.format("%s, %<s", s)
5+
def oops = f"$s%s $<s" // error: invalid string interpolation
6+
}

0 commit comments

Comments
 (0)