ruby-changes:10744
From: mame <ko1@a...>
Date: Sun, 15 Feb 2009 03:53:54 +0900 (JST)
Subject: [ruby-changes:10744] Ruby:r22308 (trunk): * hash.c (rb_hash): always return a fixnum value because a return
mame 2009-02-15 03:53:40 +0900 (Sun, 15 Feb 2009) New Revision: 22308 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22308 Log: * hash.c (rb_hash): always return a fixnum value because a return value of rb_hash may be used as a hash value itself and bignums have no unique VALUE. * test/ruby/test_hash.rb: add a test for above. Modified files: trunk/ChangeLog trunk/hash.c trunk/test/ruby/test_hash.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 22307) +++ ChangeLog (revision 22308) @@ -1,3 +1,11 @@ +Sun Feb 15 03:50:21 2009 Yusuke Endoh <mame@t...> + + * hash.c (rb_hash): always return a fixnum value because a return + value of rb_hash may be used as a hash value itself and bignums have + no unique VALUE. + + * test/ruby/test_hash.rb: add a test for above. + Sun Feb 15 00:45:41 2009 Nobuyoshi Nakada <nobu@r...> * array.c (rb_ary_uniq_bang, rb_ary_uniq): unique by the result of Index: hash.c =================================================================== --- hash.c (revision 22307) +++ hash.c (revision 22308) @@ -57,7 +57,19 @@ VALUE rb_hash(VALUE obj) { - return rb_funcall(obj, id_hash, 0); + VALUE hval = rb_funcall(obj, id_hash, 0); + retry: + switch (TYPE(hval)) { + case T_FIXNUM: + return hval; + + case T_BIGNUM: + return LONG2FIX(((long*)(RBIGNUM_DIGITS(hval)))[0]); + + default: + hval = rb_to_int(hval); + goto retry; + } } static int @@ -80,10 +92,7 @@ break; default: - hval = rb_funcall(a, id_hash, 0); - if (!FIXNUM_P(hval)) { - hval = rb_funcall(hval, '%', 1, INT2FIX(536870923)); - } + hval = rb_hash(a); hnum = (int)FIX2LONG(hval); } hnum <<= 1; Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 22307) +++ test/ruby/test_hash.rb (revision 22308) @@ -849,4 +849,15 @@ def test_hash_hash assert_equal({0=>2,11=>1}.hash, {11=>1,0=>2}.hash) end + + def test_hash_bignum_hash + x = 2<<(32-3)-1 + assert_equal({x=>1}.hash, {x=>1}.hash) + x = 2<<(64-3)-1 + assert_equal({x=>1}.hash, {x=>1}.hash) + + o = Object.new + def o.hash; 2<<100; end + assert_equal({x=>1}.hash, {x=>1}.hash) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/