Skip to content

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

@robdavid

Description

@robdavid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:bugA bug in the code. Does not apply to documentation, specs, etc.kind:regressionSomething that used to correctly work but no longer workstopic:stdlib:collection

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions