Skip to content

New format of illegal start of statement error message (+tests) #3435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2496,8 +2496,7 @@ object Parsers {
}
else if (!isStatSep && (in.token != CASE)) {
exitOnError = mustStartStat
val addendum = if (isModifier) " (no modifiers allowed here)" else ""
syntaxErrorOrIncomplete("illegal start of statement" + addendum)
syntaxErrorOrIncomplete(IllegalStartOfStatement(isModifier))
}
acceptStatSepUnlessAtEnd(CASE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public enum ErrorMessageID {
OnlyFunctionsCanBeFollowedByUnderscoreID,
MissingEmptyArgumentListID,
DuplicateNamedTypeParameterID,
UndefinedNamedTypeParameterID
UndefinedNamedTypeParameterID,
IllegalStartOfStatementID
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1879,4 +1879,13 @@ object messages {
val msg = hl"Type parameter $undefinedName is undefined. Expected one of ${definedNames.map(_.show).mkString(", ")}."
val explanation = ""
}

case class IllegalStartOfStatement(isModifier: Boolean)(implicit ctx: Context) extends Message(IllegalStartOfStatementID) {
val kind = "Syntax"
val msg = {
val addendum = if (isModifier) ": no modifiers allowed here" else ""
"Illegal start of statement" + addendum
}
val explanation = "A statement is either an import, a definition or an expression."
}
}
19 changes: 19 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1132,4 +1132,23 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals(tpParams, l2.map(_.show))

}

@Test def illegalStartOfStatement =
checkMessagesAfter("frontend") {
"""
|object Test {
| { ) }
| { private ) }
|}
""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

assertMessageCount(2, messages)
val errWithModifier :: err :: Nil = messages

assertEquals(IllegalStartOfStatement(isModifier = false), err)
assertEquals(IllegalStartOfStatement(isModifier = true), errWithModifier)
}
}