Don't pin named structs defined in Ruby

[Bug #20311]

`rb_define_class_under` assumes it's called from C and that the
reference might be held in a C global variable, so it adds the
class to the VM root.

In the case of `Struct.new('Name')` it's wasteful and make
the struct immortal.
This commit is contained in:
Jean Boussier 2024-02-29 13:17:22 +01:00 committed by Jean Boussier
parent 5d76fe6b2a
commit e626da82ea
4 changed files with 36 additions and 9 deletions

13
class.c
View file

@ -1007,7 +1007,7 @@ rb_define_class_under(VALUE outer, const char *name, VALUE super)
}
VALUE
rb_define_class_id_under(VALUE outer, ID id, VALUE super)
rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
{
VALUE klass;
@ -1024,8 +1024,6 @@ rb_define_class_id_under(VALUE outer, ID id, VALUE super)
" (%"PRIsVALUE" is given but was %"PRIsVALUE")",
outer, rb_id2str(id), RCLASS_SUPER(klass), super);
}
/* Class may have been defined in Ruby and not pin-rooted */
rb_vm_add_root_module(klass);
return klass;
}
@ -1037,11 +1035,18 @@ rb_define_class_id_under(VALUE outer, ID id, VALUE super)
rb_set_class_path_string(klass, outer, rb_id2str(id));
rb_const_set(outer, id, klass);
rb_class_inherited(super, klass);
rb_vm_add_root_module(klass);
return klass;
}
VALUE
rb_define_class_id_under(VALUE outer, ID id, VALUE super)
{
VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
rb_vm_add_root_module(klass);
return klass;
}
VALUE
rb_module_s_alloc(VALUE klass)
{