From 1fd41dce10a93ce89a098ffc33f40d5243f5e140 Mon Sep 17 00:00:00 2001 From: nagachika Date: Thu, 8 Dec 2011 14:47:19 +0000 Subject: [PATCH] * variable.c (set_const_visibility): clear inine-cache when constant's visibility is modified. [ruby-dev:44929] * test/ruby/test_module.rb (test_private_constants_clear_inlinecache): add test for it. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33987 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ test/ruby/test_module.rb | 21 +++++++++++++++++++++ variable.c | 12 +++++++++--- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0ed53c3aa9..84525843fa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Thu Dec 8 23:38:24 2011 CHIKANAGA Tomoyuki + + * variable.c (set_const_visibility): clear inine-cache when constant's + visibility is modified. [ruby-dev:44929] + + * test/ruby/test_module.rb (test_private_constants_clear_inlinecache): + add test for it. + Thu Dec 8 23:26:11 2011 NAKAMURA Usaku * ext/extmk.rb (extract_makefile): should sort after map, not before diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index 16485c30d1..16f9850c18 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -1130,6 +1130,27 @@ class TestModule < Test::Unit::TestCase assert_in_out_err([], src, %w(Object :ok), []) end + def test_private_constants_clear_inlinecache + bug5702 = '[ruby-dev:44929]' + src = <<-INPUT + class A + C = :Const + def self.get_C + A::C + end + # fill cache + A.get_C + private_constant :C, :D rescue nil + begin + A.get_C + rescue NameError + puts "A.get_C" + end + end + INPUT + assert_in_out_err([], src, %w(A.get_C), [], bug5702) + end + def test_constant_lookup_in_method_defined_by_class_eval src = <<-INPUT class A diff --git a/variable.c b/variable.c index 7f8988c579..0dec1aa285 100644 --- a/variable.c +++ b/variable.c @@ -2119,13 +2119,19 @@ set_const_visibility(VALUE mod, int argc, VALUE *argv, rb_const_flag_t flag) VALUE val = argv[i]; id = rb_check_id(&val); if (!id) { + if ( i > 0 ) + rb_clear_cache_by_class(mod); rb_name_error_str(val, "constant %s::%s not defined", rb_class2name(mod), RSTRING_PTR(val)); } - if (RCLASS_CONST_TBL(mod) && st_lookup(RCLASS_CONST_TBL(mod), (st_data_t)id, &v)) { + if (RCLASS_CONST_TBL(mod) && + st_lookup(RCLASS_CONST_TBL(mod), (st_data_t)id, &v)) { ((rb_const_entry_t*)v)->flag = flag; - continue; } - rb_name_error(id, "constant %s::%s not defined", rb_class2name(mod), rb_id2name(id)); + else { + if ( i > 0 ) + rb_clear_cache_by_class(mod); + rb_name_error(id, "constant %s::%s not defined", rb_class2name(mod), rb_id2name(id)); + } } rb_clear_cache_by_class(mod); }