Skip to content

Commit 0b4d140

Browse files
committed
Fix error message handling
We previously got: an identifier expected but `=` found even though the token that was found was a `}`! The fallacy was that in.name is not always set to something significant. I had to refactor quite a bit to avoid this. Anyway, I thought the code was a bit underwhelming with `null` interpreted and an "explanation" that simply repeated what was said in the main message.
1 parent 82ac916 commit 0b4d140

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ object Parsers {
266266
def accept(token: Int): Int = {
267267
val offset = in.offset
268268
if (in.token != token) {
269-
syntaxErrorOrIncomplete(ExpectedTokenButFound(token, in.token, in.name))
269+
syntaxErrorOrIncomplete(ExpectedTokenButFound(token, in.token))
270270
}
271271
if (in.token == token) in.nextToken()
272272
offset
@@ -477,7 +477,7 @@ object Parsers {
477477
in.nextToken()
478478
name
479479
} else {
480-
syntaxErrorOrIncomplete(ExpectedTokenButFound(IDENTIFIER, in.token, in.name))
480+
syntaxErrorOrIncomplete(ExpectedTokenButFound(IDENTIFIER, in.token))
481481
nme.ERROR
482482
}
483483

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,17 +1055,15 @@ object messages {
10551055
|""".stripMargin
10561056
}
10571057

1058-
case class ExpectedTokenButFound(expected: Token, found: Token, foundName: TermName)(implicit ctx: Context)
1058+
case class ExpectedTokenButFound(expected: Token, found: Token)(implicit ctx: Context)
10591059
extends Message(ExpectedTokenButFoundID) {
10601060
val kind = "Syntax"
10611061

10621062
private val expectedText =
10631063
if (Tokens.isIdentifier(expected)) "an identifier"
10641064
else Tokens.showToken(expected)
10651065

1066-
private val foundText =
1067-
if (foundName != null) hl"`${foundName.show}`"
1068-
else Tokens.showToken(found)
1066+
private val foundText = Tokens.showToken(found)
10691067

10701068
val msg = hl"""${expectedText} expected, but ${foundText} found"""
10711069

@@ -1075,10 +1073,7 @@ object messages {
10751073
|If you necessarily want to use $foundText as identifier, you may put it in backticks.""".stripMargin
10761074
else
10771075
""
1078-
1079-
val explanation =
1080-
s"""|The text ${foundText} may not occur at that position, the compiler expected ${expectedText}.$ifKeyword
1081-
|""".stripMargin
1076+
val explanation = s"$ifKeyword"
10821077
}
10831078

10841079
case class MixedLeftAndRightAssociativeOps(op1: Name, op2: Name, op2LeftAssoc: Boolean)(implicit ctx: Context)

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,9 @@ class ErrorMessagesTests extends ErrorMessagesTest {
121121
val defn = ictx.definitions
122122

123123
assertMessageCount(1, messages)
124-
val ExpectedTokenButFound(expected, found, foundName) :: Nil = messages
124+
val ExpectedTokenButFound(expected, found) :: Nil = messages
125125
assertEquals(Tokens.IDENTIFIER, expected)
126126
assertEquals(Tokens.VAL, found)
127-
assertEquals("val", foundName.show)
128127
}
129128

130129
@Test def expectedToken =

0 commit comments

Comments
 (0)