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

Always copy Hash's default block on #dup and #clone #10744

Merged

Conversation

HertzDevil
Copy link
Contributor

When a hash is copied by either #dup or #clone, only compare_by_identity is copied but not the default block when the hash is empty:

hsh = Hash(Int32, String).new("")
hsh.compare_by_identity
hsh.dup.compare_by_identity? # => true
hsh.dup[0]                   # Missing hash key: 0 (KeyError)

hsh = Hash(Int32, String).new("")
hsh[1] = "a"
hsh.compare_by_identity
hsh.dup.compare_by_identity? # => true
hsh.dup[0]                   # => ""

Since the default block is part of the hash's internal state, it should always be copied even if the hash has no elements.

Copy link
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

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

Great, thanks!

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection labels May 25, 2021
@straight-shoota straight-shoota added this to the 1.1.0 milestone May 25, 2021
@caspiano
Copy link
Contributor

you're an absolute don @HertzDevil!! thanks for the work you do for this language and community ❤️

@straight-shoota straight-shoota merged commit 3f99d7d into crystal-lang:master May 27, 2021
@HertzDevil HertzDevil deleted the bug/hash-empty-default-block branch May 27, 2021 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants