Skip to content

Commit 5e625e9

Browse files
authored
Merge pull request #7465 from x3ro/lucas/illegal-cyclic-type-reference
Port "illegal cyclic type reference" error to new scheme
2 parents 9e7ec22 + 9dbef3b commit 5e625e9

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
147147
StaticFieldsShouldPrecedeNonStaticID,
148148
IllegalSuperAccessorID,
149149
TraitParameterUsedAsParentPrefixID,
150-
UnknownNamedEnclosingClassOrObjectID
150+
UnknownNamedEnclosingClassOrObjectID,
151+
IllegalCyclicTypeReferenceID
151152

152153
def errorNumber = ordinal - 2
153154
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,4 +2392,11 @@ object messages {
23922392
|current scope.
23932393
""".stripMargin
23942394
}
2395+
2396+
case class IllegalCyclicTypeReference(sym: Symbol, where: String, lastChecked: Type)(implicit val ctx: Context)
2397+
extends Message(IllegalCyclicTypeReferenceID) {
2398+
val kind: String = "Type"
2399+
val msg: String = i"illegal cyclic type reference: ${where} ${hl(lastChecked.show)} of $sym refers back to the type itself"
2400+
val explanation: String = ""
2401+
}
23952402
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ object Checking {
300300
catch {
301301
case ex: CyclicReference =>
302302
if (reportErrors)
303-
errorType(i"illegal cyclic reference: ${checker.where} ${checker.lastChecked} of $sym refers back to the type itself", sym.sourcePos)
303+
errorType(IllegalCyclicTypeReference(sym, checker.where, checker.lastChecked), sym.sourcePos)
304304
else info
305305
}
306306
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,4 +1672,19 @@ class ErrorMessagesTests extends ErrorMessagesTest {
16721672
val UnknownNamedEnclosingClassOrObject(name) :: Nil = messages
16731673
assertEquals("doesNotExist", name.show)
16741674
}
1675+
1676+
@Test def illegalCyclicTypeReference() =
1677+
checkMessagesAfter(RefChecks.name) {
1678+
"""
1679+
|type X = List[X]
1680+
""".stripMargin
1681+
}
1682+
.expect { (ictx, messages) =>
1683+
implicit val ctx: Context = ictx
1684+
assertMessageCount(1, messages)
1685+
val IllegalCyclicTypeReference(sym, where, lastChecked) :: Nil = messages
1686+
assertEquals("type X", sym.show)
1687+
assertEquals("alias", where)
1688+
assertEquals("List[X]", lastChecked.show)
1689+
}
16751690
}

0 commit comments

Comments
 (0)