Skip to content

new format for "package name already defined" error (+tests) #3437

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
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public enum ErrorMessageID {
IllegalStartOfStatementID,
TraitIsExpectedID,
TraitRedefinedFinalMethodFromAnyRefID,
PackageNameAlreadyDefinedID,
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1922,4 +1922,11 @@ 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) {
val msg = hl"${pkg} is already defined, cannot be a ${"package"}"
val kind = "Naming"
val explanation =
hl"An ${"object"} cannot have the same name as an existing ${"package"}. Rename either one of them."
}
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

val PackageNameAlreadyDefined(pkg) = messages.head
assertEquals(pkg.show, "object bar")
}
}