Skip to content

Commit 129205f

Browse files
som-snytttgodzik
authored andcommitted
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 79e4a42 commit 129205f

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

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

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

693693
def testChar(idx: Int, p: Char => Boolean): Boolean = {
694694
val txt = source.content
695-
idx < txt.length && p(txt(idx))
695+
idx >= 0 && idx < txt.length && p(txt(idx))
696696
}
697697

698698
def testChar(idx: Int, c: Char): Boolean = {
699699
val txt = source.content
700-
idx < txt.length && txt(idx) == c
700+
idx >= 0 && idx < txt.length && txt(idx) == c
701701
}
702702

703703
def testChars(from: Int, str: String): Boolean =
704-
str.isEmpty ||
704+
str.isEmpty
705+
||
705706
testChar(from, str.head) && testChars(from + 1, str.tail)
706707

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

compiler/test/dotty/tools/utils.scala

+6
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ private val toolArg = raw"(?://|/\*| \*) ?(?i:(${ToolName.values.mkString("|")})
8989
/** Directive to specify to vulpix the options to pass to Dotty */
9090
private val directiveOptionsArg = raw"//> using options (.*)".r.unanchored
9191
private val directiveJavacOptions = raw"//> using javacOpt (.*)".r.unanchored
92+
private val directiveTargetOptions = raw"//> using target.platform (jvm|scala-js)".r.unanchored
93+
private val directiveUnsupported = raw"//> using (scala) (.*)".r.unanchored
94+
private val directiveUnknown = raw"//> using (.*)".r.unanchored
9295

9396
// Inspect the lines for compiler options of the form
9497
// `//> using options args`, `// scalajs: args`, `/* scalajs: args`, ` * scalajs: args` etc.
@@ -104,6 +107,9 @@ def toolArgsParse(lines: List[String], filename: Option[String]): List[(String,S
104107
lines.flatMap {
105108
case directiveOptionsArg(args) => List(("scalac", args))
106109
case directiveJavacOptions(args) => List(("javac", args))
110+
case directiveTargetOptions(platform) => List(("target", platform))
111+
case directiveUnsupported(name, args) => Nil
112+
case directiveUnknown(rest) => sys.error(s"Unknown directive: `//> using ${CommandLineParser.tokenize(rest).headOption.getOrElse("''")}`${filename.fold("")(f => s" in file $f")}")
107113
case _ => Nil
108114
}
109115

tests/neg/i19320.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//> using scala "3.3.1"
2-
//> using dep org.http4s::http4s-ember-client:1.0.0-M40
3-
//> using dep org.http4s::http4s-ember-server:1.0.0-M40
4-
//> using dep org.http4s::http4s-dsl:1.0.0-M40
1+
//!> using scala "3.3.1"
2+
//!> using dep org.http4s::http4s-ember-client:1.0.0-M40
3+
//!> using dep org.http4s::http4s-ember-server:1.0.0-M40
4+
//!> using dep org.http4s::http4s-dsl:1.0.0-M40
55

66
//import cats.effect.*
77
//import cats.implicits.*

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)