Skip to content

Commit a4d4abb

Browse files
authored
Merge pull request #2453 from dotty-staging/fix-#2213
Fix #2213: Fix errors positioned after EOF
2 parents b5584b5 + 9e3518c commit a4d4abb

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) extends interfac
107107
*/
108108
def offsetToLine(offset: Int): Int = {
109109
lastLine = Util.bestFit(lineIndices, lineIndices.length, offset, lastLine)
110+
if (offset >= length) lastLine -= 1 // compensate for the sentinel
110111
lastLine
111112
}
112113

@@ -129,7 +130,7 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) extends interfac
129130
var idx = startOfLine(offset)
130131
var col = 0
131132
while (idx != offset) {
132-
col += (if (content(idx) == '\t') (tabInc - col) % tabInc else 1)
133+
col += (if (idx < length && content(idx) == '\t') (tabInc - col) % tabInc else 1)
133134
idx += 1
134135
}
135136
col

tests/repl/i2213.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
scala> def x
2+
-- [E018] Syntax Error: <console>:1:5 ------------------------------------------
3+
1 |def x
4+
| ^
5+
| missing return type
6+
7+
longer explanation available when compiling with `-explain`
8+
scala> :quit

0 commit comments

Comments
 (0)