@@ -17,7 +17,7 @@ import scala.util.{Try, Failure, Success}
1717import config .{ScalaVersion , NoScalaVersion }
1818import Decorators ._
1919import typer .ErrorReporting ._
20- import config .Feature .warnOnMigration
20+ import config .Feature .{ warnOnMigration , migrateTo3 }
2121import reporting ._
2222
2323object RefChecks {
@@ -88,8 +88,9 @@ object RefChecks {
8888 cls.thisType
8989 }
9090
91- /** Check that self type of this class conforms to self types of parents.
92- * and required classes.
91+ /** Check that self type of this class conforms to self types of parents
92+ * and required classes. Also check that only `enum` constructs extend
93+ * `java.lang.Enum`.
9394 */
9495 private def checkParents (cls : Symbol )(using Context ): Unit = cls.info match {
9596 case cinfo : ClassInfo =>
@@ -99,10 +100,19 @@ object RefChecks {
99100 report.error(DoesNotConformToSelfType (category, cinfo.selfType, cls, otherSelf, relation, other.classSymbol),
100101 cls.sourcePos)
101102 }
102- for (parent <- cinfo.classParents)
103+ val parents = cinfo.classParents
104+ for (parent <- parents)
103105 checkSelfConforms(parent.classSymbol.asClass, " illegal inheritance" , " parent" )
104106 for (reqd <- cinfo.cls.givenSelfType.classSymbols)
105107 checkSelfConforms(reqd, " missing requirement" , " required" )
108+
109+ // Prevent wrong `extends` of java.lang.Enum
110+ if ! migrateTo3 &&
111+ ! cls.isOneOf(Enum | Trait ) &&
112+ parents.exists(_.classSymbol == defn.JavaEnumClass )
113+ then
114+ report.error(CannotExtendJavaEnum (cls), cls.sourcePos)
115+
106116 case _ =>
107117 }
108118
0 commit comments