Skip to content

Commit 2b36ef9

Browse files
authored
Merge pull request #5693 from dotty-staging/fix-errpos
Fix error position and msg for expression expected
2 parents c1b8bd0 + 8d9b447 commit 2b36ef9

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ object Parsers {
105105
def sourcePos(off: Int = in.offset): SourcePosition =
106106
source atPos Position(off)
107107

108+
/** in.offset, except if this is at a new line, in which case `lastOffset` is preferred. */
109+
def expectedOffset: Int = {
110+
val current = sourcePos(in.offset)
111+
val last = sourcePos(in.lastOffset)
112+
if (current.line != last.line) in.lastOffset else in.offset
113+
}
114+
108115
/* ------------- ERROR HANDLING ------------------------------------------- */
109116
/** The offset where the last syntax error was reported, or if a skip to a
110117
* safepoint occurred afterwards, the offset of the safe point.
@@ -116,7 +123,7 @@ object Parsers {
116123
*/
117124
def syntaxError(msg: => Message, offset: Int = in.offset): Unit =
118125
if (offset > lastErrorOffset) {
119-
val length = if (in.name != null) in.name.show.length else 0
126+
val length = if (offset == in.offset && in.name != null) in.name.show.length else 0
120127
syntaxError(msg, Position(offset, offset + length))
121128
lastErrorOffset = in.offset
122129
}
@@ -275,13 +282,13 @@ object Parsers {
275282
/** If at end of file, issue an incompleteInputError.
276283
* Otherwise issue a syntax error and skip to next safe point.
277284
*/
278-
def syntaxErrorOrIncomplete(msg: => Message): Unit =
285+
def syntaxErrorOrIncomplete(msg: => Message, offset: Int = in.offset): Unit =
279286
if (in.token == EOF) incompleteInputError(msg)
280287
else {
281-
syntaxError(msg)
288+
syntaxError(msg, offset)
282289
skip()
283290
lastErrorOffset = in.offset
284-
} // DEBUG
291+
}
285292

286293
/** Consume one token of the specified type, or
287294
* signal an error if it is not there.
@@ -1456,7 +1463,7 @@ object Parsers {
14561463
case _ =>
14571464
if (isLiteral) literal()
14581465
else {
1459-
syntaxErrorOrIncomplete(IllegalStartSimpleExpr(tokenString(in.token)))
1466+
syntaxErrorOrIncomplete(IllegalStartSimpleExpr(tokenString(in.token)), expectedOffset)
14601467
errorTermTree
14611468
}
14621469
}
@@ -1748,7 +1755,7 @@ object Parsers {
17481755
case _ =>
17491756
if (isLiteral) literal(inPattern = true)
17501757
else {
1751-
syntaxErrorOrIncomplete(IllegalStartOfSimplePattern())
1758+
syntaxErrorOrIncomplete(IllegalStartOfSimplePattern(), expectedOffset)
17521759
errorTermTree
17531760
}
17541761
}

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -569,19 +569,9 @@ object messages {
569569
case class IllegalStartSimpleExpr(illegalToken: String)(implicit ctx: Context)
570570
extends Message(IllegalStartSimpleExprID) {
571571
val kind: String = "Syntax"
572-
val msg: String = "Illegal start of simple expression"
572+
val msg: String = "expression expected"
573573
val explanation: String = {
574-
hl"""|An expression yields a value. In the case of the simple expression, this error
575-
|commonly occurs when there's a missing parenthesis or brace. The reason being
576-
|that a simple expression is one of the following:
577-
|
578-
|- Block
579-
|- Expression in parenthesis
580-
|- Identifier
581-
|- Object creation
582-
|- Literal
583-
|
584-
|which cannot start with ${Red(illegalToken)}."""
574+
hl"""|An expression cannot start with ${Red(illegalToken)}."""
585575
}
586576
}
587577

@@ -932,7 +922,7 @@ object messages {
932922
case class IllegalStartOfSimplePattern()(implicit ctx: Context)
933923
extends Message(IllegalStartOfSimplePatternID) {
934924
val kind: String = "Syntax"
935-
val msg: String = "Illegal start of simple pattern"
925+
val msg: String = "pattern expected"
936926
val explanation: String = {
937927
val sipCode =
938928
"""def f(x: Int, y: Int) = x match {

language-server/test/dotty/tools/languageserver/DiagnosticsTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DiagnosticsTest {
2121
| Nil.map(x => x).filter(x$m1 =>$m2)
2222
|$m3}""".withSource
2323
.diagnostics(m1,
24-
(m2 to m3, "Illegal start of simple expression", Error, Some(IllegalStartSimpleExprID)),
24+
(m2 to m3, "expression expected", Error, Some(IllegalStartSimpleExprID)),
2525
(m1 to m1, """Found: Null
2626
|Required: Boolean""".stripMargin, Error, Some(TypeMismatchID))
2727
)

tests/neg/errpos.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test {
2+
val x = // error: expression expected
3+
val y = 2 // error: ';' expected
4+
5+
val z = // error: expression expected
6+
7+
// ...
8+
val a = 3 // error: ';' expected
9+
10+
val b = type // error: expression expected (on "type")
11+
12+
1 match {
13+
case // error: pattern expected
14+
case 2 => ""
15+
}
16+
}

0 commit comments

Comments
 (0)