From be9de007ec392c3b456195bc39d01814abfa6bc9 Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Mon, 23 Oct 2017 16:10:50 +0200 Subject: [PATCH] Fix #3364: Name clash in class and its companion object --- .../dotc/reporting/diagnostic/messages.scala | 2 +- .../src/dotty/tools/dotc/typer/RefChecks.scala | 11 ++++++++--- tests/neg/i3364.scala | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 tests/neg/i3364.scala diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 3c5645810f45..6de9a1275a2e 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1808,7 +1808,7 @@ object messages { 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 msg = hl"Name clash: both ${cls.owner} and its companion object defines ${cls.name.stripModuleClassSuffix}" val explanation = { val kind = if (cls.owner.is(Flags.Trait)) "trait" else "class" diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index d1bd810d4642..204e9cf81e55 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -112,10 +112,15 @@ object RefChecks { * a class or module with same name */ private def checkCompanionNameClashes(cls: Symbol)(implicit ctx: Context): Unit = - if (!(cls.owner is ModuleClass)) { - val other = cls.owner.linkedClass.info.decl(cls.name).symbol - if (other.isClass) + if (!cls.owner.is(ModuleClass)) { + def clashes(sym: Symbol) = + sym.isClass && + sym.name.stripModuleClassSuffix == cls.name.stripModuleClassSuffix + + val others = cls.owner.linkedClass.info.decls.filter(clashes) + others.foreach { other => ctx.error(ClassAndCompanionNameClash(cls, other), cls.pos) + } } // Override checking ------------------------------------------------------------ diff --git a/tests/neg/i3364.scala b/tests/neg/i3364.scala new file mode 100644 index 000000000000..ece499a95f56 --- /dev/null +++ b/tests/neg/i3364.scala @@ -0,0 +1,15 @@ +object Test { + object Foo +} + +class Test { + class Foo // error: name clash +} + +object Test2 { + class Foo +} + +class Test2 { + object Foo // error: name clash +}