1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fix setting method visibility on method wrapped with prepend

Ignore prepended modules when looking for already defined methods on a
class to set the visibility on.
[Fix GH-1834]

From: Dylan Thacker-Smith <Dylan.Smith@shopify.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62725 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-03-12 06:54:43 +00:00
parent f2b094a522
commit ae36cf62b7
3 changed files with 61 additions and 2 deletions

View file

@ -51,4 +51,43 @@ describe "Module#private" do
Module.new.send(:private, :undefined) Module.new.send(:private, :undefined)
end.should raise_error(NameError) end.should raise_error(NameError)
end end
it "only makes the method private in the class it is called on" do
base = Class.new do
def wrapped
1
end
end
klass = Class.new(base) do
def wrapped
super + 1
end
private :wrapped
end
base.new.wrapped.should == 1
lambda do
klass.new.wrapped
end.should raise_error(NameError)
end
it "continues to allow a prepended module method to call super" do
wrapper = Module.new do
def wrapped
super + 1
end
end
klass = Class.new do
prepend wrapper
def wrapped
1
end
private :wrapped
end
klass.new.wrapped.should == 2
end
end end

View file

@ -1887,6 +1887,25 @@ class TestModule < Test::Unit::TestCase
assert_raise(ArgumentError) { Module.new { prepend } } assert_raise(ArgumentError) { Module.new { prepend } }
end end
def test_prepend_private_super
wrapper = Module.new do
def wrapped
super + 1
end
end
klass = Class.new do
prepend wrapper
def wrapped
1
end
private :wrapped
end
assert_equal(2, klass.new.wrapped)
end
def test_class_variables def test_class_variables
m = Module.new m = Module.new
m.class_variable_set(:@@foo, 1) m.class_variable_set(:@@foo, 1)

View file

@ -1054,8 +1054,9 @@ rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
{ {
rb_method_entry_t *me; rb_method_entry_t *me;
VALUE defined_class; VALUE defined_class;
VALUE origin_class = RCLASS_ORIGIN(klass);
me = search_method(klass, name, &defined_class); me = search_method(origin_class, name, &defined_class);
if (!me && RB_TYPE_P(klass, T_MODULE)) { if (!me && RB_TYPE_P(klass, T_MODULE)) {
me = search_method(rb_cObject, name, &defined_class); me = search_method(rb_cObject, name, &defined_class);
} }
@ -1068,7 +1069,7 @@ rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
if (METHOD_ENTRY_VISI(me) != visi) { if (METHOD_ENTRY_VISI(me) != visi) {
rb_vm_check_redefinition_opt_method(me, klass); rb_vm_check_redefinition_opt_method(me, klass);
if (klass == defined_class || RCLASS_ORIGIN(klass) == defined_class) { if (klass == defined_class || origin_class == defined_class) {
METHOD_ENTRY_VISI_SET(me, visi); METHOD_ENTRY_VISI_SET(me, visi);
if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) { if (me->def->type == VM_METHOD_TYPE_REFINED && me->def->body.refined.orig_me) {