Bug #11672
refinement
Description
I found following script behaves different between ruby 2.2.3 and trunk.
Is this difference intentional?
% cat tst.rb class C end module R refine C do def m puts :foo end end end using R C.new.m module R refine C do def m puts :bar end end end C.new.m % ./ruby -wv tst.rb ruby 2.3.0dev (2015-11-10 trunk 52511) [x86_64-linux] foo foo % ruby-2.2.3 -wv tst.rb ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux] foo tst.rb:17: warning: method redefined; discarding old m tst.rb:6: warning: previous definition of m was here bar
Updated by akr (Akira Tanaka) about 5 years ago
- Assignee set to ko1 (Koichi Sasada)
I tried git bisect (make bisect).
It seems r51126 causes the problem.
% cat test.rb class C end module R refine C do def m :foo end end end using R C.new.m module R refine C do def m :bar end end end exit C.new.m == :bar % git bisect start 84694f3d9ba586b5087355f452bdeebaf7a8a77b f0806c4863c4440f9644ef0aea233739269ed45a % cd ../b/ruby % make bisect ... 5e8a147480f87f19a8b96ad3fb33a25fb4bb19b9 is the first bad commit commit 5e8a147480f87f19a8b96ad3fb33a25fb4bb19b9 Author: ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Fri Jul 3 11:24:50 2015 +0000 * method.h: introduce rb_callable_method_entry_t to remove rb_control_frame_t::klass. [Bug #11278], [Bug #11279] rb_method_entry_t data belong to modules/classes. rb_method_entry_t::owner points defined module or class. module M def foo; end end In this case, owner is M. rb_callable_method_entry_t data belong to only classes. For modules, MRI creates corresponding T_ICLASS internally. rb_callable_method_entry_t can also belong to T_ICLASS. rb_callable_method_entry_t::defined_class points T_CLASS or T_ICLASS. rb_method_entry_t data for classes (not for modules) are also rb_callable_method_entry_t data because it is completely same data. In this case, rb_method_entry_t::owner == rb_method_entry_t::defined_class. For example, there are classes C and D, and incldues M, class C; include M; end class D; include M; end then, two T_ICLASS objects for C's super class and D's super class will be created. When C.new.foo is called, then M#foo is searcheed and rb_callable_method_t data is used by VM to invoke M#foo. rb_method_entry_t data is only one for M#foo. However, rb_callable_method_entry_t data are two (and can be more). It is proportional to the number of including (and prepending) classes (the number of T_ICLASS which point to the module). Now, created rb_callable_method_entry_t are collected when the original module M was modified. We can think it is a cache. We need to select what kind of method entry data is needed. To operate definition, then you need to use rb_method_entry_t. You can access them by the following functions. * rb_method_entry(VALUE klass, ID id); * rb_method_entry_with_refinements(VALUE klass, ID id); * rb_method_entry_without_refinements(VALUE klass, ID id); * rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me); To invoke methods, then you need to use rb_callable_method_entry_t which you can get by the following APIs corresponding to the above listed functions. * rb_callable_method_entry(VALUE klass, ID id); * rb_callable_method_entry_with_refinements(VALUE klass, ID id); * rb_callable_method_entry_without_refinements(VALUE klass, ID id); * rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me); VM pushes rb_callable_method_entry_t, so that rb_vm_frame_method_entry() returns rb_callable_method_entry_t. You can check a super class of current method by rb_callable_method_entry_t::defined_class. * method.h: renamed from rb_method_entry_t::klass to rb_method_entry_t::owner. * internal.h: add rb_classext_struct::callable_m_tbl to cache rb_callable_method_entry_t data. We need to consider abotu this field again because it is only active for T_ICLASS. * class.c (method_entry_i): ditto. * class.c (rb_define_attr): rb_method_entry() does not takes defiend_class_ptr. * gc.c (mark_method_entry): mark RCLASS_CALLABLE_M_TBL() for T_ICLASS. * cont.c (fiber_init): rb_control_frame_t::klass is removed. * proc.c: fix `struct METHOD' data structure because rb_callable_method_t has all information. * vm_core.h: remove several fields. * rb_control_frame_t::klass. * rb_block_t::klass. And catch up changes. * eval.c: catch up changes. * gc.c: ditto. * insns.def: ditto. * vm.c: ditto. * vm_args.c: ditto. * vm_backtrace.c: ditto. * vm_dump.c: ditto. * vm_eval.c: ditto. * vm_insnhelper.c: ditto. * vm_method.c: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e :100644 100644 5e304ff045633044f55be6015844de32c192bb92 82374795fcad7cca0fc321292f8a93be565d92dd M ChangeLog :100644 100644 6207fb7c5a32d634d47048905ea9ba06f1f80412 0521cf96c3073afe89ff3e53a274df25879c859a M class.c :100644 100644 19e8122c83d1fa2bd4a85ea31dca9125af329e84 0ad91619bc919ae5ffdd3e5489cc464eae67e2dc M cont.c :100644 100644 3cc9a2efe993cfd8f491888e60535e3760429eae 3462a802d4c9b66fbbe5a6a04e2b70a2fe6110b9 M eval.c :100644 100644 550c11767fffe75c0791012cadfa1b08adf919ef 31a5d1378d5756cb0e4beb8c4b9eef94d77c2d26 M gc.c :100644 100644 70b4cfb69eed96b83c4eafb9535926841fabc670 c2697c60d56acad8625168dadf7d581f85339fc5 M insns.def :100644 100644 32ad346e07ad8c9bf8838dcba0d0957075104b7e 86bf5fd9ccb234c0dbe4b1033def353fe8accb90 M internal.h :100644 100644 f66a384d7dc325f305eded052251ac1a716bfd0f 2a75c810c0afcb625937313909e094ce5c26e34f M method.h :100644 100644 adb9f674a2b635d696d271a1131fc66b2e3db2bc 15a5cef433f746d22889a6a7a66632da261fe61e M proc.c :100644 100644 ad0d8505f7aaed1c1555ac16871397e7aace5b02 ba4cf3e9f87f275a8e9b7c3ea9079b11466e5f03 M vm.c :100644 100644 e466b10bcee84145f2bf9ec1505e72549aa3acae bea9d59b7a77dff96a84ff94e22cf5aa068f44ce M vm_args.c :100644 100644 801bf1100c2553a7411ec9b40e7a9323683caaa6 99d3e7f3d3b9ff589408acda31d68ff4dac61876 M vm_backtrace.c :100644 100644 72a1b2d90813bfa2d1c13a2d0bf5dc969f054434 58179ffbef34d8a32686898c19ce0f57b615fd30 M vm_core.h :100644 100644 be8feb179446a70b4b0d2d85a98d62afd33f0ae1 edd8176f9664f0275697d035ffada1ef17459cfc M vm_dump.c :100644 100644 bbc766617ce577411d1fb4090f43bbc7094dfe27 c63cffb15e8587f809f1deb0ba7ca9ae6334d4d9 M vm_eval.c :100644 100644 76f5bbe42a7482ffc9e999aba9763027092180b0 639efeef1c554a96d188458ad72ba3df123968a2 M vm_insnhelper.c :100644 100644 d9c0b4fc977a1abd1ecb8f993cee0a540a2460fd 0ae14c833a4f92bc82a8d4d4e0fc8a2e0a4091d7 M vm_method.c bisect run success
Updated by ko1 (Koichi Sasada) about 5 years ago
- Status changed from Open to Closed
Applied in changeset r52648.
- vm_method.c (rb_class_clear_method_cache): should clear all RCLASS_CALLABLE_M_TBLs of all sub-classes (T_ICLASS).
RCLASS_CALLABLE_M_TBL() caches complemented method entries.
It should be cleared when the modules are cleared.
On previous version clears only for direct children.
It is enough for normal modules because corresponding T_ICLASSes
are direct children.
However, refinements create complex data structure. So that
we need to clear all children (and descendants).
[ruby-core:71423] [Bug #11672]
vm_method.c (rb_clear_method_cache_by_class): rb_mKernel
doesn't call rb_class_clear_method_cache, so that
clear child T_ICLASSes.test/ruby/test_refinement.rb: enable disabled test.