Skip to content

Test chars safely when highlighting #22918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -732,16 +732,17 @@ object Parsers {

def testChar(idx: Int, p: Char => Boolean): Boolean = {
val txt = source.content
idx < txt.length && p(txt(idx))
idx >= 0 && idx < txt.length && p(txt(idx))
}

def testChar(idx: Int, c: Char): Boolean = {
val txt = source.content
idx < txt.length && txt(idx) == c
idx >= 0 && idx < txt.length && txt(idx) == c
}

def testChars(from: Int, str: String): Boolean =
str.isEmpty ||
str.isEmpty
||
testChar(from, str.head) && testChars(from + 1, str.tail)

def skipBlanks(idx: Int, step: Int = 1): Int =
Expand Down
2 changes: 2 additions & 0 deletions compiler/test/dotty/tools/utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private val toolArg = raw"(?://|/\*| \*) ?(?i:(${ToolName.values.mkString("|")})
private val directiveOptionsArg = raw"//> using options (.*)".r.unanchored
private val directiveJavacOptions = raw"//> using javacOpt (.*)".r.unanchored
private val directiveTargetOptions = raw"//> using target.platform (jvm|scala-js)".r.unanchored
private val directiveUnsupported = raw"//> using (scala) (.*)".r.unanchored
private val directiveUnknown = raw"//> using (.*)".r.unanchored

// Inspect the lines for compiler options of the form
Expand All @@ -141,6 +142,7 @@ def toolArgsParse(lines: List[String], filename: Option[String]): List[(String,S
case directiveOptionsArg(args) => List(("scalac", args))
case directiveJavacOptions(args) => List(("javac", args))
case directiveTargetOptions(platform) => List(("target", platform))
case directiveUnsupported(name, args) => Nil
case directiveUnknown(rest) => sys.error(s"Unknown directive: `//> using ${CommandLineParser.tokenize(rest).headOption.getOrElse("''")}`${filename.fold("")(f => s" in file $f")}")
case _ => Nil
}
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i22906.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flag -indent set repeatedly
-- Error: tests/neg/i22906.scala:6:15 ----------------------------------------------------------------------------------
6 | {`1`: Int => 5} // error
| ^
| parentheses are required around the parameter of a lambda
| This construct can be rewritten automatically under -rewrite -source 3.0-migration.
6 changes: 6 additions & 0 deletions tests/neg/i22906.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//> using options -rewrite -indent
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nominally a no-op without -source 3.0-migration but would throw; it requires the other options to demonstrate the behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to document my comment that vulpix doesn't demonstrate the error.

//> nominally using scala 3.7.0-RC1
// does not reproduce under "vulpix" test rig, which enforces certain flag sets?

def program: Int => Int =
{`1`: Int => 5} // error
Loading