Skip to content

Commit be9de00

Browse files
committed
Fix #3364: Name clash in class and its companion object
1 parent e1365d9 commit be9de00

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,7 @@ object messages {
18081808
case class ClassAndCompanionNameClash(cls: Symbol, other: Symbol)(implicit ctx: Context)
18091809
extends Message(ClassAndCompanionNameClashID) {
18101810
val kind = "Naming"
1811-
val msg = hl"Name clash: both ${cls.owner} and its companion object defines ${cls.name}"
1811+
val msg = hl"Name clash: both ${cls.owner} and its companion object defines ${cls.name.stripModuleClassSuffix}"
18121812
val explanation = {
18131813
val kind = if (cls.owner.is(Flags.Trait)) "trait" else "class"
18141814

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,15 @@ object RefChecks {
112112
* a class or module with same name
113113
*/
114114
private def checkCompanionNameClashes(cls: Symbol)(implicit ctx: Context): Unit =
115-
if (!(cls.owner is ModuleClass)) {
116-
val other = cls.owner.linkedClass.info.decl(cls.name).symbol
117-
if (other.isClass)
115+
if (!cls.owner.is(ModuleClass)) {
116+
def clashes(sym: Symbol) =
117+
sym.isClass &&
118+
sym.name.stripModuleClassSuffix == cls.name.stripModuleClassSuffix
119+
120+
val others = cls.owner.linkedClass.info.decls.filter(clashes)
121+
others.foreach { other =>
118122
ctx.error(ClassAndCompanionNameClash(cls, other), cls.pos)
123+
}
119124
}
120125

121126
// Override checking ------------------------------------------------------------

tests/neg/i3364.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Test {
2+
object Foo
3+
}
4+
5+
class Test {
6+
class Foo // error: name clash
7+
}
8+
9+
object Test2 {
10+
class Foo
11+
}
12+
13+
class Test2 {
14+
object Foo // error: name clash
15+
}

0 commit comments

Comments
 (0)