[ruby-core:77685] [Ruby trunk Bug#12855] Inconsistent keys identity in compare_by_identity Hash when using literals

From: merch-redmine@...
Date: 2016-10-20 13:52:55 UTC
List: ruby-core #77685
Issue #12855 has been updated by Jeremy Evans.


While this was a behavior change between 2.1 and 2.2, I'm not sure I would consider it a regression.  It seems unlikely anyone who uses compare_by_identity hashes would also be using string literals and want string literals keys to have different values.

If we do decide to revert to 2.1 behavior for string literals, at the very least we should make sure that on ruby 2.1+:

~~~ ruby
h = {}.compare_by_identity
h['pear'.freeze] = 1
h['pear'.freeze] = 2

p h.size # => 1
# because 'pear'.freeze.equal?('pear'.freeze)
~~~

and on ruby 2.3+:

~~~ ruby
# frozen-string-literal: true
# or when using --enable-frozen-string-literal
h = {}.compare_by_identity
h['pear'] = 1
h['pear'] = 2

p h.size # => 1
# because 'pear'.equal?('pear')
~~~

----------------------------------------
Bug #12855: Inconsistent keys identity in compare_by_identity Hash when using literals
https://bugs.ruby-lang.org/issues/12855#change-60968

* Author: Benoit Daloze
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
This seems a regression since 2.2.
I would guess it's due to some optimization for having a string literal between []=.
That optimization should not trigger for compare_by_identity hashes, so both cases below are consistent.

~~~ruby
h = {}.compare_by_identity
h['pear'] = 1
h['pear'] = 2

p h.size # => 1
p h


h = {}.compare_by_identity
k1 = 'pear'
h[k1] = 1
k2 = 'pear'
h[k2] = 2

p h.size # => 2
p h
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next