From d732fc3073c50412f2bcadddcba9fae9232d1acc Mon Sep 17 00:00:00 2001 From: Karol Chmist Date: Fri, 3 Nov 2017 16:09:57 +0100 Subject: [PATCH 1/4] New format of illegal start of statement error message (+tests) --- .../dotty/tools/dotc/parsing/Parsers.scala | 3 +-- .../reporting/diagnostic/ErrorMessageID.java | 3 ++- .../dotc/reporting/diagnostic/messages.scala | 11 ++++++++++ .../dotc/reporting/ErrorMessagesTests.scala | 20 +++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index a871c2c8de01..fab8e60278f6 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -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 = isModifier)) } acceptStatSepUnlessAtEnd(CASE) } diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index d4920b5665a8..a01e6568df1e 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -109,7 +109,8 @@ public enum ErrorMessageID { OnlyFunctionsCanBeFollowedByUnderscoreID, MissingEmptyArgumentListID, DuplicateNamedTypeParameterID, - UndefinedNamedTypeParameterID + UndefinedNamedTypeParameterID, + IllegalStartOfStatementID ; public int errorNumber() { diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 4305f9594b68..9be3447c4d6a 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1879,4 +1879,15 @@ 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" + private val addendum = if(isModifier) "(no modifiers allowed here)" else "" + val msg = hl"Illegal start of statement $addendum" + val explanation = hl""" + | Expected an import, a statement or an expression at the start of a statement. + | For a detailed list of allowed statements, please consult the syntax of BlockStat at http://dotty.epfl.ch/docs/internals/syntax.html#expressions + """ + + } } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 1f8c65a874f7..f82e7435ae72 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -1132,4 +1132,24 @@ 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) + } } From f2a9eab897aa7c023c48f4fed6b1e967cab8f237 Mon Sep 17 00:00:00 2001 From: Karol Chmist Date: Sat, 4 Nov 2017 22:29:36 +0100 Subject: [PATCH 2/4] [CodeReview] --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 2 +- .../tools/dotc/reporting/diagnostic/messages.scala | 12 +++++------- .../tools/dotc/reporting/ErrorMessagesTests.scala | 1 - 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index fab8e60278f6..523891400e9c 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -2496,7 +2496,7 @@ object Parsers { } else if (!isStatSep && (in.token != CASE)) { exitOnError = mustStartStat - syntaxErrorOrIncomplete(IllegalStartOfStatement(isModifier = isModifier)) + syntaxErrorOrIncomplete(IllegalStartOfStatement(isModifier)) } acceptStatSepUnlessAtEnd(CASE) } diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 9be3447c4d6a..36503f0b2117 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1882,12 +1882,10 @@ object messages { case class IllegalStartOfStatement(isModifier: Boolean)(implicit ctx: Context) extends Message(IllegalStartOfStatementID) { val kind = "Syntax" - private val addendum = if(isModifier) "(no modifiers allowed here)" else "" - val msg = hl"Illegal start of statement $addendum" - val explanation = hl""" - | Expected an import, a statement or an expression at the start of a statement. - | For a detailed list of allowed statements, please consult the syntax of BlockStat at http://dotty.epfl.ch/docs/internals/syntax.html#expressions - """ - + val msg = { + val addendum = if(isModifier) "(no modifiers allowed here)" else "" + hl"Illegal start of statement $addendum" + } + val explanation = hl"A statement is either an import, a definition or an expression." } } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index f82e7435ae72..8238173aea65 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -1140,7 +1140,6 @@ class ErrorMessagesTests extends ErrorMessagesTest { | { ) } | { private ) } |} - | """.stripMargin } .expect { (ictx, messages) => From 2092a03d5ec004c9c3ea09ed5715b3037c95a9bd Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Mon, 6 Nov 2017 14:25:43 +0100 Subject: [PATCH 3/4] Polishing --- .../dotty/tools/dotc/reporting/diagnostic/messages.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 36503f0b2117..c4be3ecc3577 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1883,9 +1883,9 @@ object messages { 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 "" - hl"Illegal start of statement $addendum" + val addendum = if (isModifier) ": no modifiers allowed here" else "" + s"Illegal start of statement" + addendum } - val explanation = hl"A statement is either an import, a definition or an expression." + val explanation = "A statement is either an import, a definition or an expression." } } From 2953a4f9fb266b376df60e3367c3cab2698dc705 Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Mon, 6 Nov 2017 14:27:05 +0100 Subject: [PATCH 4/4] Polishing --- .../src/dotty/tools/dotc/reporting/diagnostic/messages.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index c4be3ecc3577..a64061609d40 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1884,7 +1884,7 @@ object messages { val kind = "Syntax" val msg = { val addendum = if (isModifier) ": no modifiers allowed here" else "" - s"Illegal start of statement" + addendum + "Illegal start of statement" + addendum } val explanation = "A statement is either an import, a definition or an expression." }