Skip to content

Commit ac00dbb

Browse files
committed
Port more Desugar errors to the new scheme
1 parent 2dba45c commit ac00dbb

File tree

8 files changed

+84
-3
lines changed

8 files changed

+84
-3
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ object desugar {
558558
else if (originalTparams.isEmpty)
559559
appliedRef(enumClassRef)
560560
else {
561-
ctx.error(i"explicit extends clause needed because both enum case and enum class have type parameters"
561+
ctx.error(TypedCaseDoesNotExplicitlyExtendTypedEnum(enumClass, cdef)
562562
, cdef.sourcePos.startPos)
563563
appliedTypeTree(enumClassRef, constrTparams map (_ => anyRef))
564564
}
@@ -979,7 +979,7 @@ object desugar {
979979
if (name.isEmpty) name = name.likeSpaced(inventGivenOrExtensionName(impl))
980980
if (ctx.owner == defn.ScalaPackageClass && defn.reservedScalaClassNames.contains(name.toTypeName)) {
981981
def kind = if (name.isTypeName) "class" else "object"
982-
ctx.error(em"illegal redefinition of standard $kind $name", mdef.sourcePos)
982+
ctx.error(IllegalRedefinitionOfStandardKind(kind, name), mdef.sourcePos)
983983
name = name.errorName
984984
}
985985
name

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
155155
CaseClassMissingNonImplicitParamListID,
156156
EnumerationsShouldNotBeEmptyID,
157157
AbstractCannotBeUsedForObjectsID,
158-
ModifierRedundantForObjectsID
158+
ModifierRedundantForObjectsID,
159+
TypedCaseDoesNotExplicitlyExtendTypedEnumID,
160+
IllegalRedefinitionOfStandardKindID
159161

160162
def errorNumber = ordinal - 2
161163
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,4 +2443,30 @@ object messages {
24432443
| ${hl("object")} ${mdef.name} { }
24442444
|""".stripMargin
24452445
}
2446+
2447+
case class TypedCaseDoesNotExplicitlyExtendTypedEnum(enumDef: Symbol, caseDef: untpd.TypeDef)(implicit ctx: Context)
2448+
extends Message(TypedCaseDoesNotExplicitlyExtendTypedEnumID) {
2449+
val kind: String = "Syntax"
2450+
val msg: String = i"explicit extends clause needed because both enum case and enum class have type parameters"
2451+
2452+
val explanation: String =
2453+
em"""Enumerations where the enum class as well as the enum case have type parameters need
2454+
|an explicit extends.
2455+
|for example:
2456+
| ${hl("enum")} ${enumDef.name}[T] {
2457+
| ${hl("case")} ${caseDef.name}[U](u: U) ${hl("extends")} ${enumDef.name}[U]
2458+
| }
2459+
|""".stripMargin
2460+
}
2461+
2462+
case class IllegalRedefinitionOfStandardKind(kindType: String, name: Name)(implicit ctx: Context)
2463+
extends Message(IllegalRedefinitionOfStandardKindID) {
2464+
val kind: String = "Syntax"
2465+
val msg: String = em"illegal redefinition of standard $kindType $name"
2466+
2467+
val explanation: String =
2468+
em"""| "$name" is a standard Scala core `$kindType`
2469+
| Please choose a different name to avoid conflicts
2470+
|""".stripMargin
2471+
}
24462472
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,4 +1770,39 @@ class ErrorMessagesTests extends ErrorMessagesTest {
17701770
assertEquals("sealed modifier is redundant for objects", errorMsg)
17711771
assertEquals("Foo", mdef.name.toString)
17721772
}
1773+
1774+
@Test def enumAndCaseWithTypesNeedExplicitExtends =
1775+
checkMessagesAfter(RefChecks.name) {
1776+
"""
1777+
|enum E[T,U,V] {
1778+
| case C[X,Y,Z](x: X, y: Y, z: Z)
1779+
|}
1780+
""".stripMargin
1781+
}
1782+
.expect { (ictx, messages)
1783+
implicit val ctx: Context = ictx
1784+
assertMessageCount(1, messages)
1785+
val errorMsg = messages.head.msg
1786+
val TypedCaseDoesNotExplicitlyExtendTypedEnum(enumDef, caseDef) :: Nil = messages
1787+
assertEquals("explicit extends clause needed because both enum case and enum class have type parameters", errorMsg)
1788+
assertEquals("E", enumDef.name.toString)
1789+
assertEquals("C", caseDef.name.toString)
1790+
}
1791+
1792+
@Test def illegalRedefinitionOfStandardKind =
1793+
checkMessagesAfter(RefChecks.name) {
1794+
""" package scala {
1795+
| class Any()
1796+
| }
1797+
""".stripMargin
1798+
}
1799+
.expect { (ictx, messages)
1800+
implicit val ctx: Context = ictx
1801+
assertMessageCount(1, messages)
1802+
val errorMsg = messages.head.msg
1803+
val IllegalRedefinitionOfStandardKind(kind, name) :: Nil = messages
1804+
assertEquals("illegal redefinition of standard class Any", errorMsg)
1805+
assertEquals("class", kind)
1806+
assertEquals("Any", name.toString)
1807+
}
17731808
}

tests/neg/enumWithType.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E148] Syntax Error: tests/neg/enumWithType.scala:2:2 ---------------------------------------------------------------
2+
2 | case C[U](u: U) // error
3+
| ^
4+
| explicit extends clause needed because both enum case and enum class have type parameters
5+
6+
longer explanation available when compiling with `-explain`

tests/neg/enumWithType.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
enum E[T] {
2+
case C[U](u: U) // error
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E149] Syntax Error: tests/neg/scalaStandardRedefinition.scala:2:8 --------------------------------------------------
2+
2 | class Any() // error
3+
| ^^^^^^^^^^^
4+
| illegal redefinition of standard class Any
5+
6+
longer explanation available when compiling with `-explain`
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package scala {
2+
class Any() // error
3+
}

0 commit comments

Comments
 (0)