Skip to content

Commit

Permalink
* vm_insnhelper.c (vm_search_const_defined_class): search
Browse files Browse the repository at this point in the history
  ancestors only when global scope.  [ruby-core:39227] [Bug ruby#5264]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33163 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Sep 2, 2011
1 parent 5e14b97 commit 7dddaf6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
@@ -1,3 +1,8 @@
Fri Sep 2 14:36:47 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>

* 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@ruby-lang.org>

* parse.y (parser_tokadd_string, parser_yylex): ignore a backslash
Expand Down
10 changes: 4 additions & 6 deletions insns.def
Expand Up @@ -895,7 +895,6 @@ defineclass
(VALUE val)
{
VALUE klass;
int newclass = 1;

switch ((int)define_type) {
case 0: /* scoped: class Foo::Bar */
Expand All @@ -904,16 +903,15 @@ defineclass

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));
}
Expand Down Expand Up @@ -949,8 +947,8 @@ defineclass
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));
Expand Down
28 changes: 19 additions & 9 deletions test/ruby/test_module.rb
Expand Up @@ -548,15 +548,25 @@ class Bar; include Foo; end
def test_const_in_module
bug3423 = '[ruby-core:37698]'
assert_in_out_err([], <<-INPUT, %w[ok], [], bug3423)
module LangModuleSpecInObject
module LangModuleTop
end
end
include LangModuleSpecInObject
module LangModuleTop
end
puts "ok" if LangModuleSpecInObject::LangModuleTop == LangModuleTop
INPUT
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

def test_class_variable_get
Expand Down
17 changes: 10 additions & 7 deletions vm_insnhelper.c
Expand Up @@ -1253,15 +1253,18 @@ vm_get_cvar_base(NODE *cref)
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
Expand Down

0 comments on commit 7dddaf6

Please sign in to comment.