Skip to content

Commit f8c1198

Browse files
committed
Refinements
1. Indicate error after last token only if it appears on line before current token 2. Apply the same scheme to missing patterns I tried to apply it to missing types as well, but there it is not so easy. I got follow-up errors that I would not get with the existing scheme. So in summary it's not clear that the change for types would be worth it.
1 parent e598b4b commit f8c1198

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

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

Lines changed: 10 additions & 3 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
}
@@ -1456,7 +1463,7 @@ object Parsers {
14561463
case _ =>
14571464
if (isLiteral) literal()
14581465
else {
1459-
syntaxErrorOrIncomplete(IllegalStartSimpleExpr(tokenString(in.token)), in.lastOffset)
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ object messages {
922922
case class IllegalStartOfSimplePattern()(implicit ctx: Context)
923923
extends Message(IllegalStartOfSimplePatternID) {
924924
val kind: String = "Syntax"
925-
val msg: String = "Illegal start of simple pattern"
925+
val msg: String = "pattern expected"
926926
val explanation: String = {
927927
val sipCode =
928928
"""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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
object Test {
22
val x = // error: expression expected
33
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+
type T1 = // error: type expected
13+
14+
type T2 = String // error: ';' expected
15+
type T3 = this // error: Expected an additional member selection after the keyword this
16+
17+
type T4 = ; // error: type expected
18+
19+
1 match {
20+
case // error: pattern expected
21+
case 2 => ""
22+
}
423
}

0 commit comments

Comments
 (0)