Skip to content

Commit f553e23

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.
1 parent 6bd8a0f commit f553e23

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ object Parsers {
641641
def inBracesOrIndented[T](body: => T, rewriteWithColon: Boolean = false): T =
642642
if in.token == INDENT then
643643
val rewriteToBraces = in.rewriteNoIndent
644-
&& !testChars(in.lastOffset - 3, " =>") // braces are always optional after `=>` so none should be inserted
644+
&& !testCharsSafe(in.lastOffset - 3, " =>") // braces are optional after `=>` so none should be inserted
645645
if rewriteToBraces then indentedToBraces(body)
646646
else enclosed(INDENT, body)
647647
else
@@ -744,6 +744,9 @@ object Parsers {
744744
str.isEmpty ||
745745
testChar(from, str.head) && testChars(from + 1, str.tail)
746746

747+
def testCharsSafe(from: Int, str: String): Boolean =
748+
from >= 0 && testChars(from, str)
749+
747750
def skipBlanks(idx: Int, step: Int = 1): Int =
748751
if (testChar(idx, c => c == ' ' || c == '\t' || c == Chars.CR)) skipBlanks(idx + step, step)
749752
else idx
@@ -860,7 +863,7 @@ object Parsers {
860863
case _ => false
861864
}
862865
var canRewrite = allBraces(in.currentRegion) && // test (1)
863-
!testChars(in.lastOffset - 3, " =>") // test(6)
866+
!testCharsSafe(in.lastOffset - 3, " =>") // test(6)
864867

865868
def isStartOfSymbolicFunction: Boolean =
866869
opStack.headOption.exists { x =>

compiler/test/dotty/tools/dotc/CompilationTests.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class CompilationTests {
8888
compileFile("tests/rewrites/ambiguous-named-tuple-assignment.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")),
8989
compileFile("tests/rewrites/i21382.scala", defaultOptions.and("-indent", "-rewrite")),
9090
compileFile("tests/rewrites/unused.scala", defaultOptions.and("-rewrite", "-Wunused:all")),
91-
compileFile("tests/rewrites/i22440.scala", defaultOptions.and("-rewrite"))
91+
compileFile("tests/rewrites/i22440.scala", defaultOptions.and("-rewrite")),
9292
).checkRewrites()
9393
}
9494

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:5:15 ----------------------------------------------------------------------------------
3+
5 | {`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

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//> using scala 3.7.0-RC1
2+
//> using options -rewrite -indent
3+
4+
def program: Int => Int =
5+
{`1`: Int => 5} // error

0 commit comments

Comments
 (0)