From 5792bd7149a30f7f43a739199d84603cc579f4e5 Mon Sep 17 00:00:00 2001 From: Thomas Marshall Date: Tue, 8 Oct 2024 17:30:51 +0100 Subject: [PATCH] Emit warning for other method redefinition types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- test/ruby/test_object.rb | 31 +++++++++++++++++++++++-------- vm_method.c | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb index 37c09596a2..3afad0ef43 100644 --- a/test/ruby/test_object.rb +++ b/test/ruby/test_object.rb @@ -480,15 +480,30 @@ 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$") - $VERBOSE = false - def (Object.new).object_id; end - INPUT + %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).#{m}; end + INPUT - assert_in_out_err([], <<-INPUT, [], %r"warning: redefining '__send__' may cause serious problems$") - $VERBOSE = false - def (Object.new).__send__; end - INPUT + assert_in_out_err([], <<-INPUT, [], %r"warning: redefining '#{m}' may cause serious problems$") + $VERBOSE = false + 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) diff --git a/vm_method.c b/vm_method.c index 4eb074bbd8..e436538f56 100644 --- a/vm_method.c +++ b/vm_method.c @@ -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)); } }