Bug #9202

Array#uniq freezes duplicate strings

Added by Eric Hodel almost 2 years ago. Updated over 1 year ago.

[ruby-core:58809]
Status:Closed
Priority:Normal
Assignee:Masaki Matsushita
ruby -v:ruby 2.1.0dev (2013-12-02 trunk 43958) [x86_64-darwin13.0] Backport:1.9.3: DONTNEED, 2.0.0: DONTNEED

Description

I imagine this is related to the recent frozen string changes. This behavior change in Array#uniq breaks RubyGems:

$ cat test.rb
p unique: %w[a].uniq.any? { |item| item.frozen? }
p duplicate: %w[a a].uniq.any? { |item| item.frozen? }
$ ruby -v test.rb
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
{:unique=>false}
{:duplicate=>false}
$ make runruby
./miniruby -I../trunk/lib -I. -I.ext/common ../trunk/tool/runruby.rb --extout=.ext -- --disable-gems ../trunk/test.rb
{:unique=>false}
{:duplicate=>true}

0001-Array-uniq-uniq-does-not-return-frozen-hash-keys.patch Magnifier (3.68 KB) Eric Wong, 12/03/2013 12:10 PM

Associated revisions

Revision 43969
Added by glass almost 2 years ago

  • array.c (ary_add_hash): set and return values because string keys
    will be frozen. [Bug #9202]

  • array.c (rb_ary_uniq_bang): ditto.

  • array.c (rb_ary_or): ditto.

  • array.c (rb_ary_uniq): ditto.

  • test/ruby/test_array.rb: tests for above.

The patch is from normalperson (Eric Wong).

Revision 43969
Added by glass almost 2 years ago

  • array.c (ary_add_hash): set and return values because string keys
    will be frozen. [Bug #9202]

  • array.c (rb_ary_uniq_bang): ditto.

  • array.c (rb_ary_or): ditto.

  • array.c (rb_ary_uniq): ditto.

  • test/ruby/test_array.rb: tests for above.

The patch is from normalperson (Eric Wong).

History

#1 Updated by Eric Hodel almost 2 years ago

  • ruby -v changed from ruby 2.0.0dev (2012-11-29 trunk 37968) [x86_64-darwin12.2.0] to ruby 2.1.0dev (2013-12-02 trunk 43958) [x86_64-darwin13.0]

#2 Updated by Eric Wong almost 2 years ago

It's not r43870 (Use rb_fstring() to de-duplicate hash string keys)
Bisecting now...

#3 Updated by Eric Wong almost 2 years ago

bisected to r43194:

 * array.c (rb_ary_uniq): use rb_hash_keys().

 * internal.h: define rb_hash_keys() as internal API.

 * hash.c (rb_hash_keys): ditto.

#4 Updated by Eric Wong almost 2 years ago

This should fix it, can you confirm/? Thanks

#5 Updated by Masaki Matsushita over 1 year ago

  • Assignee changed from Charlie Somerville to Masaki Matsushita

I confirmed it. Thank you for your patch!

#6 Updated by Anonymous over 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r43969.
Eric, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • array.c (ary_add_hash): set and return values because string keys
    will be frozen. [Bug #9202]

  • array.c (rb_ary_uniq_bang): ditto.

  • array.c (rb_ary_or): ditto.

  • array.c (rb_ary_uniq): ditto.

  • test/ruby/test_array.rb: tests for above.

The patch is from normalperson (Eric Wong).

#7 Updated by Eric Wong over 1 year ago

"Glass_saga (Masaki Matsushita)" glass.saga@gmail.com wrote:

I confirmed it. Thank you for your patch!

No problem, thanks for the review.

Also, I think we may remove rb_hash_keys from internal.h and make it
static again.

Also available in: Atom PDF