Skip to content

Commit 0214700

Browse files
committed
Tweak error reporting for illegal start of statement
1 parent 1100d43 commit 0214700

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ object Parsers {
311311
def acceptStatSep(): Unit =
312312
if in.isNewLine then in.nextToken() else accept(SEMI)
313313

314-
def exitStats[T <: Tree](stats: ListBuffer[T], altEnd: Token = EOF, noPrevStat: Boolean): Boolean =
314+
def exitStats[T <: Tree](stats: ListBuffer[T], noPrevStat: Boolean, altEnd: Token = EOF, what: String = "statement"): Boolean =
315315
def recur(sepSeen: Boolean, endSeen: Boolean): Boolean =
316316
if isStatSep then
317317
in.nextToken()
@@ -326,9 +326,10 @@ object Parsers {
326326
false
327327
else
328328
val found = in.token
329+
val statFollows = mustStartStatTokens.contains(found)
329330
syntaxError(
330-
if noPrevStat then IllegalStartOfStatement(isModifier)
331-
else i"end of statement expected but ${showToken(found)} found")
331+
if noPrevStat then IllegalStartOfStatement(what, isModifier, statFollows)
332+
else i"end of $what expected but ${showToken(found)} found")
332333
if mustStartStatTokens.contains(found) then
333334
true // it's a statement that might be legal in an outer context
334335
else
@@ -3933,7 +3934,7 @@ object Parsers {
39333934
stats +++= localDef(in.offset)
39343935
else
39353936
empty = true
3936-
!exitStats(stats, CASE, empty)
3937+
!exitStats(stats, empty, CASE)
39373938
do ()
39383939
stats.toList
39393940
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,12 +1824,16 @@ import transform.SymUtils._
18241824
def explain = ""
18251825
}
18261826

1827-
class IllegalStartOfStatement(isModifier: Boolean)(using Context) extends SyntaxMsg(IllegalStartOfStatementID) {
1828-
def msg = {
1829-
val addendum = if (isModifier) ": this modifier is not allowed here" else ""
1830-
"Illegal start of statement" + addendum
1831-
}
1832-
def explain = "A statement is either an import, a definition or an expression."
1827+
class IllegalStartOfStatement(what: String, isModifier: Boolean, isStat: Boolean)(using Context) extends SyntaxMsg(IllegalStartOfStatementID) {
1828+
def msg =
1829+
if isStat then
1830+
"this kind of statement is not allowed here"
1831+
else
1832+
val addendum = if isModifier then ": this modifier is not allowed here" else ""
1833+
s"Illegal start of $what$addendum"
1834+
def explain =
1835+
i"""A statement is an import or export, a definition or an expression.
1836+
|Some statements are only allowed in certain contexts"""
18331837
}
18341838

18351839
class TraitIsExpected(symbol: Symbol)(using Context) extends SyntaxMsg(TraitIsExpectedID) {

0 commit comments

Comments
 (0)