Skip UseEnumSetOf empty conversion for static fields to avoid circular class-init (#1157)#1161
Merged
Merged
Conversation
Skip conversion for empty Set.of() in static field initializers to avoid ClassCastException.
|
Quick as usual, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
UseEnumSetOfrewrites an emptySet.of()toEnumSet.noneOf(E.class). In a static field initializer,EnumSet.noneOfreflectively readsE's constants during class initialization. When two enums have a circular static dependency (one enum's constants carry a value from a sibling enum that also holds anEnumSet.noneOf(...)static field), this can re-enter a not-yet-initialized enum and throw a misleadingClassCastException("... not an enum")at runtime — even though the code compiles cleanly.Fix
Skip the empty
Set.of()→EnumSet.noneOf(E.class)conversion when the assignment target is a static field. Static field initializers run during<clinit>, which is the only place the re-entrant class-init hazard exists; local variables and instance fields are unaffected and still convert (e.g. the existingdontHaveArgstest). The non-emptySet.of(E.X, ...)case is untouched since it does not use the reflectivenoneOfpath.HashMap).