From 0b4b2cd1ee161f2875375181e0978d7757e25c8a Mon Sep 17 00:00:00 2001 From: Haldun Bayhantopcu Date: Wed, 15 Feb 2023 21:13:54 +0100 Subject: Fix removing ivars from clases and modules. Co-authored-by: Adam Hess --- shape.c | 4 +++- test/ruby/test_shapes.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/shape.c b/shape.c index 7c3bdf8c50..6852a8d554 100644 --- a/shape.c +++ b/shape.c @@ -238,7 +238,9 @@ remove_shape_recursive(VALUE obj, ID id, rb_shape_t * shape, VALUE * removed) // has the same attributes as this shape. if (new_parent) { bool dont_care; - rb_shape_t * new_child = get_next_shape_internal(new_parent, shape->edge_name, shape->type, &dont_care, true, false); + enum ruby_value_type type = BUILTIN_TYPE(obj); + bool new_shape_necessary = type == T_CLASS || type == T_MODULE; + rb_shape_t * new_child = get_next_shape_internal(new_parent, shape->edge_name, shape->type, &dont_care, true, new_shape_necessary); new_child->capacity = shape->capacity; if (new_child->type == SHAPE_IVAR) { move_iv(obj, id, shape->next_iv_index - 1, new_child->next_iv_index - 1); diff --git a/test/ruby/test_shapes.rb b/test/ruby/test_shapes.rb index 85adcd4a80..0e7a2d0b0c 100644 --- a/test/ruby/test_shapes.rb +++ b/test/ruby/test_shapes.rb @@ -108,6 +108,32 @@ class TestShapes < Test::Unit::TestCase assert_false RubyVM::Shape.of(obj).too_complex? end + def test_removing_when_too_many_ivs_on_class + obj = Class.new + + (RubyVM::Shape::SHAPE_MAX_NUM_IVS + 2).times do + obj.instance_variable_set(:"@a#{_1}", 1) + end + (RubyVM::Shape::SHAPE_MAX_NUM_IVS + 2).times do + obj.remove_instance_variable(:"@a#{_1}") + end + + assert_empty obj.instance_variables + end + + def test_removing_when_too_many_ivs_on_module + obj = Module.new + + (RubyVM::Shape::SHAPE_MAX_NUM_IVS + 2).times do + obj.instance_variable_set(:"@a#{_1}", 1) + end + (RubyVM::Shape::SHAPE_MAX_NUM_IVS + 2).times do + obj.remove_instance_variable(:"@a#{_1}") + end + + assert_empty obj.instance_variables + end + def test_too_complex_ractor assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") begin; -- cgit v1.2.1