Skip to content
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

Test chars safely when highlighting #22918

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ object Parsers {
def inBracesOrIndented[T](body: => T, rewriteWithColon: Boolean = false): T =
if in.token == INDENT then
val rewriteToBraces = in.rewriteNoIndent
&& !testChars(in.lastOffset - 3, " =>") // braces are always optional after `=>` so none should be inserted
&& !testCharsSafe(in.lastOffset - 3, " =>") // braces are optional after `=>` so none should be inserted
if rewriteToBraces then indentedToBraces(body)
else enclosed(INDENT, body)
else
Expand Down Expand Up @@ -744,6 +744,9 @@ object Parsers {
str.isEmpty ||
testChar(from, str.head) && testChars(from + 1, str.tail)

def testCharsSafe(from: Int, str: String): Boolean =
from >= 0 && testChars(from, str)

def skipBlanks(idx: Int, step: Int = 1): Int =
if (testChar(idx, c => c == ' ' || c == '\t' || c == Chars.CR)) skipBlanks(idx + step, step)
else idx
Expand Down Expand Up @@ -860,7 +863,7 @@ object Parsers {
case _ => false
}
var canRewrite = allBraces(in.currentRegion) && // test (1)
!testChars(in.lastOffset - 3, " =>") // test(6)
!testCharsSafe(in.lastOffset - 3, " =>") // test(6)

def isStartOfSymbolicFunction: Boolean =
opStack.headOption.exists { x =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class CompilationTests {
compileFile("tests/rewrites/ambiguous-named-tuple-assignment.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
compileFile("tests/rewrites/i21382.scala", defaultOptions.and("-indent", "-rewrite")),
compileFile("tests/rewrites/unused.scala", defaultOptions.and("-rewrite", "-Wunused:all")),
compileFile("tests/rewrites/i22440.scala", defaultOptions.and("-rewrite"))
compileFile("tests/rewrites/i22440.scala", defaultOptions.and("-rewrite")),
).checkRewrites()
}

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:5:15 ----------------------------------------------------------------------------------
5 | {`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.
5 changes: 5 additions & 0 deletions tests/neg/i22906.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//> using scala 3.7.0-RC1
//> using options -rewrite -indent

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