Warn when redefining __id__ as well as object_id

[Feature #20912]
This commit is contained in:
John Hawthorn 2024-11-26 16:40:47 -08:00
parent a505cd32fb
commit f1dda5ed01
Notes: git 2024-11-30 04:41:19 +00:00
5 changed files with 11 additions and 8 deletions

View File

@ -8,6 +8,7 @@ firstline, predefined = __LINE__+1, %[\
inspect
intern
object_id
__id__
const_added
const_missing
method_missing MethodMissing

View File

@ -11,8 +11,10 @@ describe "BasicObject#equal?" do
it "is unaffected by overriding __id__" do
o1 = mock("object")
o2 = mock("object")
def o1.__id__; 10; end
def o2.__id__; 10; end
suppress_warning {
def o1.__id__; 10; end
def o2.__id__; 10; end
}
o1.equal?(o2).should be_false
end

View File

@ -1488,7 +1488,7 @@ class TestModule < Test::Unit::TestCase
class << o; self; end.instance_eval { undef_method(:foo) }
end
%w(object_id __send__ initialize).each do |n|
%w(object_id __id__ __send__ initialize).each do |n|
assert_in_out_err([], <<-INPUT, [], %r"warning: undefining '#{n}' may cause serious problems$")
$VERBOSE = false
Class.new.instance_eval { undef_method(:#{n}) }

View File

@ -480,7 +480,7 @@ class TestObject < Test::Unit::TestCase
end
def test_redefine_method_which_may_case_serious_problem
%w(object_id __send__).each do |m|
%w(object_id __id__ __send__).each do |m|
assert_in_out_err([], <<-INPUT, [], %r"warning: redefining '#{m}' may cause serious problems$")
$VERBOSE = false
def (Object.new).#{m}; end
@ -542,7 +542,7 @@ class TestObject < Test::Unit::TestCase
bug2202 = '[ruby-core:26074]'
assert_raise(NoMethodError, bug2202) {o2.meth2}
%w(object_id __send__ initialize).each do |m|
%w(object_id __id__ __send__ initialize).each do |m|
assert_in_out_err([], <<-INPUT, %w(:ok), %r"warning: removing '#{m}' may cause serious problems$")
$VERBOSE = false
begin

View File

@ -1072,7 +1072,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 (mid == object_id || mid == id__id__ || mid == id__send__) {
if (type != VM_METHOD_TYPE_CFUNC && search_method(klass, mid, 0)) {
rb_warn("redefining '%s' may cause serious problems", rb_id2name(mid));
}
@ -1681,7 +1681,7 @@ remove_method(VALUE klass, ID mid)
rb_class_modify_check(klass);
klass = RCLASS_ORIGIN(klass);
if (mid == object_id || mid == id__send__ || mid == idInitialize) {
if (mid == object_id || mid == id__id__ || mid == id__send__ || mid == idInitialize) {
rb_warn("removing '%s' may cause serious problems", rb_id2name(mid));
}
@ -1911,7 +1911,7 @@ rb_undef(VALUE klass, ID id)
rb_raise(rb_eTypeError, "no class to undef method");
}
rb_class_modify_check(klass);
if (id == object_id || id == id__send__ || id == idInitialize) {
if (id == object_id || id == id__id__ || id == id__send__ || id == idInitialize) {
rb_warn("undefining '%s' may cause serious problems", rb_id2name(id));
}