File tree Expand file tree Collapse file tree 2 files changed +31
-6
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 2 files changed +31
-6
lines changed Original file line number Diff line number Diff line change @@ -1080,20 +1080,23 @@ trait Checking {
10801080 report.error(em " $what can only be used in an inline method " , pos)
10811081
10821082 /** 1. Check that all case classes that extend `scala.Enum` are `enum` cases
1083- * 2. Check that case class `enum` cases do not extend java.lang.Enum.
1083+ * 2. Check that parameterised `enum` cases do not extend java.lang.Enum.
1084+ * 3. Check that only a static `enum` base class can extend java.lang.Enum.
10841085 */
10851086 def checkEnum (cdef : untpd.TypeDef , cls : Symbol , firstParent : Symbol )(using Context ): Unit = {
10861087 def isEnumAnonCls =
10871088 cls.isAnonymousClass
10881089 && cls.owner.isTerm
10891090 && (cls.owner.flagsUNSAFE.isAllOf(EnumCase )
10901091 || ((cls.owner.name eq nme.DOLLAR_NEW ) && cls.owner.flagsUNSAFE.isAllOf(Private | Synthetic )))
1091- if (! isEnumAnonCls)
1092- if (cdef.mods.isEnumCase) {
1093- if (cls.derivesFrom(defn.JavaEnumClass ))
1092+ val isJavaEnum = cls.derivesFrom(defn.JavaEnumClass )
1093+ if isJavaEnum && cdef.mods.isEnumClass && ! cls.isStatic then
1094+ report.error(em " An enum extending java.lang.Enum must be declared in a static scope " , cdef.srcPos)
1095+ if ! isEnumAnonCls then
1096+ if cdef.mods.isEnumCase then
1097+ if isJavaEnum then
10941098 report.error(em " paramerized case is not allowed in an enum that extends java.lang.Enum " , cdef.srcPos)
1095- }
1096- else if (cls.is(Case ) || firstParent.is(Enum ))
1099+ else if cls.is(Case ) || firstParent.is(Enum ) then
10971100 // Since enums are classes and Namer checks that classes don't extend multiple classes, we only check the class
10981101 // parent.
10991102 //
Original file line number Diff line number Diff line change 1+ import java .{lang => jl }
2+
3+ object TestSuite :
4+ def test (op : => Unit ): Unit = op
5+ test {
6+ enum E extends jl.Enum [E ] { case A } // error: enum extending java.lang.Enum must be declared in a static scope
7+ }
8+
9+ class Container :
10+ enum E extends jl.Enum [E ] { case A } // error: enum extending java.lang.Enum must be declared in a static scope
11+
12+ object Wrap :
13+ def force =
14+ enum E extends jl.Enum [E ] { case A } // error: enum extending java.lang.Enum must be declared in a static scope
15+
16+ trait Universe :
17+ enum E extends jl.Enum [E ] { case A } // error: enum extending java.lang.Enum must be declared in a static scope
18+
19+ enum E extends jl.Enum [E ] { case A } // ok, a declaration at package level is static.
20+
21+ object Static :
22+ enum E extends jl.Enum [E ] { case A } // ok, a declaration within a static object is static.
You can’t perform that action at this time.
0 commit comments