ruby-changes:70762
From: Jeremy <ko1@a...>
Date: Fri, 7 Jan 2022 01:03:50 +0900 (JST)
Subject: [ruby-changes:70762] a79c59472d (master): Allow include before calling Module#initialize
https://git.ruby-lang.org/ruby.git/commit/?id=a79c59472d From a79c59472df38297c246b27713c277f2edaefa7a Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Wed, 5 Jan 2022 16:12:31 -0800 Subject: 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(-) diff --git a/class.c b/class.c index 1281a243ab1..5ece9e34e82 100644 --- a/class.c +++ b/class.c @@ -912,6 +912,7 @@ rb_module_s_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/class.c#L912 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 2c9cbe7403d..9243df55873 100644 --- a/object.c +++ b/object.c @@ -1689,7 +1689,6 @@ static VALUE rb_mod_initialize_exec(VALUE module); https://github.com/ruby/ruby/blob/trunk/object.c#L1689 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 e1524a5d815..b84a090fce7 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -519,6 +519,16 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L519 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) -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/