From 8889992b75f85c8080fc09c7a3a1cb6123a098c1 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 31 Oct 2023 16:38:05 -0400 Subject: [PATCH] Fix remove_instance_variable for too complex class --- test/ruby/test_shapes.rb | 19 ++++++++++++++++++- variable.c | 7 ++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/test/ruby/test_shapes.rb b/test/ruby/test_shapes.rb index a098edf409..529964729b 100644 --- a/test/ruby/test_shapes.rb +++ b/test/ruby/test_shapes.rb @@ -233,7 +233,7 @@ class TestShapes < Test::Unit::TestCase end; end - def test_run_out_of_shape + def test_run_out_of_shape_for_object assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") begin; class A @@ -252,6 +252,23 @@ class TestShapes < Test::Unit::TestCase end; end + def test_run_out_of_shape_for_class + assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + c = Class.new + i = 0 + while RubyVM::Shape.shapes_available > 0 + c.instance_variable_set(:"@i#{i}", 1) + i += 1 + end + + c.instance_variable_set(:@a, 1) + assert_equal(1, c.instance_variable_get(:@a)) + c.remove_instance_variable(:@a) + assert_nil(c.instance_variable_get(:@a)) + end; + end + def test_run_out_of_shape_generic_ivar_set assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") begin; diff --git a/variable.c b/variable.c index f179b8b612..a374756974 100644 --- a/variable.c +++ b/variable.c @@ -2198,7 +2198,12 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name) case T_CLASS: case T_MODULE: IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id); - rb_shape_transition_shape_remove_ivar(obj, id, shape, &val); + if (rb_shape_obj_too_complex(obj)) { + st_delete(RCLASS_IV_HASH(obj), (st_data_t *)&id, (st_data_t *)&val); + } + else { + rb_shape_transition_shape_remove_ivar(obj, id, shape, &val); + } break; case T_OBJECT: { if (rb_shape_obj_too_complex(obj)) {