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.
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+:
h={}.compare_by_identityh['pear'.freeze]=1h['pear'.freeze]=2ph.size# => 1# because 'pear'.freeze.equal?('pear'.freeze)
and on ruby 2.3+:
# frozen-string-literal: true# or when using --enable-frozen-string-literalh={}.compare_by_identityh['pear']=1h['pear']=2ph.size# => 1# because 'pear'.equal?('pear')
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.
The main reason I consider it a bug is that it contradicts the very basic intuition that replacing a literal with an expression producing it has identical behavior.
For example, "z = #{3*4}" vs r = 12; "z = #{r}".
While of course a much smaller area of exposition,
I think there is no reason to introduce this change (and I'm fairly confident it was not intended).
Thank you for the patch!
I would like to commit it then, unless there is an objection.
IMHO optimizations have to be correct, even in edge cases.
Such instructions are already a trade-off of duplication and complexity for speed improvement,
so this small change in size does not matter much to me.
Optimizations need to be totally transparent to the user to be called as such.
Patch by Eric Wong [ruby-core:78797].
I don't like the idea of making insns.def any bigger to support
a corner case, and "test_hash_aref_fstring_identity" shows
how contrived this is.