@@ -638,7 +638,7 @@ trait Checking {
638638 *
639639 * The standard library relies on this idiom.
640640 */
641- def checkTraitInheritance (parent : Symbol , cls : ClassSymbol , pos : Position )(implicit ctx : Context ): Unit = {
641+ def checkTraitInheritance (parent : Symbol , cls : ClassSymbol , pos : Position )(implicit ctx : Context ): Unit =
642642 parent match {
643643 case parent : ClassSymbol if parent is Trait =>
644644 val psuper = parent.superClass
@@ -650,7 +650,18 @@ trait Checking {
650650 ctx.error(em " illegal trait inheritance: super $csuper does not derive from $parent's super $psuper" , pos)
651651 case _ =>
652652 }
653- }
653+
654+ /** Check that case classes are not inherited by case classes.
655+ */
656+ def checkCaseInheritance (parent : Symbol , caseCls : ClassSymbol , pos : Position )(implicit ctx : Context ): Unit =
657+ parent match {
658+ case parent : ClassSymbol =>
659+ if (parent is Case )
660+ ctx.error(ex """ case $caseCls has case ancestor $parent, but case-to-case inheritance is prohibited.
661+ |To overcome this limitation, use extractors to pattern match on non-leaf nodes. """ , pos)
662+ else checkCaseInheritance(parent.superClass, caseCls, pos)
663+ case _ =>
664+ }
654665
655666 /** Check that method parameter types do not reference their own parameter
656667 * or later parameters in the same parameter section.
@@ -686,5 +697,6 @@ trait NoChecking extends Checking {
686697 override def checkNotSingleton (tpt : Tree , where : String )(implicit ctx : Context ): Tree = tpt
687698 override def checkDerivedValueClass (clazz : Symbol , stats : List [Tree ])(implicit ctx : Context ) = ()
688699 override def checkTraitInheritance (parentSym : Symbol , cls : ClassSymbol , pos : Position )(implicit ctx : Context ) = ()
700+ override def checkCaseInheritance (parentSym : Symbol , caseCls : ClassSymbol , pos : Position )(implicit ctx : Context ) = ()
689701 override def checkNoForwardDependencies (vparams : List [ValDef ])(implicit ctx : Context ): Unit = ()
690702}
0 commit comments