From 48f9012df931de3a526db7d3ce03c408a2c11ba5 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 29 Jul 2015 05:54:21 +0000 Subject: 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 --- ChangeLog | 5 ++++- hash.c | 15 +++++++++++++++ string.c | 3 +++ test/ruby/test_hash.rb | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 35a8665120..8a431c7add 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ -Wed Jul 29 14:53:32 2015 Nobuyoshi Nakada +Wed Jul 29 14:54:16 2015 Nobuyoshi Nakada + + * 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] diff --git a/hash.c b/hash.c index 9ae6d9564f..e852570183 100644 --- a/hash.c +++ b/hash.c @@ -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) { diff --git a/string.c b/string.c index bf07d97c1f..0a8f4e1d9c 100644 --- a/string.c +++ b/string.c @@ -9149,6 +9149,8 @@ rb_to_symbol(VALUE name) return rb_str_intern(name); } +VALUE rb_sym_hash(VALUE); + /* * A String 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); diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index d4837ca6cf..c37e072e21 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -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 -- cgit v1.2.3