Skip to content

Commit d00572d

Browse files
committed
Avoid crasher when first token of a program is in error
This used to give a crash in SourcePositiom, promoted by feeding its calculations with a negative offset.
1 parent 0fba875 commit d00572d

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ object Parsers {
192192
case _ =>
193193
if (mustStartStat &&
194194
in.isAfterLineEnd() &&
195-
isLeqIndented(in.offset, lastStatOffset))
195+
isLeqIndented(in.offset, lastStatOffset max 0))
196196
return
197197
}
198198
in.nextToken()

src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) {
103103
lastLine
104104
}
105105

106-
def startOfLine(offset: Int): Int = lineToOffset(offsetToLine(offset))
106+
def startOfLine(offset: Int): Int = {
107+
require(offset >= 0)
108+
lineToOffset(offsetToLine(offset))
109+
}
107110

108111
def nextLine(offset: Int): Int =
109112
lineToOffset(offsetToLine(offset) + 1 min lineIndices.length - 1)

test/dotc/tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class tests extends CompilerTest {
140140
@Test def neg_shadowedImplicits = compileFile(negDir, "arrayclone-new", xerrors = 2)
141141
@Test def neg_traitParamsTyper = compileFile(negDir, "traitParamsTyper", xerrors = 5)
142142
@Test def neg_traitParamsMixin = compileFile(negDir, "traitParamsMixin", xerrors = 2)
143+
@Test def neg_firstError = compileFile(negDir, "firstError", xerrors = 3)
143144

144145
@Test def run_all = runFiles(runDir)
145146

tests/neg/firstError.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.
2+
3+
\u890u3084eu
4+

0 commit comments

Comments
 (0)