Skip to content

fix #9579: prevent non static enums that extend jl.Enum #9678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1080,20 +1080,23 @@ trait Checking {
report.error(em"$what can only be used in an inline method", pos)

/** 1. Check that all case classes that extend `scala.Enum` are `enum` cases
* 2. Check that case class `enum` cases do not extend java.lang.Enum.
* 2. Check that parameterised `enum` cases do not extend java.lang.Enum.
* 3. Check that only a static `enum` base class can extend java.lang.Enum.
*/
def checkEnum(cdef: untpd.TypeDef, cls: Symbol, firstParent: Symbol)(using Context): Unit = {
def isEnumAnonCls =
cls.isAnonymousClass
&& cls.owner.isTerm
&& (cls.owner.flagsUNSAFE.isAllOf(EnumCase)
|| ((cls.owner.name eq nme.DOLLAR_NEW) && cls.owner.flagsUNSAFE.isAllOf(Private | Synthetic)))
if (!isEnumAnonCls)
if (cdef.mods.isEnumCase) {
if (cls.derivesFrom(defn.JavaEnumClass))
val isJavaEnum = cls.derivesFrom(defn.JavaEnumClass)
if isJavaEnum && cdef.mods.isEnumClass && !cls.isStatic then
report.error(em"An enum extending java.lang.Enum must be declared in a static scope", cdef.srcPos)
if !isEnumAnonCls then
if cdef.mods.isEnumCase then
if isJavaEnum then
report.error(em"paramerized case is not allowed in an enum that extends java.lang.Enum", cdef.srcPos)
}
else if (cls.is(Case) || firstParent.is(Enum))
else if cls.is(Case) || firstParent.is(Enum) then
// Since enums are classes and Namer checks that classes don't extend multiple classes, we only check the class
// parent.
//
Expand Down
22 changes: 22 additions & 0 deletions tests/neg/extend-java-enum-nonstatic.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.{lang => jl}

object TestSuite:
def test(op: => Unit): Unit = op
test {
enum E extends jl.Enum[E] { case A } // error: enum extending java.lang.Enum must be declared in a static scope
}

class Container:
enum E extends jl.Enum[E] { case A } // error: enum extending java.lang.Enum must be declared in a static scope

object Wrap:
def force =
enum E extends jl.Enum[E] { case A } // error: enum extending java.lang.Enum must be declared in a static scope

trait Universe:
enum E extends jl.Enum[E] { case A } // error: enum extending java.lang.Enum must be declared in a static scope

enum E extends jl.Enum[E] { case A } // ok, a declaration at package level is static.

object Static:
enum E extends jl.Enum[E] { case A } // ok, a declaration within a static object is static.