Issue #10765 has been updated by Seiei Higa.


If touch the `me` after `rb_unlink_method_entry`, it could cause error?

``` patch
diff --git vm_method.c vm_method.c
index 8ad2b72..41a311c 100644
--- vm_method.c
+++ vm_method.c
@@ -775,11 +775,13 @@ remove_method(VALUE klass, ID mid)
     key = (st_data_t)mid;
     st_delete(RCLASS_M_TBL(klass), &key, &data);

+    int keep_refined_method_entry = me->def->type == VM_METHOD_TYPE_REFINED;
+
     rb_vm_check_redefinition_opt_method(me, klass);
     rb_clear_method_cache_by_class(klass);
     rb_unlink_method_entry(me);

-    if (me->def->type == VM_METHOD_TYPE_REFINED) {
+    if (keep_refined_method_entry) {
 	rb_add_refined_method_entry(klass, mid);
     }
```

----------------------------------------
Bug #10765: Module#remove_method remove refined method entry.
https://bugs.ruby-lang.org/issues/10765#change-51370

* Author: Seiei Higa
* Status: Assigned
* Priority: Normal
* Assignee: Shugo Maeda
* ruby -v: ruby 2.3.0dev (2015-01-21 trunk 49326) [x86_64-darwin14]
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED
----------------------------------------
`Module#remove_method` should raise a `NameError`
if method is not defined in refined class, such as [`undef`](https://bugs.ruby-lang.org/issues/8966).
But if method is defined in refined class, `Module#remove_method` should keep refined method and remove original method.

I confirmed by following examples in ruby-trunk, 2.2.0, 2.1.5, 2.0.0-p598

``` ruby
class C
  def foo
    "C#foo"
  end
end

module RefinementBug
  refine C do
    def foo
      "RefinementBug#foo"
    end
  end
end

using RefinementBug

class C
  remove_method :foo
end

puts C.new.foo

# expected:
#   RefinementBug#foo
#
# actual:
#   bug.rb:21:in `<main>': undefined method `foo' for #<C:0x007f9e5c087b48> (NoMethodError)
```

``` ruby
class C
end

module RefinementBug
  refine C do
    def foo
    end
  end
end

using RefinementBug

class C
  remove_method :foo
end

# expected:
#   bug2.rb:14:in `remove_method': method `foo' not defined in C (NameError)
#   from bug2.rb:14:in `<class:C>'
#   from bug2.rb:13:in `<main>'
#
# actual:
#   # => nothing raised.
```

---Files--------------------------------
bug2.rb (327 Bytes)
bug.rb (342 Bytes)
0001-vm_method.c-fix-remove-refined-method.patch (2.68 KB)


-- 
https://bugs.ruby-lang.org/