diff options
author | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-02-14 18:53:40 +0000 |
---|---|---|
committer | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-02-14 18:53:40 +0000 |
commit | a48f90b05b654cc98727a91c5fd9952c5a86221c (patch) | |
tree | 1f1c3bfccf90207a542fce694d9040315457bfe1 | |
parent | 766df600db46f3cc8a2bd0c0af2dfceeec2d8fb3 (diff) | |
download | ruby-a48f90b05b654cc98727a91c5fd9952c5a86221c.tar.gz |
* 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.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22308 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | hash.c | 19 | ||||
-rw-r--r-- | test/ruby/test_hash.rb | 11 |
3 files changed, 33 insertions, 5 deletions
@@ -1,3 +1,11 @@ +Sun Feb 15 03:50:21 2009 Yusuke Endoh <mame@tsg.ne.jp> + + * 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@ruby-lang.org> * array.c (rb_ary_uniq_bang, rb_ary_uniq): unique by the result of @@ -57,7 +57,19 @@ rb_any_cmp(VALUE a, VALUE b) 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 @@ rb_any_hash(VALUE a) 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; diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 5a17ac4fb5..50a34b8160 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -849,4 +849,15 @@ class TestHash < Test::Unit::TestCase 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 |