Skip to content

Commit d732fc3

Browse files
committed
New format of illegal start of statement error message (+tests)
1 parent 22ff8a4 commit d732fc3

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,8 +2496,7 @@ object Parsers {
24962496
}
24972497
else if (!isStatSep && (in.token != CASE)) {
24982498
exitOnError = mustStartStat
2499-
val addendum = if (isModifier) " (no modifiers allowed here)" else ""
2500-
syntaxErrorOrIncomplete("illegal start of statement" + addendum)
2499+
syntaxErrorOrIncomplete(IllegalStartOfStatement(isModifier = isModifier))
25012500
}
25022501
acceptStatSepUnlessAtEnd(CASE)
25032502
}

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public enum ErrorMessageID {
109109
OnlyFunctionsCanBeFollowedByUnderscoreID,
110110
MissingEmptyArgumentListID,
111111
DuplicateNamedTypeParameterID,
112-
UndefinedNamedTypeParameterID
112+
UndefinedNamedTypeParameterID,
113+
IllegalStartOfStatementID
113114
;
114115

115116
public int errorNumber() {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,4 +1879,15 @@ object messages {
18791879
val msg = hl"Type parameter $undefinedName is undefined. Expected one of ${definedNames.map(_.show).mkString(", ")}."
18801880
val explanation = ""
18811881
}
1882+
1883+
case class IllegalStartOfStatement(isModifier: Boolean)(implicit ctx: Context) extends Message(IllegalStartOfStatementID) {
1884+
val kind = "Syntax"
1885+
private val addendum = if(isModifier) "(no modifiers allowed here)" else ""
1886+
val msg = hl"Illegal start of statement $addendum"
1887+
val explanation = hl"""
1888+
| Expected an import, a statement or an expression at the start of a statement.
1889+
| For a detailed list of allowed statements, please consult the syntax of BlockStat at http://dotty.epfl.ch/docs/internals/syntax.html#expressions
1890+
"""
1891+
1892+
}
18821893
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,4 +1132,24 @@ class ErrorMessagesTests extends ErrorMessagesTest {
11321132
assertEquals(tpParams, l2.map(_.show))
11331133

11341134
}
1135+
1136+
@Test def illegalStartOfStatement =
1137+
checkMessagesAfter("frontend") {
1138+
"""
1139+
|object Test {
1140+
| { ) }
1141+
| { private ) }
1142+
|}
1143+
|
1144+
""".stripMargin
1145+
}
1146+
.expect { (ictx, messages) =>
1147+
implicit val ctx: Context = ictx
1148+
1149+
assertMessageCount(2, messages)
1150+
val errWithModifier :: err :: Nil = messages
1151+
1152+
assertEquals(IllegalStartOfStatement(isModifier = false), err)
1153+
assertEquals(IllegalStartOfStatement(isModifier = true), errWithModifier)
1154+
}
11351155
}

0 commit comments

Comments
 (0)