Skip to content

Add error message for companion and class/module name clash #3361

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
merged 3 commits into from
Oct 22, 2017
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 @@ -103,6 +103,7 @@ public enum ErrorMessageID {
ExtendFinalClassID,
EnumCaseDefinitionInNonEnumOwnerID,
ExpectedTypeBoundOrEqualsID,
ClassAndCompanionNameClashID,
;

public int errorNumber() {
Expand Down
13 changes: 13 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1804,4 +1804,17 @@ object messages {
|refers to a subtype of type ${"A"}.
|"""
}

case class ClassAndCompanionNameClash(cls: Symbol, other: Symbol)(implicit ctx: Context)
extends Message(ClassAndCompanionNameClashID) {
val kind = "Naming"
val msg = hl"Name clash: both ${cls.owner} and its companion object defines ${cls.name}"
val explanation = {
val kind = if (cls.owner.is(Flags.Trait)) "trait" else "class"

hl"""|A $kind and its companion object cannot both define a ${"class"}, ${"trait"} or ${"object"} with the same name:
| - ${cls.owner} defines ${cls}
| - ${other.owner} defines ${other}"""
}
}
}
8 changes: 3 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ object RefChecks {
*/
private def checkCompanionNameClashes(cls: Symbol)(implicit ctx: Context): Unit =
if (!(cls.owner is ModuleClass)) {
val other = cls.owner.linkedClass.info.decl(cls.name)
if (other.symbol.isClass)
ctx.error(s"name clash: ${cls.owner} defines $cls" + "\n" +
s"and its companion ${cls.owner.companionModule} also defines $other",
cls.pos)
val other = cls.owner.linkedClass.info.decl(cls.name).symbol
if (other.isClass)
ctx.error(ClassAndCompanionNameClash(cls, other), cls.pos)
}

// Override checking ------------------------------------------------------------
Expand Down
23 changes: 23 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1038,4 +1038,27 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val ExpectedTypeBoundOrEquals(found) :: Nil = messages
assertEquals(Tokens.IDENTIFIER, found)
}

@Test def classAndCompanionNameClash =
checkMessagesAfter("refchecks") {
"""
|class T {
| class G
|}
|object T {
| trait G
|}
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

assertMessageCount(1, messages)
val ClassAndCompanionNameClash(cls, other) :: Nil = messages

assertEquals("class T", cls.owner.show)
assertEquals("class G", cls.show)
assertEquals("object T", other.owner.show)
assertEquals("trait G", other.show)

}
}