summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Evans <[email protected]>2021-12-27 09:39:15 -0800
committerBenoit Daloze <[email protected]>2022-08-20 13:44:00 +0200
commit8212aab81a77a2a91fb7c1681b4968171193b48f ()
tree05894efbb106ad56f06a25b9bbfd9432440c7ad9
parentb32a3f1275a8c7748f2134492ce3c532f277d261 (diff)
Make Object#method and Module#instance_method not skip ZSUPER methods
Based on https://.com/jeremyevans/ruby/commit/c95e7e5329140f640b6497905485761f3336d967 Among other things, this fixes calling visibility methods (public?, protected?, and private?) on them. It also fixes #owner to show the class the zsuper method entry is defined in, instead of the original class it references. For some backwards compatibility, adjust #parameters and #source_location, to show the parameters and source location of the method originally defined. Also have the parameters and source location still be shown by #inspect. Clarify documentation of {Method,UnboundMethod}#owner. Add tests based on the description of https://bugs.ruby-lang.org/issues/18435 and based on https://.com/ruby/ruby/pull/5356#issuecomment-1005298809 Fixes [Bug #18435] [Bug #18729] Co-authored-by: Benoit Daloze <[email protected]>
Notes: Merged: https://.com/ruby/ruby/pull/6242
-rw-r--r--proc.c63
-rw-r--r--test/ruby/test_method.rb59
2 files changed, 100 insertions, 22 deletions
@@ -1684,7 +1684,6 @@ mnew_internal(const rb_method_entry_t *me, VALUE klass, VALUE iclass,
VALUE method;
rb_method_visibility_t visi = METHOD_VISI_UNDEF;
- again:
if (UNDEFINED_METHOD_ENTRY_P(me)) {
if (respond_to_missing_p(klass, obj, ID2SYM(id), scope)) {
return mnew_missing(klass, obj, id, mclass);
@@ -1700,19 +1699,6 @@ mnew_internal(const rb_method_entry_t *me, VALUE klass, VALUE iclass,
rb_print_inaccessible(klass, id, visi);
}
}
- if (me->def->type == VM_METHOD_TYPE_ZSUPER) {
- if (me->defined_class) {
- VALUE klass = RCLASS_SUPER(RCLASS_ORIGIN(me->defined_class));
- id = me->def->original_id;
- me = (rb_method_entry_t *)rb_callable_method_entry_with_refinements(klass, id, &iclass);
- }
- else {
- VALUE klass = RCLASS_SUPER(RCLASS_ORIGIN(me->owner));
- id = me->def->original_id;
- me = rb_method_entry_without_refinements(klass, id, &iclass);
- }
- goto again;
- }
method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
@@ -1934,7 +1920,15 @@ method_original_name(VALUE obj)
* call-seq:
* meth.owner -> class_or_module
*
- * Returns the class or module that defines the method.
* See also Method#receiver.
*
* (1..3).method(:map).owner #=> Enumerable
@@ -2951,6 +2945,24 @@ rb_method_entry_location(const rb_method_entry_t *me)
return method_def_location(me->def);
}
/*
* call-seq:
* meth.source_location -> [String, Integer]
@@ -2962,7 +2974,7 @@ rb_method_entry_location(const rb_method_entry_t *me)
VALUE
rb_method_location(VALUE method)
{
- return method_def_location(rb_method_def(method));
}
static const rb_method_definition_t *
@@ -3050,7 +3062,7 @@ method_def_parameters(const rb_method_definition_t *def)
static VALUE
rb_method_parameters(VALUE method)
{
- return method_def_parameters(rb_method_def(method));
}
/*
@@ -3112,6 +3124,23 @@ method_inspect(VALUE method)
if (data->me->def->type == VM_METHOD_TYPE_ALIAS) {
defined_class = data->me->def->body.alias.original_me->owner;
}
else {
defined_class = method_entry_defined_class(data->me);
}
@@ -1056,20 +1056,28 @@ class TestMethod < Test::Unit::TestCase
assert_equal(sm, im.clone.bind(o).super_method)
end
- def test_super_method_removed
c1 = Class.new {private def foo; end}
c2 = Class.new(c1) {public :foo}
c3 = Class.new(c2) {def foo; end}
c1.class_eval {undef foo}
m = c3.instance_method(:foo)
m = assert_nothing_raised(NameError, Feature9781) {break m.super_method}
- assert_nil(m, Feature9781)
end
def test_prepended_public_zsuper
mod = EnvUtil.labeled_module("Mod") {private def foo; :ok end}
- mods = [mod]
obj = Object.new.extend(mod)
class << obj
public :foo
end
@@ -1079,7 +1087,7 @@ class TestMethod < Test::Unit::TestCase
end
m = obj.method(:foo)
assert_equal(mods, mods.map {m.owner.tap {m = m.super_method}})
- assert_nil(m)
end
def test_super_method_with_prepended_module
@@ -1192,6 +1200,47 @@ class TestMethod < Test::Unit::TestCase
assert_nil(super_method)
end
def rest_parameter(*rest)
rest
end
@@ -1310,7 +1359,7 @@ class TestMethod < Test::Unit::TestCase
::Object.prepend(M2)
m = Object.instance_method(:x)
- assert_equal M, m.owner
end;
end