From d00572d159a3a15664afbf09d1ff914df4d2c512 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 25 Jun 2015 12:01:32 +0200 Subject: 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. --- src/dotty/tools/dotc/parsing/Parsers.scala | 2 +- src/dotty/tools/dotc/util/SourceFile.scala | 5 ++++- test/dotc/tests.scala | 1 + tests/neg/firstError.scala | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 tests/neg/firstError.scala diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala index b79077245..2a42a7fa9 100644 --- a/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/src/dotty/tools/dotc/parsing/Parsers.scala @@ -192,7 +192,7 @@ object Parsers { case _ => if (mustStartStat && in.isAfterLineEnd() && - isLeqIndented(in.offset, lastStatOffset)) + isLeqIndented(in.offset, lastStatOffset max 0)) return } in.nextToken() diff --git a/src/dotty/tools/dotc/util/SourceFile.scala b/src/dotty/tools/dotc/util/SourceFile.scala index 45119a881..6e2ac7d79 100644 --- a/src/dotty/tools/dotc/util/SourceFile.scala +++ b/src/dotty/tools/dotc/util/SourceFile.scala @@ -103,7 +103,10 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) { lastLine } - def startOfLine(offset: Int): Int = lineToOffset(offsetToLine(offset)) + def startOfLine(offset: Int): Int = { + require(offset >= 0) + lineToOffset(offsetToLine(offset)) + } def nextLine(offset: Int): Int = lineToOffset(offsetToLine(offset) + 1 min lineIndices.length - 1) diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index 1aa35e3ee..520abc222 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -140,6 +140,7 @@ class tests extends CompilerTest { @Test def neg_shadowedImplicits = compileFile(negDir, "arrayclone-new", xerrors = 2) @Test def neg_traitParamsTyper = compileFile(negDir, "traitParamsTyper", xerrors = 5) @Test def neg_traitParamsMixin = compileFile(negDir, "traitParamsMixin", xerrors = 2) + @Test def neg_firstError = compileFile(negDir, "firstError", xerrors = 3) @Test def run_all = runFiles(runDir) diff --git a/tests/neg/firstError.scala b/tests/neg/firstError.scala new file mode 100644 index 000000000..317adcced --- /dev/null +++ b/tests/neg/firstError.scala @@ -0,0 +1,4 @@ +. + +\u890u3084eu + -- cgit v1.2.3