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

Conversation

rsoeldner
Copy link
Contributor

@rsoeldner rsoeldner commented Oct 20, 2017

Add error message inside RefChecks.scala for companion and class or module naming clash.
This is related to #1589

@felixmulder please review

Copy link
Member

@dottybot dottybot left a 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! ☀️

@rsoeldner rsoeldner force-pushed the add-errormsg-refChecks-clashes branch from 76cde9a to f9af73d Compare October 20, 2017 20:26

case class ClassAndCompanionNameClash(cls: Symbol)(implicit ctx: Context)
extends Message(ClassAndCompanionNameClashID) {
val kind = "Syntax"
Copy link
Contributor

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)
Copy link
Contributor

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"
Copy link
Contributor

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.
"""
Copy link
Contributor

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))
Copy link
Contributor

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 {}
Copy link
Contributor

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)
Copy link
Contributor

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)

@rsoeldner
Copy link
Contributor Author

@allanrenucci Thank you for the help, this is my first contribution to dotty 🎉

Copy link
Contributor

@allanrenucci allanrenucci left a 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
Copy link
Contributor

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
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@allanrenucci
Copy link
Contributor

@rsoeldner If you are interested, there is a related issue #3364 that should be relatively easy to solve

@allanrenucci allanrenucci merged commit 0438e48 into scala:master Oct 22, 2017
allanrenucci added a commit to dotty-staging/dotty that referenced this pull request Oct 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants