Skip to content

Commit d3d0ab5

Browse files
committed
alias should care complemented flag
`me->defined_class` should be 0 for method entries of Modules. The current implementation violates this condition when the aliased object was complemented cme. This patch checks this condition and fix ruby#11965 (comment)
1 parent 53df2a3 commit d3d0ab5

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

test/ruby/test_alias.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,40 @@ class A
292292
end
293293
end;
294294
end
295+
296+
def test_alias_complemented_method
297+
assert_in_out_err(%w[-w], "#{<<~"begin;"}\n#{<<~'end;'}")
298+
begin;
299+
module M
300+
def foo = 1
301+
self.extend M
302+
end
303+
304+
3.times{|i|
305+
module M
306+
alias foo2 foo
307+
remove_method :foo
308+
def foo = 2
309+
ensure
310+
remove_method :foo
311+
alias foo foo2
312+
remove_method :foo2
313+
end
314+
315+
M.foo
316+
317+
original_foo = M.method(:foo)
318+
319+
M.class_eval do
320+
remove_method :foo
321+
def foo = 3
322+
end
323+
324+
M.class_eval do
325+
remove_method :foo
326+
define_method :foo, original_foo
327+
end
328+
}
329+
end;
330+
end
295331
end

vm_method.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,11 @@ rb_alias(VALUE klass, ID alias_name, ID original_name)
23332333

23342334
alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
23352335
RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
2336-
RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2336+
2337+
// defiend_class should not copy from complemented me (cme)
2338+
if (!METHOD_ENTRY_COMPLEMENTED(orig_me)) {
2339+
RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
2340+
}
23372341
}
23382342
}
23392343

0 commit comments

Comments
 (0)