summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2026-04-12 15:13:03 +0200
committernagachika <nagachika@ruby-lang.org>2026-04-15 18:51:03 +0900
commit1bdce3585e2e135a3d69c5bf3e3ea446d4839851 (patch)
tree58f56c1e9c63097be5af52cd642bd6c7deb00bbe
parent0280c61fb4ac408611721dd9680e4bb9a436a9c3 (diff)
iseq.c: rb_estimate_iv_count handle no superclass
[Bug #21992] When redefining `BasicObject#initialize` there's no super class to access.
-rw-r--r--iseq.c4
-rw-r--r--test/ruby/test_super.rb14
2 files changed, 17 insertions, 1 deletions
diff --git a/iseq.c b/iseq.c
index 7a2aefa584..c248834e2d 100644
--- a/iseq.c
+++ b/iseq.c
@@ -2916,7 +2916,9 @@ rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq)
attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
VALUE superclass = rb_class_superclass(klass);
- count += RCLASS_EXT(superclass)->max_iv_count;
+ if (!NIL_P(superclass)) { // BasicObject doesn't have a superclass
+ count += RCLASS_EXT(superclass)->max_iv_count;
+ }
rb_id_table_free(iv_names);
diff --git a/test/ruby/test_super.rb b/test/ruby/test_super.rb
index 25bad2242a..39594d74be 100644
--- a/test/ruby/test_super.rb
+++ b/test/ruby/test_super.rb
@@ -760,6 +760,20 @@ class TestSuper < Test::Unit::TestCase
assert_equal 2, inherited.test # it may read index=1 while it should be index=2
end
+ def test_define_initialize_in_basic_object
+ assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
+ begin;
+ class ::BasicObject
+ alias_method :initialize, :initialize
+ def initialize
+ @bug = "[Bug #21992]"
+ end
+ end
+
+ assert_not_nil Object.new
+ end;
+ end
+
def test_super_in_basic_object
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;