@@ -846,14 +846,21 @@ trait Checking {
846
846
}
847
847
848
848
/** Check that all case classes that extend `scala.Enum` are `enum` cases */
849
- def checkEnum (cdef : untpd.TypeDef , cls : Symbol )(implicit ctx : Context ): Unit = {
849
+ def checkEnum (cdef : untpd.TypeDef , cls : Symbol , firstParent : Symbol )(implicit ctx : Context ): Unit = {
850
850
import untpd .modsDeco
851
851
def isEnumAnonCls =
852
852
cls.isAnonymousClass &&
853
853
cls.owner.isTerm &&
854
854
(cls.owner.flagsUNSAFE.is(Case ) || cls.owner.name == nme.DOLLAR_NEW )
855
- if (! cdef.mods.isEnumCase && ! isEnumAnonCls)
856
- ctx.error(em " normal case $cls in ${cls.owner} cannot extend an enum " , cdef.pos)
855
+ if (! cdef.mods.isEnumCase && ! isEnumAnonCls) {
856
+ if (cls.is(Case ))
857
+ ctx.error(em " normal case $cls in ${cls.owner} cannot extend an enum " , cdef.pos)
858
+ else if (firstParent != defn.EnumClass && firstParent.derivesFrom(defn.EnumClass ) && ! cdef.mods.isEnumCase && ! isEnumAnonCls)
859
+ // Since enums are classes and Namer checks that classes don't extend multiple classes, we only check the class
860
+ // parent.
861
+ // Tricky to phrase; language taken from "case-to-case inheritance is prohibited".
862
+ ctx.error(s " ${cls.name} has enum ancestor ${firstParent.name}, but inheriting from enums is prohibited " , cdef.pos)
863
+ }
857
864
}
858
865
859
866
/** Check that all references coming from enum cases in an enum companion object
@@ -931,7 +938,7 @@ trait Checking {
931
938
932
939
trait ReChecking extends Checking {
933
940
import tpd ._
934
- override def checkEnum (cdef : untpd.TypeDef , cls : Symbol )(implicit ctx : Context ): Unit = ()
941
+ override def checkEnum (cdef : untpd.TypeDef , cls : Symbol , firstParent : Symbol )(implicit ctx : Context ): Unit = ()
935
942
override def checkRefsLegal (tree : tpd.Tree , badOwner : Symbol , allowed : (Name , Symbol ) => Boolean , where : String )(implicit ctx : Context ): Unit = ()
936
943
override def checkEnumCompanions (stats : List [Tree ], enumContexts : collection.Map [Symbol , Context ])(implicit ctx : Context ): List [Tree ] = stats
937
944
}
0 commit comments