1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Allow include before calling Module#initialize

This is to allow Module subclasses that include modules before
calling super in the subclass's initialize.

Remove rb_module_check_initializable from Module#initialize.
Module#initialize only calls module_exec if a block is passed,
it doesn't have other issues that would cause problems if
called multiple times or with an already initialized module.

Move initialization of super to Module#allocate, though I'm not
sure it is required there.  However, it's needed to be removed
from Module#initialize for this to work.

Fixes [Bug #18292]
This commit is contained in:
Jeremy Evans 2022-01-05 16:12:31 -08:00
parent 73be7a85cd
commit a79c59472d
Notes: git 2022-01-07 01:04:00 +09:00
3 changed files with 11 additions and 1 deletions

View file

@ -912,6 +912,7 @@ rb_module_s_alloc(VALUE klass)
VALUE mod = class_alloc(T_MODULE, klass);
RCLASS_M_TBL_INIT(mod);
FL_SET(mod, RMODULE_ALLOCATED_BUT_NOT_INITIALIZED);
RB_OBJ_WRITE(mod, &RCLASS(mod)->super, 0);
return mod;
}

View file

@ -1689,7 +1689,6 @@ static VALUE rb_mod_initialize_exec(VALUE module);
static VALUE
rb_mod_initialize(VALUE module)
{
rb_module_check_initializable(module);
return rb_mod_initialize_exec(module);
}

View file

@ -519,6 +519,16 @@ class TestModule < Test::Unit::TestCase
assert_raise(ArgumentError) { Module.new { include } }
end
def test_include_before_initialize
m = Class.new(Module) do
def initialize(...)
include Enumerable
super
end
end.new
assert_equal(true, m < Enumerable)
end
def test_prepend_self
m = Module.new
assert_equal([m], m.ancestors)