Skip to content

Update the error message for 'CaseClassCannotExtendEnum' to include enum name #5051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2121,9 +2121,9 @@ object messages {
|""".stripMargin
}

case class CaseClassCannotExtendEnum(cls: Symbol)(implicit ctx: Context) extends Message(CaseClassCannotExtendEnumID) {
case class CaseClassCannotExtendEnum(cls: Symbol, parent: Symbol)(implicit ctx: Context) extends Message(CaseClassCannotExtendEnumID) {
override def kind: String = "Syntax"
override def msg: String = hl"""normal case $cls in ${cls.owner} cannot extend an enum"""
override def msg: String = hl"""normal case class cannot extend an enum. case $cls in ${cls.owner} is extending enum ${parent.name}."""
override def explanation: String = ""
}
}
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -853,14 +853,14 @@ trait Checking {
ctx.error(em"$what can only be used in a rewrite method", pos)

/** Check that all case classes that extend `scala.Enum` are `enum` cases */
def checkEnum(cdef: untpd.TypeDef, cls: Symbol)(implicit ctx: Context): Unit = {
def checkEnum(cdef: untpd.TypeDef, cls: Symbol, parent: Symbol)(implicit ctx: Context): Unit = {
import untpd.modsDeco
def isEnumAnonCls =
cls.isAnonymousClass &&
cls.owner.isTerm &&
(cls.owner.flagsUNSAFE.is(Case) || cls.owner.name == nme.DOLLAR_NEW)
if (!cdef.mods.isEnumCase && !isEnumAnonCls)
ctx.error(CaseClassCannotExtendEnum(cls), cdef.pos)
ctx.error(CaseClassCannotExtendEnum(cls, parent), cdef.pos)
}

/** Check that all references coming from enum cases in an enum companion object
Expand Down Expand Up @@ -938,7 +938,7 @@ trait Checking {

trait ReChecking extends Checking {
import tpd._
override def checkEnum(cdef: untpd.TypeDef, cls: Symbol)(implicit ctx: Context): Unit = ()
override def checkEnum(cdef: untpd.TypeDef, cls: Symbol, parent: Symbol)(implicit ctx: Context): Unit = ()
override def checkRefsLegal(tree: tpd.Tree, badOwner: Symbol, allowed: (Name, Symbol) => Boolean, where: String)(implicit ctx: Context): Unit = ()
override def checkEnumCompanions(stats: List[Tree], enumContexts: collection.Map[Symbol, Context])(implicit ctx: Context): List[Tree] = stats
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,10 @@ class Typer extends Namer
.withType(dummy.termRef)
if (!cls.is(AbstractOrTrait) && !ctx.isAfterTyper)
checkRealizableBounds(cls, cdef.namePos)
if (cls.is(Case) && cls.derivesFrom(defn.EnumClass)) checkEnum(cdef, cls)
if (cls.is(Case) && cls.derivesFrom(defn.EnumClass)) {
val firstParent = parents1.head.tpe.dealias.typeSymbol
checkEnum(cdef, cls, firstParent)
}
val cdef1 = assignType(cpy.TypeDef(cdef)(name, impl1), cls)
checkVariance(cdef1)
if (ctx.phase.isTyper && cdef1.tpe.derivesFrom(defn.DynamicClass) && !ctx.dynamicsEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class ErrorMessagesTests extends ErrorMessagesTest {
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val errorMsg = messages.head
val CaseClassCannotExtendEnum(cls) :: Nil = messages
val CaseClassCannotExtendEnum(cls, parent) :: Nil = messages
assertEquals("Bar", cls.name.show)
assertEquals("Foo", parent.name.show)
assertEquals("<empty>", cls.owner.name.show)
}

Expand Down