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

It's possible to create a Hash with duplicate keys using missing key block and update method #14416

Closed
robdavid opened this issue Apr 2, 2024 · 2 comments · Fixed by #14417
Closed
Labels
kind:bug kind:regression Something that used to correctly work but no longer works topic:stdlib:collection

Comments

@robdavid
Copy link

robdavid commented Apr 2, 2024

Consider the following code

result = Hash(String,Int32).new{ |h,k| h[k] = 0 }
result.update("NAME") { |v| v+1 }
pp! result

Running this under Crystal 1.9.2 gives the expected result of:

result # => {"NAME" => 1}

However, from Crystal 1.10.1 up to 1.11.2 (latest) I'm seeing:

result # => {"NAME" => 0, "NAME" => 1}

with two values for the same key!

Also,

pp! result.size
pp! result["NAME"]

Gives

result.size # => 2
result["NAME"] # => 0

You can see this easily in the crystal playground
image
image

@HertzDevil HertzDevil added topic:stdlib:collection kind:regression Something that used to correctly work but no longer works labels Apr 2, 2024
@HertzDevil
Copy link
Contributor

Regression of #13590

@MatthewMerrill
Copy link

It is also possible using Hash#put_if_absent. IMHO, in addition to #14417 the private method insert_new should be removed in favor of upsert in put_if_absent.

image

my_hash = Hash(String, Int32).new
my_hash.put_if_absent("a") { |k| my_hash[k] = 1 }
p! my_hash
# my_hash # => {"a" => 1, "a" => 1}

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug kind:regression Something that used to correctly work but no longer works topic:stdlib:collection
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants