ruby-changes:21114
From: nobu <ko1@a...>
Date: Fri, 2 Sep 2011 14:38:30 +0900 (JST)
Subject: [ruby-changes:21114] nobu:r33163 (ruby_1_9_3, trunk): * vm_insnhelper.c (vm_search_const_defined_class): search
nobu 2011-09-02 14:36:49 +0900 (Fri, 02 Sep 2011) New Revision: 33163 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33163 Log: * vm_insnhelper.c (vm_search_const_defined_class): search ancestors only when global scope. [ruby-core:39227] [Bug #5264] Modified files: branches/ruby_1_9_3/ChangeLog branches/ruby_1_9_3/insns.def branches/ruby_1_9_3/test/ruby/test_module.rb branches/ruby_1_9_3/vm_insnhelper.c trunk/ChangeLog trunk/insns.def trunk/test/ruby/test_module.rb trunk/vm_insnhelper.c Index: ChangeLog =================================================================== --- ChangeLog (revision 33162) +++ ChangeLog (revision 33163) @@ -1,3 +1,8 @@ +Fri Sep 2 14:36:47 2011 Nobuyoshi Nakada <nobu@r...> + + * vm_insnhelper.c (vm_search_const_defined_class): search + ancestors only when global scope. [ruby-core:39227] [Bug #5264] + Fri Sep 2 09:58:08 2011 Nobuyoshi Nakada <nobu@r...> * parse.y (parser_tokadd_string, parser_yylex): ignore a backslash Index: insns.def =================================================================== --- insns.def (revision 33162) +++ insns.def (revision 33163) @@ -895,7 +895,6 @@ (VALUE val) { VALUE klass; - int newclass = 1; switch ((int)define_type) { case 0: /* scoped: class Foo::Bar */ @@ -904,16 +903,15 @@ if (super == Qnil) { super = rb_cObject; - newclass = 0; } vm_check_if_namespace(cbase); /* find klass */ rb_autoload_load(cbase, id); - if (vm_const_defined_at(cbase, id, newclass)) { + if ((klass = vm_search_const_defined_class(cbase, id)) != 0) { /* already exist */ - klass = define_type == 0 ? rb_public_const_get(cbase, id) : rb_const_get_from(cbase, id); + klass = define_type == 0 ? rb_public_const_get_at(klass, id) : rb_const_get_at(klass, id); if (TYPE(klass) != T_CLASS) { rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id)); } @@ -949,8 +947,8 @@ vm_check_if_namespace(cbase); /* find klass */ - if (vm_const_defined_at(cbase, id, 0)) { - klass = define_type == 2 ? rb_public_const_get(cbase, id) : rb_const_get_from(cbase, id); + if ((klass = vm_search_const_defined_class(cbase, id)) != 0) { + klass = define_type == 2 ? rb_public_const_get_at(klass, id) : rb_const_get_at(klass, id); /* already exist */ if (TYPE(klass) != T_MODULE) { rb_raise(rb_eTypeError, "%s is not a module", rb_id2name(id)); Index: vm_insnhelper.c =================================================================== --- vm_insnhelper.c (revision 33162) +++ vm_insnhelper.c (revision 33163) @@ -1253,15 +1253,18 @@ return klass; } -static int -vm_const_defined_at(VALUE cbase, ID id, int newclass) +static VALUE +vm_search_const_defined_class(const VALUE cbase, ID id) { - int ret = rb_const_defined_at(cbase, id); - if (!ret && !newclass) { - while ((cbase = RCLASS_SUPER(cbase)) != 0 && cbase != rb_cObject && - !(ret = rb_const_defined_at(cbase, id))); + if (rb_const_defined_at(cbase, id)) return cbase; + if (cbase == rb_cObject) { + VALUE tmp = RCLASS_SUPER(cbase); + while (tmp) { + if (rb_const_defined_at(tmp, id)) return tmp; + tmp = RCLASS_SUPER(tmp); + } } - return ret; + return 0; } #ifndef USE_IC_FOR_IVAR Index: test/ruby/test_module.rb =================================================================== --- test/ruby/test_module.rb (revision 33162) +++ test/ruby/test_module.rb (revision 33163) @@ -548,16 +548,26 @@ def test_const_in_module bug3423 = '[ruby-core:37698]' assert_in_out_err([], <<-INPUT, %w[ok], [], bug3423) -module LangModuleSpecInObject - module LangModuleTop + module LangModuleSpecInObject + module LangModuleTop + end + end + include LangModuleSpecInObject + module LangModuleTop + end + puts "ok" if LangModuleSpecInObject::LangModuleTop == LangModuleTop + INPUT + + bug5264 = '[ruby-core:39227]' + assert_in_out_err([], <<-'INPUT', [], [], bug5264) + class A + class X; end + end + class B < A + module X; end + end + INPUT end -end -include LangModuleSpecInObject -module LangModuleTop -end -puts "ok" if LangModuleSpecInObject::LangModuleTop == LangModuleTop -INPUT - end def test_class_variable_get c = Class.new Index: ruby_1_9_3/ChangeLog =================================================================== --- ruby_1_9_3/ChangeLog (revision 33162) +++ ruby_1_9_3/ChangeLog (revision 33163) @@ -1,3 +1,8 @@ +Fri Sep 2 14:36:47 2011 Nobuyoshi Nakada <nobu@r...> + + * vm_insnhelper.c (vm_search_const_defined_class): search + ancestors only when global scope. [ruby-core:39227] [Bug #5264] + Fri Sep 2 09:58:08 2011 Nobuyoshi Nakada <nobu@r...> * parse.y (parser_tokadd_string, parser_yylex): ignore a backslash Index: ruby_1_9_3/insns.def =================================================================== --- ruby_1_9_3/insns.def (revision 33162) +++ ruby_1_9_3/insns.def (revision 33163) @@ -895,7 +895,6 @@ (VALUE val) { VALUE klass; - int newclass = 1; switch ((int)define_type) { case 0: /* scoped: class Foo::Bar */ @@ -904,16 +903,15 @@ if (super == Qnil) { super = rb_cObject; - newclass = 0; } vm_check_if_namespace(cbase); /* find klass */ rb_autoload_load(cbase, id); - if (vm_const_defined_at(cbase, id, newclass)) { + if ((klass = vm_search_const_defined_class(cbase, id)) != 0) { /* already exist */ - klass = define_type == 0 ? rb_public_const_get(cbase, id) : rb_const_get_from(cbase, id); + klass = define_type == 0 ? rb_public_const_get_at(klass, id) : rb_const_get_at(klass, id); if (TYPE(klass) != T_CLASS) { rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id)); } @@ -949,8 +947,8 @@ vm_check_if_namespace(cbase); /* find klass */ - if (vm_const_defined_at(cbase, id, 0)) { - klass = define_type == 2 ? rb_public_const_get(cbase, id) : rb_const_get_from(cbase, id); + if ((klass = vm_search_const_defined_class(cbase, id)) != 0) { + klass = define_type == 2 ? rb_public_const_get_at(klass, id) : rb_const_get_at(klass, id); /* already exist */ if (TYPE(klass) != T_MODULE) { rb_raise(rb_eTypeError, "%s is not a module", rb_id2name(id)); Index: ruby_1_9_3/vm_insnhelper.c =================================================================== --- ruby_1_9_3/vm_insnhelper.c (revision 33162) +++ ruby_1_9_3/vm_insnhelper.c (revision 33163) @@ -1252,15 +1252,18 @@ return klass; } -static int -vm_const_defined_at(VALUE cbase, ID id, int newclass) +static VALUE +vm_search_const_defined_class(const VALUE cbase, ID id) { - int ret = rb_const_defined_at(cbase, id); - if (!ret && !newclass) { - while ((cbase = RCLASS_SUPER(cbase)) != 0 && cbase != rb_cObject && - !(ret = rb_const_defined_at(cbase, id))); + if (rb_const_defined_at(cbase, id)) return cbase; + if (cbase == rb_cObject) { + VALUE tmp = RCLASS_SUPER(cbase); + while (tmp) { + if (rb_const_defined_at(tmp, id)) return tmp; + tmp = RCLASS_SUPER(tmp); + } } - return ret; + return 0; } #ifndef USE_IC_FOR_IVAR Index: ruby_1_9_3/test/ruby/test_module.rb =================================================================== --- ruby_1_9_3/test/ruby/test_module.rb (revision 33162) +++ ruby_1_9_3/test/ruby/test_module.rb (revision 33163) @@ -535,16 +535,26 @@ def test_const_in_module bug3423 = '[ruby-core:37698]' assert_in_out_err([], <<-INPUT, %w[ok], [], bug3423) -module LangModuleSpecInObject - module LangModuleTop + module LangModuleSpecInObject + module LangModuleTop + end + end + include LangModuleSpecInObject + module LangModuleTop + end + puts "ok" if LangModuleSpecInObject::LangModuleTop == LangModuleTop + INPUT + + bug5264 = '[ruby-core:39227]' + assert_in_out_err([], <<-'INPUT', [], [], bug5264) + class A + class X; end + end + class B < A + module X; end + end + INPUT end -end -include LangModuleSpecInObject -module LangModuleTop -end -puts "ok" if LangModuleSpecInObject::LangModuleTop == LangModuleTop -INPUT - end def test_class_variable_get c = Class.new -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/