Skip to content

Commit 6593cc5

Browse files
committed
iseq.c: rb_estimate_iv_count handle no superclass
[Bug #21992] When redefining `BasicObject#initialize` there's no super class to access.
1 parent 8e96d6f commit 6593cc5

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

iseq.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3027,7 +3027,9 @@ rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq)
30273027
attr_index_t count = (attr_index_t)iv_names.num_entries;
30283028

30293029
VALUE superclass = rb_class_superclass(klass);
3030-
count += RCLASS_MAX_IV_COUNT(superclass);
3030+
if (!NIL_P(superclass)) { // BasicObject doesn't have a superclass
3031+
count += RCLASS_MAX_IV_COUNT(superclass);
3032+
}
30313033

30323034
set_free_embedded_table(&iv_names);
30333035

test/ruby/test_super.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,20 @@ def initialize
760760
assert_equal 2, inherited.test # it may read index=1 while it should be index=2
761761
end
762762

763+
def test_define_initialize_in_basic_object
764+
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
765+
begin;
766+
class ::BasicObject
767+
alias_method :initialize, :initialize
768+
def initialize
769+
@bug = "[Bug #21992]"
770+
end
771+
end
772+
773+
assert_not_nil Object.new
774+
end;
775+
end
776+
763777
def test_super_in_basic_object
764778
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
765779
begin;

0 commit comments

Comments
 (0)