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 1 commit
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
12 changes: 7 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1088,12 +1088,14 @@ trait Checking {
&& 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
20 changes: 20 additions & 0 deletions tests/neg/extend-java-enum-nonstatic.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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

object Static:
enum E extends jl.Enum[E] { case A } // ok