mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
Merged r22308. [ruby-dev:39637]
* 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@25661 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
60a40ecaf6
commit
ee3e66e7a6
4 changed files with 32 additions and 12 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,8 +1,10 @@
|
|||
Thu Nov 5 20:49:47 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
||||
Sun Feb 15 03:50:21 2009 Yusuke Endoh <mame@tsg.ne.jp>
|
||||
|
||||
* gc.c (rb_obj_id): a Fixnum which is beyond signed long
|
||||
was converted to a Bignum in calculating its hash.
|
||||
[ruby-dev:39637]
|
||||
* 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.
|
||||
|
||||
Thu Nov 5 12:06:35 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
||||
|
||||
|
|
2
gc.c
2
gc.c
|
@ -2142,7 +2142,7 @@ rb_obj_id(obj)
|
|||
return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;
|
||||
}
|
||||
if (SPECIAL_CONST_P(obj)) {
|
||||
return LONG2FIX((long)obj);
|
||||
return LONG2NUM((long)obj);
|
||||
}
|
||||
return (VALUE)((long)obj|FIXNUM_FLAG);
|
||||
}
|
||||
|
|
19
hash.c
19
hash.c
|
@ -83,7 +83,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
|
||||
|
@ -104,10 +116,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;
|
||||
|
|
|
@ -637,7 +637,16 @@ class TestHash < Test::Unit::TestCase
|
|||
|
||||
def test_hash_hash
|
||||
assert_equal({0=>2,11=>1}.hash, {11=>1,0=>2}.hash)
|
||||
assert_equal({:a=>2**29}.hash, {:a=>2**29}.hash)
|
||||
assert_equal({:a=>2**61}.hash, {:a=>2**61}.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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue