File tree 2 files changed +31
-6
lines changed
compiler/src/dotty/tools/dotc/typer
2 files changed +31
-6
lines changed Original file line number Diff line number Diff line change @@ -1080,20 +1080,23 @@ trait Checking {
1080
1080
report.error(em " $what can only be used in an inline method " , pos)
1081
1081
1082
1082
/** 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.
1084
1085
*/
1085
1086
def checkEnum (cdef : untpd.TypeDef , cls : Symbol , firstParent : Symbol )(using Context ): Unit = {
1086
1087
def isEnumAnonCls =
1087
1088
cls.isAnonymousClass
1088
1089
&& cls.owner.isTerm
1089
1090
&& (cls.owner.flagsUNSAFE.isAllOf(EnumCase )
1090
1091
|| ((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
1094
1098
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
1097
1100
// Since enums are classes and Namer checks that classes don't extend multiple classes, we only check the class
1098
1101
// parent.
1099
1102
//
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