From 6593cc52d6153c5535968a8faff6cf4ac83a11fe Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Sun, 12 Apr 2026 15:13:03 +0200 Subject: iseq.c: rb_estimate_iv_count handle no superclass [Bug #21992] When redefining `BasicObject#initialize` there's no super class to access. --- iseq.c | 4 +++- test/ruby/test_super.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/iseq.c b/iseq.c index 759c8be690..5ba26dd53b 100644 --- a/iseq.c +++ b/iseq.c @@ -3027,7 +3027,9 @@ rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq) attr_index_t count = (attr_index_t)iv_names.num_entries; VALUE superclass = rb_class_superclass(klass); - count += RCLASS_MAX_IV_COUNT(superclass); + if (!NIL_P(superclass)) { // BasicObject doesn't have a superclass + count += RCLASS_MAX_IV_COUNT(superclass); + } set_free_embedded_table(&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; -- cgit v1.2.3