File tree Expand file tree Collapse file tree 4 files changed +32
-3
lines changed
test/dotty/tools/dotc/reporting Expand file tree Collapse file tree 4 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -103,6 +103,7 @@ public enum ErrorMessageID {
103
103
ExtendFinalClassID ,
104
104
EnumCaseDefinitionInNonEnumOwnerID ,
105
105
ExpectedTypeBoundOrEqualsID ,
106
+ ClassAndCompanioNameClashID ,
106
107
;
107
108
108
109
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -1804,4 +1804,14 @@ object messages {
1804
1804
|refers to a subtype of type ${" A" }.
1805
1805
| """
1806
1806
}
1807
+
1808
+ case class ClassAndCompanionNameClash (cls : Symbol )(implicit ctx : Context )
1809
+ extends Message (ClassAndCompanioNameClashID ) {
1810
+ val kind = " Syntax"
1811
+ val msg = hl " Naming clash, ${cls.owner} and it's companion object defines $cls"
1812
+ val explanation =
1813
+ s """ It is not allowed to define a class or module with the same
1814
+ |name inside a class and companion object.
1815
+ """
1816
+ }
1807
1817
}
Original file line number Diff line number Diff line change @@ -115,9 +115,7 @@ object RefChecks {
115
115
if (! (cls.owner is ModuleClass )) {
116
116
val other = cls.owner.linkedClass.info.decl(cls.name)
117
117
if (other.symbol.isClass)
118
- ctx.error(s " name clash: ${cls.owner} defines $cls" + " \n " +
119
- s " and its companion ${cls.owner.companionModule} also defines $other" ,
120
- cls.pos)
118
+ ctx.error(ClassAndCompanionNameClash (cls))
121
119
}
122
120
123
121
// Override checking ------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -1038,4 +1038,24 @@ class ErrorMessagesTests extends ErrorMessagesTest {
1038
1038
val ExpectedTypeBoundOrEquals (found) :: Nil = messages
1039
1039
assertEquals(Tokens .IDENTIFIER , found)
1040
1040
}
1041
+
1042
+ @ Test def classAndCompanionNameClash =
1043
+ checkMessagesAfter(" refchecks" ) {
1044
+ """
1045
+ |class T {
1046
+ | class G {}
1047
+ |}
1048
+ |object T {
1049
+ | class G {}
1050
+ |}
1051
+ """ .stripMargin
1052
+ }.expect { (ictx, messages) =>
1053
+ implicit val ctx : Context = ictx
1054
+
1055
+ assertMessageCount(1 , messages)
1056
+ val ClassAndCompanionNameClash (symbol) :: Nil = messages
1057
+
1058
+ assertEquals(" class T" , symbol.owner.show)
1059
+ assertEquals(" class G" , symbol.show)
1060
+ }
1041
1061
}
You can’t perform that action at this time.
0 commit comments