summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2023-11-01 08:25:09 -0400
committerPeter Zhu <peter@peterzhu.ca>2023-11-01 08:25:09 -0400
commit9c6dd25093f49d926ac71c3b91e1320d4bbc553a (patch)
treeb27adec6caae1bbacc95a92c29f035dc18501444
parenta1e24ab4841b8032b274bfd49c30adfdeb82b859 (diff)
Fix removing non-existent ivar for too complex
-rw-r--r--test/ruby/test_shapes.rb9
-rw-r--r--variable.c8
2 files changed, 15 insertions, 2 deletions
diff --git a/test/ruby/test_shapes.rb b/test/ruby/test_shapes.rb
index 228e9a4913..7e3a77fdf2 100644
--- a/test/ruby/test_shapes.rb
+++ b/test/ruby/test_shapes.rb
@@ -265,8 +265,13 @@ class TestShapes < Test::Unit::TestCase
c = Class.new
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))
+
+ assert_raise(NameError) do
+ c.remove_instance_variable(:@a)
+ end
end;
end
@@ -290,6 +295,10 @@ class TestShapes < Test::Unit::TestCase
tc.remove_instance_variable(:@a)
assert_nil(tc.instance_variable_get(:@a))
+
+ assert_raise(NameError) do
+ tc.remove_instance_variable(:@a)
+ end
end;
end
diff --git a/variable.c b/variable.c
index 1e44775887..41c2b3650c 100644
--- a/variable.c
+++ b/variable.c
@@ -2199,7 +2199,9 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name)
case T_MODULE:
IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
if (rb_shape_obj_too_complex(obj)) {
- st_delete(RCLASS_IV_HASH(obj), (st_data_t *)&id, (st_data_t *)&val);
+ if (!st_delete(RCLASS_IV_HASH(obj), (st_data_t *)&id, (st_data_t *)&val)) {
+ val = Qundef;
+ }
}
else {
rb_shape_transition_shape_remove_ivar(obj, id, shape, &val);
@@ -2220,7 +2222,9 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name)
if (rb_shape_obj_too_complex(obj)) {
struct gen_ivtbl *ivtbl;
if (rb_gen_ivtbl_get(obj, 0, &ivtbl)) {
- st_delete(ivtbl->as.complex.table, (st_data_t *)&id, (st_data_t *)&val);
+ if (!st_delete(ivtbl->as.complex.table, (st_data_t *)&id, (st_data_t *)&val)) {
+ val = Qundef;
+ }
}
}
else {