Skip to content

Backport "Mix in the productPrefix hash statically in case class hashCode" to 3.3 LTS #345

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
merged 2 commits into from
Apr 28, 2025

Conversation

tgodzik
Copy link

@tgodzik tgodzik commented Apr 27, 2025

Backports scala#22865 to the 3.3.7.

PR submitted by the release tooling.
[skip ci]

lrytz and others added 2 commits April 27, 2025 22:59
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.
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.

[Cherry-picked d2fc358][modified]
@tgodzik
Copy link
Author

tgodzik commented Apr 28, 2025

No regressions detected in the community build up to backport-lts-3.3-22957.

Reference

@tgodzik tgodzik merged commit d0255bb into lts-3.3 Apr 28, 2025
22 checks passed
@tgodzik tgodzik deleted the backport-lts-3.3-22865 branch April 28, 2025 08:40
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