Skip to content

Make hashcode of enum items stable #23218

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

mbovel
Copy link
Member

@mbovel mbovel commented May 21, 2025

Fixes #19177. Done during the compiler issue spree of Monday, May 19th.

Note: hashCode of String is cached (see https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/String.java#L2403-L2436), so this change should not introduce a significant performance overhead.

Co-authored-by: anna herlihy <[email protected]>
Co-authored-by: Miguel Trigueira <[email protected]>
@@ -116,6 +116,12 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
def syntheticDefIfMissing(sym: Symbol): List[Tree] =
if (existingDef(sym, clazz).exists) Nil else syntheticDef(sym) :: Nil

def identifierRef: Tree =
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: this was just moved from an inner scope. No changes.

@mbovel mbovel requested a review from sjrd May 21, 2025 12:49
@soronpo
Copy link
Contributor

soronpo commented May 22, 2025

I think a proper check should show different cases have different hashes and also if you have a case with an argument that two instances with the same argument have the same hash.

@mbovel
Copy link
Member Author

mbovel commented May 22, 2025

@soronpo I added both tests. This PR only changes the hashcode of "enum values" (cases without parentheses), but it's probably still good to test the other cases as well.

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.

HashCode of enum cases is not stable
2 participants