Skip to content

Commit d80887d

Browse files
committed
Always check lower bound in testChar
1 parent c4f531b commit d80887d

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

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

+6-8
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-
&& !testCharsSafe(in.lastOffset - 3, " =>") // braces are optional after `=>` so none should be inserted
644+
&& !testChars(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
@@ -732,21 +732,19 @@ 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

747-
def testCharsSafe(from: Int, str: String): Boolean =
748-
from >= 0 && testChars(from, str)
749-
750748
def skipBlanks(idx: Int, step: Int = 1): Int =
751749
if (testChar(idx, c => c == ' ' || c == '\t' || c == Chars.CR)) skipBlanks(idx + step, step)
752750
else idx
@@ -863,7 +861,7 @@ object Parsers {
863861
case _ => false
864862
}
865863
var canRewrite = allBraces(in.currentRegion) && // test (1)
866-
!testCharsSafe(in.lastOffset - 3, " =>") // test(6)
864+
!testChars(in.lastOffset - 3, " =>") // test(6)
867865

868866
def isStartOfSymbolicFunction: Boolean =
869867
opStack.headOption.exists { x =>

0 commit comments

Comments
 (0)