Skip to content

Commit 78c60a7

Browse files
Merge pull request #3483 from maseev/iss1589-cyclic-inheritence-error-message
Add an error message for a cyclic inheritance case
2 parents 1d24b19 + 0bc09b3 commit 78c60a7

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public enum ErrorMessageID {
116116
PackageNameAlreadyDefinedID,
117117
UnapplyInvalidNumberOfArgumentsID,
118118
StaticFieldsOnlyAllowedInObjectsID,
119+
CyclicInheritanceID,
119120
;
120121

121122
public int errorNumber() {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,4 +1949,21 @@ object messages {
19491949
val explanation =
19501950
hl"${"@static"} members are only allowed inside objects."
19511951
}
1952+
1953+
case class CyclicInheritance(symbol: Symbol, addendum: String)(implicit ctx: Context) extends Message(CyclicInheritanceID) {
1954+
val kind = "Syntax"
1955+
val msg = hl"Cyclic inheritance: $symbol extends itself$addendum"
1956+
val explanation = {
1957+
val codeExample = "class A extends A"
1958+
1959+
hl"""Cyclic inheritance is prohibited in Dotty.
1960+
|Consider the following example:
1961+
|
1962+
|$codeExample
1963+
|
1964+
|The example mentioned above would fail because this type of inheritance hierarchy
1965+
|creates a "cycle" where a not yet defined class A extends itself which makes
1966+
|impossible to instantiate an object of this class"""
1967+
}
1968+
}
19521969
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ class Namer { typer: Typer =>
869869
"\n(Note that inheriting a class of the same name is no longer allowed)"
870870
case _ => ""
871871
}
872-
ctx.error(i"cyclic inheritance: $cls extends itself$addendum", parent.pos)
872+
ctx.error(CyclicInheritance(cls, addendum), parent.pos)
873873
defn.ObjectType
874874
}
875875
else {

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,6 @@ class ErrorMessagesTests extends ErrorMessagesTest {
12251225
assertEquals("(class Int, class String)", argTypes.map(_.typeSymbol).mkString("(", ", ", ")"))
12261226
}
12271227

1228-
12291228
@Test def staticOnlyAllowedInsideObjects =
12301229
checkMessagesAfter("checkStatic") {
12311230
"""
@@ -1238,4 +1237,16 @@ class ErrorMessagesTests extends ErrorMessagesTest {
12381237
val StaticFieldsOnlyAllowedInObjects(field) = messages.head
12391238
assertEquals(field.show, "method bar")
12401239
}
1240+
1241+
@Test def cyclicInheritance =
1242+
checkMessagesAfter("frontend") {
1243+
"class A extends A"
1244+
}
1245+
.expect { (ictx, messages) =>
1246+
implicit val ctx: Context = ictx
1247+
1248+
assertMessageCount(1, messages)
1249+
val CyclicInheritance(symbol, _) :: Nil = messages
1250+
assertEquals("class A", symbol.show)
1251+
}
12411252
}

0 commit comments

Comments
 (0)