Skip to content
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

Mix in the productPrefix hash statically in case class hashCode #22865

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lrytz
Copy link
Member

@lrytz lrytz commented Mar 24, 2025

Since 2.13, case class hashCode mixes in the hash code of the productPrefix string. This is inconsistent with the equals method, subclasses of case classes that override productPrefix compare equal but have a different hashCode (scala/bug#13033).

This commit changes hashCode to mix in the productPrefix.hashCode statically instead of invoking productPrefix at runtime.

For case classes without primitive fields, the synthetic hashCode invokes ScalaRunTime._hashCode, which mixes in the result of productPrefix. To fix that, the synthetic hashCode is changed to invoke MurmurHash3.productHash directly and mix in the name to the seed statically.

Scala 3 forward port of scala/scala#11023

Since 2.13, case class `hashCode` mixes in the hash code of the
`productPrefix` string. This is inconsistent with the `equals` method,
subclasses of case classes that override `productPrefix` compare
equal but have a different `hashCode`.

This commit changes `hashCode` to mix in the `productPrefix.hashCode`
statically instead of invoking `productPrefix` at runtime.

For case classes without primitive fields, the synthetic `hashCode`
invokes `ScalaRunTime._hashCode`, which mixes in the result of
`productPrefix`. To fix that, the synthetic hashCode is changed
to invoke `MurmurHash3.productHash` directly and mix in the name
to the seed statically.
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.

2 participants