1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

hash.c: fix symbol hash

* hash.c (rb_sym_hash): return same value as rb_any_hash() of
  Symbol.  [Bug #9381]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51426 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-07-29 05:54:21 +00:00
parent efc7a3a712
commit 48f9012df9
4 changed files with 23 additions and 1 deletions

View file

@ -1,4 +1,7 @@
Wed Jul 29 14:53:32 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
Wed Jul 29 14:54:16 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* hash.c (rb_sym_hash): return same value as rb_any_hash() of
Symbol. [Bug #9381]
* hash.c (rb_any_hash): fix Float hash. rb_dbl_hash() returns a
Fixnum, but not a long. [Bug #9381]

15
hash.c
View file

@ -129,6 +129,21 @@ rb_hash(VALUE obj)
long rb_objid_hash(st_index_t index);
VALUE
rb_sym_hash(VALUE sym)
{
st_index_t hnum;
if (STATIC_SYM_P(sym)) {
sym >>= (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
hnum = rb_objid_hash((st_index_t)sym);
}
else {
hnum = RSYMBOL(sym)->hashval;
}
return LONG2FIX(hnum);
}
static st_index_t
rb_any_hash(VALUE a)
{

View file

@ -9149,6 +9149,8 @@ rb_to_symbol(VALUE name)
return rb_str_intern(name);
}
VALUE rb_sym_hash(VALUE);
/*
* A <code>String</code> object holds and manipulates an arbitrary sequence of
* bytes, typically representing characters. String objects may be created
@ -9311,6 +9313,7 @@ Init_String(void)
rb_undef_method(CLASS_OF(rb_cSymbol), "new");
rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0); /* in symbol.c */
rb_define_method(rb_cSymbol, "hash", rb_sym_hash, 0); /* in hash.c */
rb_define_method(rb_cSymbol, "==", sym_equal, 1);
rb_define_method(rb_cSymbol, "===", sym_equal, 1);
rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);

View file

@ -1282,6 +1282,7 @@ class TestHash < Test::Unit::TestCase
bad = [
5, true, false, nil,
0.0, 1.72723e-77,
:foo, "dsym_#{self.object_id.to_s(16)}_#{Time.now.to_i.to_s(16)}".to_sym,
].select do |x|
hash = {x => bug9381}
hash[wrapper.new(x)] != bug9381