Project

General

Profile

Bug #17048

Updated by alanwu (Alan Wu) over 4 years ago

 

 Here's a repro script 
 ```ruby 
 loop do 
   m = Module.new do 
     prepend Module.new 
     def hello 
     end 
   end 

   klass = Class.new { include m } 
   m.send(:initialize_copy, Module.new) 
   GC.start 

   klass.new.hello rescue nil 
 end 
 ``` 

 Here's a script that shows that it has broken semantics even 
 when it happens to not crash. 

 ```ruby 
 module A 
 end 

 class B 
   include A 
 end 

 module C 
   Const = :C 
 end 

 module D 
   Const = :D 
 end 

 A.send(:initialize_copy, C) 
 p B::Const # :C, makes sense 
 A.send(:initialize_copy, D) 
 p B::Const # :D, makes sense 
 A.send(:initialize_copy, Module.new) 
 p (begin B::Const rescue NameError; NameError 'NameError' end) # NameError, makes sense 
 A.send(:initialize_copy, C) 
 p B::Const # still NameErorr. Weird 
 ``` 
 This example shows that the problem exists [as far back as 2.0.0](https://wandbox.org/permlink/ASonJzNTUvxMdP8A). 

 I think the easiest way to fix this is to forbid calling `:initialize_copy` 
 on modules that have children. Another option is to try to decide on 
 the semantics of this. Though I don't think it's worth the effort as this 
 has been broken for a long time and people don't seem to to be using it. 
 Thoughts? 

Back