ruby-changes:48862
From: shugo <ko1@a...>
Date: Sat, 2 Dec 2017 19:54:44 +0900 (JST)
Subject: [ruby-changes:48862] shugo:r60980 (trunk): Modules should not have subclasses.
shugo 2017-12-02 19:54:39 +0900 (Sat, 02 Dec 2017) New Revision: 60980 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60980 Log: Modules should not have subclasses. When refining a module, the module was set to the superclass of its refinement, and a segmentation fault occurred. The superclass of the refinement should be an iclass of the module. [ruby-core:83617] [Bug #14070] Modified files: trunk/eval.c trunk/test/ruby/test_refinement.rb Index: test/ruby/test_refinement.rb =================================================================== --- test/ruby/test_refinement.rb (revision 60979) +++ test/ruby/test_refinement.rb (revision 60980) @@ -2036,6 +2036,25 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L2036 INPUT end + def test_refining_module_repeatedly + bug14070 = '[ruby-core:83617] [Bug #14070]' + assert_in_out_err([], <<-INPUT, ["ok"], [], bug14070) + 1000.times do + Class.new do + include Enumerable + end + + Module.new do + refine Enumerable do + def foo + end + end + end + end + puts "ok" + INPUT + end + private def eval_using(mod, s) Index: eval.c =================================================================== --- eval.c (revision 60979) +++ eval.c (revision 60980) @@ -1304,6 +1304,10 @@ rb_using_refinement(rb_cref_t *cref, VAL https://github.com/ruby/ruby/blob/trunk/eval.c#L1304 } } FL_SET(module, RMODULE_IS_OVERLAID); + if (RB_TYPE_P(superclass, T_MODULE)) { + superclass = rb_include_class_new(superclass, + RCLASS_SUPER(superclass)); + } c = iclass = rb_include_class_new(module, superclass); RCLASS_REFINED_CLASS(c) = klass; @@ -1398,6 +1402,10 @@ add_activated_refinement(VALUE activated https://github.com/ruby/ruby/blob/trunk/eval.c#L1402 } } FL_SET(refinement, RMODULE_IS_OVERLAID); + if (RB_TYPE_P(superclass, T_MODULE)) { + superclass = rb_include_class_new(superclass, + RCLASS_SUPER(superclass)); + } c = iclass = rb_include_class_new(refinement, superclass); RCLASS_REFINED_CLASS(c) = klass; refinement = RCLASS_SUPER(refinement); @@ -1453,7 +1461,12 @@ rb_mod_refine(VALUE module, VALUE klass) https://github.com/ruby/ruby/blob/trunk/eval.c#L1461 refinement = rb_hash_lookup(refinements, klass); if (NIL_P(refinement)) { refinement = rb_module_new(); - RCLASS_SET_SUPER(refinement, klass); + if (RB_TYPE_P(klass, T_MODULE)) { + rb_include_module(refinement, klass); + } + else { + RCLASS_SET_SUPER(refinement, klass); + } FL_SET(refinement, RMODULE_IS_REFINEMENT); CONST_ID(id_refined_class, "__refined_class__"); rb_ivar_set(refinement, id_refined_class, klass); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/