From 28ec24db4a8201fa624692cdc1cbef9b00489412 Mon Sep 17 00:00:00 2001 From: "NARUSE, Yui" Date: Sun, 30 Jan 2022 19:08:49 +0900 Subject: merge revision(s) a79c59472df38297c246b27713c277f2edaefa7a: [Backport #18292] 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] --- class.c | 1 + object.c | 1 - test/ruby/test_module.rb | 10 ++++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) --- class.c | 1 + object.c | 1 - test/ruby/test_module.rb | 10 ++++++++++ version.h | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/class.c b/class.c index f83a16a08e..5d3d46d1e6 100644 --- a/class.c +++ b/class.c @@ -913,6 +913,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; } diff --git a/object.c b/object.c index 430f7eafd0..3b83c7d08a 100644 --- a/object.c +++ b/object.c @@ -1800,7 +1800,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); } diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index e1524a5d81..b84a090fce 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -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) diff --git a/version.h b/version.h index 5b20907ec7..2033b4b3e9 100644 --- a/version.h +++ b/version.h @@ -11,7 +11,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 5 +#define RUBY_PATCHLEVEL 6 #define RUBY_RELEASE_YEAR 2022 #define RUBY_RELEASE_MONTH 1 -- cgit v1.2.3