From 622193d32a5ca27074acd072b655e4037f845dec Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 8 Apr 2015 04:01:06 +0000 Subject: hash.c: compare symbols by identities * hash.c (rb_any_hash): Symbols are compared by the identities always. [ruby-core:68767] [Bug #11035] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50182 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ hash.c | 3 +++ test/ruby/test_symbol.rb | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/ChangeLog b/ChangeLog index 39d44ec6e5..c647f30964 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Apr 8 13:01:06 2015 Nobuyoshi Nakada + + * hash.c (rb_any_hash): Symbols are compared by the identities + always. [ruby-core:68767] [Bug #11035] + Tue Apr 7 10:22:51 2015 SHIBATA Hiroshi * internal.h: fix typo. Patch by @sferik [fix GH-865] diff --git a/hash.c b/hash.c index 463c7c8e94..122abb3ea8 100644 --- a/hash.c +++ b/hash.c @@ -149,6 +149,9 @@ rb_any_hash(VALUE a) else if (BUILTIN_TYPE(a) == T_STRING) { hnum = rb_str_hash(a); } + else if (BUILTIN_TYPE(a) == T_SYMBOL) { + hnum = rb_objid_hash((st_index_t)a); + } else if (BUILTIN_TYPE(a) == T_FLOAT) { return rb_dbl_hash(rb_float_value(a)); } diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index bfdfe5245b..7f278ad4d7 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -238,4 +238,23 @@ class TestSymbol < Test::Unit::TestCase 200_000.times { |i| i.to_s.to_sym } end; end + + def test_hash_redefinition + assert_separately([], <<-'end;') + bug11035 = '[ruby-core:68767] [Bug #11035]' + class Symbol + def hash + raise + end + end + + h = {} + assert_nothing_raised(RuntimeError, bug11035) { + h[:foo] = 1 + } + assert_nothing_raised(RuntimeError, bug11035) { + h['bar'.to_sym] = 2 + } + end + end; end -- cgit v1.2.3