diff options
author | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-12-13 17:48:12 +0000 |
---|---|---|
committer | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-12-13 17:48:12 +0000 |
commit | 5164ba53f2b17ead1e0c471652012ed4557b4b8d (patch) | |
tree | f159b1623071ec1f6dd58fc096be4ffe06b99542 | |
parent | 6de1532775ec9802acc71c2c8b21794309b479b9 (diff) |
merge revision(s) 25659:25661:
* 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/branches/ruby_1_8_7@26081 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 | ||||
-rw-r--r-- | version.h | 2 |
4 files changed, 34 insertions, 6 deletions
@@ -1,3 +1,11 @@ +Mon Dec 14 02:27:32 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. + Mon Dec 14 00:42:55 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> * string.c (rb_str_inspect): get rid of adding garbage to shor @@ -81,7 +81,19 @@ VALUE rb_hash(obj) 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 @@ -102,10 +114,7 @@ rb_any_hash(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 4131a6bdd3..71cf8dab93 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -638,4 +638,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 @@ -2,7 +2,7 @@ #define RUBY_RELEASE_DATE "2009-12-14" #define RUBY_VERSION_CODE 187 #define RUBY_RELEASE_CODE 20091214 -#define RUBY_PATCHLEVEL 227 +#define RUBY_PATCHLEVEL 228 #define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MINOR 8 |