Skip to content

Commit fb6cc9b

Browse files
committed
Test chars safely when highlighting
The arrow test at lastOffset - 3 is out-of-range when highlighting, which parses a snippet of source. Always check lower bound in testChar. Don't check as initial condition in testChars because it recurses.
1 parent 9d90ff5 commit fb6cc9b

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -732,16 +732,17 @@ object Parsers {
732732

733733
def testChar(idx: Int, p: Char => Boolean): Boolean = {
734734
val txt = source.content
735-
idx < txt.length && p(txt(idx))
735+
idx >= 0 && idx < txt.length && p(txt(idx))
736736
}
737737

738738
def testChar(idx: Int, c: Char): Boolean = {
739739
val txt = source.content
740-
idx < txt.length && txt(idx) == c
740+
idx >= 0 && idx < txt.length && txt(idx) == c
741741
}
742742

743743
def testChars(from: Int, str: String): Boolean =
744-
str.isEmpty ||
744+
str.isEmpty
745+
||
745746
testChar(from, str.head) && testChars(from + 1, str.tail)
746747

747748
def skipBlanks(idx: Int, step: Int = 1): Int =

compiler/test/dotty/tools/utils.scala

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private val toolArg = raw"(?://|/\*| \*) ?(?i:(${ToolName.values.mkString("|")})
124124
private val directiveOptionsArg = raw"//> using options (.*)".r.unanchored
125125
private val directiveJavacOptions = raw"//> using javacOpt (.*)".r.unanchored
126126
private val directiveTargetOptions = raw"//> using target.platform (jvm|scala-js)".r.unanchored
127+
private val directiveUnsupported = raw"//> using (scala) (.*)".r.unanchored
127128
private val directiveUnknown = raw"//> using (.*)".r.unanchored
128129

129130
// Inspect the lines for compiler options of the form
@@ -141,6 +142,7 @@ def toolArgsParse(lines: List[String], filename: Option[String]): List[(String,S
141142
case directiveOptionsArg(args) => List(("scalac", args))
142143
case directiveJavacOptions(args) => List(("javac", args))
143144
case directiveTargetOptions(platform) => List(("target", platform))
145+
case directiveUnsupported(name, args) => Nil
144146
case directiveUnknown(rest) => sys.error(s"Unknown directive: `//> using ${CommandLineParser.tokenize(rest).headOption.getOrElse("''")}`${filename.fold("")(f => s" in file $f")}")
145147
case _ => Nil
146148
}

tests/neg/i22906.check

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Flag -indent set repeatedly
2+
-- Error: tests/neg/i22906.scala:6:15 ----------------------------------------------------------------------------------
3+
6 | {`1`: Int => 5} // error
4+
| ^
5+
| parentheses are required around the parameter of a lambda
6+
| This construct can be rewritten automatically under -rewrite -source 3.0-migration.

tests/neg/i22906.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//> using options -rewrite -indent
2+
//> nominally using scala 3.7.0-RC1
3+
// does not reproduce under "vulpix" test rig, which enforces certain flag sets?
4+
5+
def program: Int => Int =
6+
{`1`: Int => 5} // error

0 commit comments

Comments
 (0)