Skip to content

Commit

Permalink
Use the flag for uninitialized module [Bug #18185]
Browse files Browse the repository at this point in the history
Make `Module#ancestors` not to include `BasicObject`.
  • Loading branch information
nobu committed Sep 23, 2021
1 parent 65285bf commit b929af4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
15 changes: 9 additions & 6 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,22 @@ copy_tables(VALUE clone, VALUE orig)

static bool ensure_origin(VALUE klass);

/**
* If this flag is set, that module is allocated but not initialized yet.
*/
enum {RMODULE_ALLOCATED_BUT_NOT_INITIALIZED = RUBY_FL_USER5};

static inline bool
RMODULE_UNINITIALIZED(VALUE module)
{
return RCLASS_SUPER(module) == rb_cBasicObject;
return FL_TEST_RAW(module, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
}

void
rb_module_set_initialized(VALUE mod)
{
if (RMODULE_UNINITIALIZED(mod)) {
RB_OBJ_WRITE(mod, &RCLASS(mod)->super, 0);
/* no more re-initialization */
}
FL_UNSET_RAW(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
/* no more re-initialization */
}

void
Expand Down Expand Up @@ -817,7 +820,7 @@ rb_module_s_alloc(VALUE klass)
{
VALUE mod = class_alloc(T_MODULE, klass);
RCLASS_M_TBL_INIT(mod);
RB_OBJ_WRITE(mod, &RCLASS(mod)->super, rb_cBasicObject);
FL_SET(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
return mod;
}

Expand Down
3 changes: 3 additions & 0 deletions test/ruby/test_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,9 @@ def initialize_copy(other)
class Bug18185 < Module
module InstanceMethods
end
attr_reader :ancestor_list
def initialize
@ancestor_list = ancestors
include InstanceMethods
end
class Foo
Expand All @@ -470,6 +472,7 @@ def test_module_subclass_initialize
assert_equal(1, anc.count(BasicObject), ->{anc.inspect})
b = c.new(key: 1)
assert_equal(1, b.key)
assert_not_include(mod.ancestor_list, BasicObject)
end

def test_dup
Expand Down

0 comments on commit b929af4

Please sign in to comment.