From 78ade4cc785c6508153898a7d632f9dff78d59e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Richez?= Date: Fri, 3 Nov 2017 17:10:53 +0100 Subject: [PATCH 1/3] new format for package name already defined error (+tests) --- .../dotc/reporting/diagnostic/ErrorMessageID.java | 1 + .../tools/dotc/reporting/diagnostic/messages.scala | 8 ++++++++ compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 +- .../tools/dotc/reporting/ErrorMessagesTests.scala | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index b7ad3692185e..bc3bec0e3bcb 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -113,6 +113,7 @@ public enum ErrorMessageID { IllegalStartOfStatementID, TraitIsExpectedID, TraitRedefinedFinalMethodFromAnyRefID, + PackageNameAlreadyDefinedID, ; 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 be31e609fd2c..e250be82004c 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1922,4 +1922,12 @@ object messages { val msg = hl"Traits cannot redefine final $method from ${"class AnyRef"}." val explanation = "" } + + case class PackageNameAlreadyDefined(pkg: Symbol)(implicit ctx: Context) extends Message(PackageNameAlreadyDefinedID) { + + override def msg: String = hl"${pkg} is already defined, cannot be a package" + override def kind: String = "Syntax" + override def explanation: String = + "An object cannot have the same name as an existing package. Rename ${pkg} or the package with the same name." + } } diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index d7c8daee2cd6..983dfcc48e19 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -1522,7 +1522,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit val packageContext = if (pkg is Package) ctx.fresh.setOwner(pkg.moduleClass).setTree(tree) else { - ctx.error(em"$pkg is already defined, cannot be a package", tree.pos) + ctx.error(PackageNameAlreadyDefined(pkg), tree.pos) ctx } val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageContext) diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 4282e5aa367d..7c930ef93a3d 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -1189,4 +1189,18 @@ class ErrorMessagesTests extends ErrorMessagesTest { assertEquals("method wait", method.show) } + + @Test def packageNameAlreadyDefined = + checkMessagesAfter("frontend") { + """ + |package bar { } + |object bar { } + | + """.stripMargin + }.expect { (ictx, messages) => + implicit val ctx: Context = ictx + + assertMessageCount(1, messages) + assert(messages.head.isInstanceOf[PackageNameAlreadyDefined]) + } } From a74feb6620af88bba37d80f17b196d9c7140ec45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Richez?= Date: Wed, 15 Nov 2017 13:58:50 +0100 Subject: [PATCH 2/3] code review : removes unnecessary override/types and improves test --- .../dotty/tools/dotc/reporting/diagnostic/messages.scala | 8 ++++---- .../dotty/tools/dotc/reporting/ErrorMessagesTests.scala | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index e250be82004c..8065b57b8880 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1925,9 +1925,9 @@ object messages { case class PackageNameAlreadyDefined(pkg: Symbol)(implicit ctx: Context) extends Message(PackageNameAlreadyDefinedID) { - override def msg: String = hl"${pkg} is already defined, cannot be a package" - override def kind: String = "Syntax" - override def explanation: String = - "An object cannot have the same name as an existing package. Rename ${pkg} or the package with the same name." + val msg = hl"${pkg} is already defined, cannot be a ${"package"}" + val kind = "Syntax" + val explanation = + hl"An ${"object"} cannot have the same name as an existing ${"package"}. Rename either one of them." } } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 7c930ef93a3d..a6bc8a46fd38 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -1200,7 +1200,7 @@ class ErrorMessagesTests extends ErrorMessagesTest { }.expect { (ictx, messages) => implicit val ctx: Context = ictx - assertMessageCount(1, messages) - assert(messages.head.isInstanceOf[PackageNameAlreadyDefined]) + val PackageNameAlreadyDefined(pkg) = messages.head + assertEquals(pkg.show, "object bar") } } From 72432f4ea9aa1cb458608bd889c16d3b3f192d6e Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Sun, 19 Nov 2017 22:17:01 +0100 Subject: [PATCH 3/3] Polish --- .../src/dotty/tools/dotc/reporting/diagnostic/messages.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 8065b57b8880..2a79fe54c90b 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1924,9 +1924,8 @@ object messages { } case class PackageNameAlreadyDefined(pkg: Symbol)(implicit ctx: Context) extends Message(PackageNameAlreadyDefinedID) { - val msg = hl"${pkg} is already defined, cannot be a ${"package"}" - val kind = "Syntax" + val kind = "Naming" val explanation = hl"An ${"object"} cannot have the same name as an existing ${"package"}. Rename either one of them." }