-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1804,4 +1804,14 @@ object messages { | |
|refers to a subtype of type ${"A"}. | ||
|""" | ||
} | ||
|
||
case class ClassAndCompanionNameClash(cls: Symbol)(implicit ctx: Context) | ||
extends Message(ClassAndCompanionNameClashID) { | ||
val kind = "Syntax" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be "Naming" |
||
val msg = hl"Naming clash, ${cls.owner} and it's companion object defines $cls" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say val msg = hl"Name clash: both ${cls.owner} and its companion object defines ${cls.name}" |
||
val explanation = | ||
s"""It is not allowed to define a class or module with the same | ||
|name inside a class and companion object. | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming that you have the two symbols that clash: val explanation = {
val kind = if (cls.owner.is(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}"""
} |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,9 +115,7 @@ object RefChecks { | |
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) | ||
ctx.error(ClassAndCompanionNameClash(cls)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You forgot the position: ctx.error(ClassAndCompanionNameClash(cls), cls.pos) |
||
} | ||
|
||
// Override checking ------------------------------------------------------------ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1038,4 +1038,24 @@ class ErrorMessagesTests extends ErrorMessagesTest { | |
val ExpectedTypeBoundOrEquals(found) :: Nil = messages | ||
assertEquals(Tokens.IDENTIFIER, found) | ||
} | ||
|
||
@Test def classAndCompanionNameClash = | ||
checkMessagesAfter("refchecks") { | ||
""" | ||
|class T { | ||
| class G {} | ||
|} | ||
|object T { | ||
| class G {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make it a |
||
|} | ||
""".stripMargin | ||
}.expect { (ictx, messages) => | ||
implicit val ctx: Context = ictx | ||
|
||
assertMessageCount(1, messages) | ||
val ClassAndCompanionNameClash(symbol) :: Nil = messages | ||
|
||
assertEquals("class T", symbol.owner.show) | ||
assertEquals("class G", symbol.show) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming that you have the two symbols that clash val ClassAndCompanionNameClash(cls, other) :: Nil = messages
assertEquals("class G", cls.show)
assertEquals("trait G", other.show)
assertEquals("class T", cls.owner.show)
assertEquals("object T", other.owner.show) |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would include the two symbols that clash:
cls
andother