Skip to content

Preserve Java enum semantics for explicit java.lang.Enum inheritance#25658

Open
zielinsky wants to merge 6 commits intoscala:mainfrom
zielinsky:i11253v2
Open

Preserve Java enum semantics for explicit java.lang.Enum inheritance#25658
zielinsky wants to merge 6 commits intoscala:mainfrom
zielinsky:i11253v2

Conversation

@zielinsky
Copy link
Copy Markdown
Member

@zielinsky zielinsky commented Mar 31, 2026

Scala currently accepts some explicit java.lang.Enum[...] usages that are not consistent with Java enum semantics. In particular, F-bound checking alone is not sufficient here: it can accept programs where the type argument satisfies E <: Enum[E], but does not actually match the enum class.

This leads to problematic cases such as enum E extends Enum[Nothing] { case X } (i11252)

These cases may compile, but can break the expected Java enum behavior, especially around reflection and runtime consistency.

After discussion during the Scala Core meeting (01.04.2026), we decided that when code explicitly uses extends java.lang.Enum[...], Scala should preserve Java enum semantics more strictly.

This means we require:

  1. the java.lang.Enum type argument to refer to the enum class itself
  2. the enum to have no type parameters

This rejects examples such as:

  • enum E extends Enum[Nothing]
  • generic enums like enum E[A] extends Enum[E[?]]

Fixes #11252
Fixes #11253


Typo in the error message has also been fixed (paramerized -> parameterized)

@zielinsky zielinsky changed the title Alternative PR for i11253 and i11252 Preserve Java enum semantics for explicit java.lang.Enum inheritance Apr 2, 2026
@zielinsky zielinsky force-pushed the i11253v2 branch 3 times, most recently from 2199230 to e5c9626 Compare April 2, 2026 12:48
@zielinsky zielinsky marked this pull request as ready for review April 3, 2026 10:18
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.

Enum.values should be consistent with getEnumConstants Enum[Nothing] is allowed but has a sharp edge

1 participant