Skip to content

Fix #7499: Prevent extending java.lang.Enum except from an enum #9487

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

Conversation

TheElectronWill
Copy link
Contributor

With this PR, extending java.lang.Enum from a class or trait gives an error.

class C1 extends jl.Enum[C1]
      ^
class C1 cannot extend java.lang.Enum: only enums defined with the enum syntax can

(one of) The Scala2 way(s) of declaring a java-compatible enum is also covered:

class C2(name: String, ordinal: Int) extends jl.Enum[C2](name, ordinal)
      ^
class C2 cannot extend java.lang.Enum: only enums defined with the enum syntax can

To emit a nice error for the above case I made the two-arguments constructor of Enum visible (if there's a better way I'll be glad to change that), and I introduced a check in CompleteJavaEnums to prevent that constructor from being explicitely called.

enum E extends java.lang.Enum[E]("name", 0) {
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            the constructor of java.lang.Enum cannot be called explicitly

@TheElectronWill TheElectronWill linked an issue Aug 3, 2020 that may be closed by this pull request
@smarter smarter requested a review from bishabosha August 3, 2020 18:20
Copy link
Member

@bishabosha bishabosha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on 💯 I just have a few comments

@TheElectronWill TheElectronWill force-pushed the prevent-extend-java-enum branch from ea212f2 to 72d9322 Compare August 7, 2020 13:52
@TheElectronWill
Copy link
Contributor Author

TheElectronWill commented Aug 7, 2020

It's now possible to have a class that extends java.lang.Enum under -source 3.0-migration 🙃

@TheElectronWill TheElectronWill linked an issue Aug 7, 2020 that may be closed by this pull request
Copy link
Member

@bishabosha bishabosha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks :)

@TheElectronWill TheElectronWill merged commit 1af1f46 into scala:master Aug 7, 2020
@TheElectronWill TheElectronWill deleted the prevent-extend-java-enum branch August 10, 2020 14:08
@bishabosha
Copy link
Member

@TheElectronWill sorry, I made a mistake by suggesting Traits should be able to extend java.lang.Enum, e.g. in the test case

trait T extends java.lang.Enum[T]

enum MyEnum extends T {
  case A, B
}

something like EnumSet becomes unusable due to the F bound:

scala> java.util.EnumSet.allOf(classOf[MyEnum])
1 |java.util.EnumSet.allOf(classOf[MyEnum])
  |                        ^^^^^^^^^^^^^^^
  |        Found:    (classOf[MyEnum] : Class[MyEnum])
  |        Required: Class[E]
  |
  |        where:    E is a type variable with constraint <: Enum[LazyRef(E)]

@TheElectronWill
Copy link
Contributor Author

Ah! It's easy to forbid traits again, I'll make a PR right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent extending java.lang.Enum except from an enum Problem extending java.lang.Enum
2 participants