-
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
Add error message for companion and class/module name clash #3361
Conversation
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.
Hello, and thank you for opening this PR! 🎉
All contributors have signed the CLA, thank you! ❤️
Have an awesome day! ☀️
76cde9a
to
f9af73d
Compare
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Should be "Naming"
@@ -1804,4 +1804,14 @@ object messages { | |||
|refers to a subtype of type ${"A"}. | |||
|""" | |||
} | |||
|
|||
case class ClassAndCompanionNameClash(cls: Symbol)(implicit ctx: Context) |
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
and other
case class ClassAndCompanionNameClash(cls: Symbol)(implicit ctx: Context) | ||
extends Message(ClassAndCompanionNameClashID) { | ||
val kind = "Syntax" | ||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming that you have the two symbols that clash: cls
and other
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}"""
}
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 comment
The reason will be displayed to describe this comment to others. Learn more.
You forgot the position:
ctx.error(ClassAndCompanionNameClash(cls), cls.pos)
| class G {} | ||
|} | ||
|object T { | ||
| class G {} |
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 make it a trait
, to make the test case more interesting
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming that you have the two symbols that clash cls
and other
:
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)
@allanrenucci Thank you for the help, this is my first contribution to dotty 🎉 |
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.
Otherwise LGTM. Thank you for your contribution! 🎉
checkMessagesAfter("refchecks") { | ||
""" | ||
|class T { | ||
| trait G |
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 make it a trait, to make the test case more interesting
Sorry if it wasn't clear, this one should be class G
. I only meant to change the one in object T
:
class T {
class G
}
object T {
trait G
}
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.
👍
@rsoeldner If you are interested, there is a related issue #3364 that should be relatively easy to solve |
Add error message inside
RefChecks.scala
for companion and class or module naming clash.This is related to #1589
@felixmulder please review