Emit warning for other method redefinition types

This commit ensures warnings about `object_id` and `__send__` method
redefinitions are emitted for other method types such as aliases, procs,
and attr readers—anything except C functions.
This commit is contained in:
Thomas Marshall 2024-10-08 17:30:51 +01:00 committed by Nobuyoshi Nakada
parent a985695b9e
commit 5792bd7149
2 changed files with 24 additions and 9 deletions

View File

@ -480,16 +480,31 @@ class TestObject < Test::Unit::TestCase
end
def test_redefine_method_which_may_case_serious_problem
assert_in_out_err([], <<-INPUT, [], %r"warning: redefining 'object_id' may cause serious problems$")
%w(object_id __send__).each do |m|
assert_in_out_err([], <<-INPUT, [], %r"warning: redefining '#{m}' may cause serious problems$")
$VERBOSE = false
def (Object.new).object_id; end
def (Object.new).#{m}; end
INPUT
assert_in_out_err([], <<-INPUT, [], %r"warning: redefining '__send__' may cause serious problems$")
assert_in_out_err([], <<-INPUT, [], %r"warning: redefining '#{m}' may cause serious problems$")
$VERBOSE = false
def (Object.new).__send__; end
Class.new.define_method(:#{m}) {}
INPUT
assert_in_out_err([], <<-INPUT, [], %r"warning: redefining '#{m}' may cause serious problems$")
$VERBOSE = false
Class.new.attr_reader(:#{m})
INPUT
assert_in_out_err([], <<-INPUT, [], %r"warning: redefining '#{m}' may cause serious problems$")
$VERBOSE = false
Class.new do
def foo; end
alias #{m} foo
end
INPUT
end
bug10421 = '[ruby-dev:48691] [Bug #10421]'
assert_in_out_err([], <<-INPUT, ["1"], [], bug10421)
$VERBOSE = false

View File

@ -1062,7 +1062,7 @@ rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibil
}
/* check mid */
if (mid == object_id || mid == id__send__) {
if (type == VM_METHOD_TYPE_ISEQ && search_method(klass, mid, 0)) {
if (type != VM_METHOD_TYPE_CFUNC && search_method(klass, mid, 0)) {
rb_warn("redefining '%s' may cause serious problems", rb_id2name(mid));
}
}