Skip to content

Commit 5c42107

Browse files
committed
Adjust rendering of error at EOF if NL
1 parent 99f7979 commit 5c42107

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,28 @@ trait MessageRendering {
243243
given Level = Level(level)
244244
given Offset = Offset(maxLineNumber.toString.length + 2)
245245
val sb = StringBuilder()
246-
val posString = posStr(pos, msg, diagnosticLevel(dia))
246+
def adjust(pos: SourcePosition): SourcePosition =
247+
if pos.span.isSynthetic
248+
&& pos.span.isZeroExtent
249+
&& pos.span.exists
250+
&& pos.span.start == pos.source.length
251+
&& pos.source(pos.span.start - 1) == '\n'
252+
then
253+
pos.withSpan(pos.span.shift(-1))
254+
else
255+
pos
256+
val adjusted = adjust(pos)
257+
val posString = posStr(adjusted, msg, diagnosticLevel(dia))
247258
if (posString.nonEmpty) sb.append(posString).append(EOL)
248259
if (pos.exists) {
249260
val pos1 = pos.nonInlined
250261
if (pos1.exists && pos1.source.file.exists) {
251-
val (srcBefore, srcAfter, offset) = sourceLines(pos1)
252-
val marker = positionMarker(pos1)
253-
val err = errorMsg(pos1, msg.message)
262+
val readjusted =
263+
if pos1 == pos then adjusted
264+
else adjust(pos1)
265+
val (srcBefore, srcAfter, offset) = sourceLines(readjusted)
266+
val marker = positionMarker(readjusted)
267+
val err = errorMsg(readjusted, msg.message)
254268
sb.append((srcBefore ::: marker :: err :: srcAfter).mkString(EOL))
255269

256270
if inlineStack.nonEmpty then

tests/neg/i23815.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
| ^
44
| ':' or '{' expected, but 'end of statement' found
55
| Nested package statements that are not at the beginning of the file require braces or ':' with an indented body.
6-
-- [E040] Syntax Error: tests/neg/i23815.scala:10:0 --------------------------------------------------------------------
7-
10 |
8-
|^
9-
|'}' expected, but eof found
6+
-- [E040] Syntax Error: tests/neg/i23815.scala:9:15 --------------------------------------------------------------------
7+
9 |// anypos-error
8+
| ^
9+
| '}' expected, but eof found

0 commit comments

Comments
 (0)