1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Warn instance variable E

It is not dumped, as it is a short alias for `:encoding`.
This commit is contained in:
Nobuyoshi Nakada 2019-08-10 13:18:41 +09:00
parent 3c3783ac88
commit ffdef3674a
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
3 changed files with 25 additions and 6 deletions

View file

@ -1,12 +1,13 @@
#include <ruby.h> #include <ruby.h>
static ID id_normal_ivar, id_internal_ivar; static ID id_normal_ivar, id_internal_ivar, id_encoding_short;
static VALUE static VALUE
init(VALUE self, VALUE arg1, VALUE arg2) init(VALUE self, VALUE arg1, VALUE arg2, VALUE arg3)
{ {
rb_ivar_set(self, id_normal_ivar, arg1); rb_ivar_set(self, id_normal_ivar, arg1);
rb_ivar_set(self, id_internal_ivar, arg2); rb_ivar_set(self, id_internal_ivar, arg2);
rb_ivar_set(self, id_encoding_short, arg3);
return self; return self;
} }
@ -22,6 +23,12 @@ get_internal(VALUE self)
return rb_attr_get(self, id_internal_ivar); return rb_attr_get(self, id_internal_ivar);
} }
static VALUE
get_encoding_short(VALUE self)
{
return rb_attr_get(self, id_encoding_short);
}
void void
Init_internal_ivar(void) Init_internal_ivar(void)
{ {
@ -33,7 +40,9 @@ Init_internal_ivar(void)
/* leave id_internal_ivar being 0 */ /* leave id_internal_ivar being 0 */
id_internal_ivar = rb_make_internal_id(); id_internal_ivar = rb_make_internal_id();
#endif #endif
rb_define_method(newclass, "initialize", init, 2); id_encoding_short = rb_intern_const("E");
rb_define_method(newclass, "initialize", init, 3);
rb_define_method(newclass, "normal", get_normal, 0); rb_define_method(newclass, "normal", get_normal, 0);
rb_define_method(newclass, "internal", get_internal, 0); rb_define_method(newclass, "internal", get_internal, 0);
rb_define_method(newclass, "encoding_short", get_encoding_short, 0);
} }

View file

@ -576,7 +576,13 @@ w_obj_each(st_data_t key, st_data_t val, st_data_t a)
struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a; struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a;
struct dump_call_arg *arg = ivarg->dump; struct dump_call_arg *arg = ivarg->dump;
if (to_be_skipped_id(id)) return ST_CONTINUE; if (to_be_skipped_id(id)) {
if (id == s_encoding_short) {
rb_warn("instance variable `E' on class %"PRIsVALUE" is not dumped",
CLASS_OF(arg->obj));
}
return ST_CONTINUE;
}
if (!ivarg->num_ivar) { if (!ivarg->num_ivar) {
rb_raise(rb_eRuntimeError, "instance variable added to %"PRIsVALUE" instance", rb_raise(rb_eRuntimeError, "instance variable added to %"PRIsVALUE" instance",
CLASS_OF(arg->obj)); CLASS_OF(arg->obj));

View file

@ -7,14 +7,18 @@ module Bug end
module Bug::Marshal module Bug::Marshal
class TestInternalIVar < Test::Unit::TestCase class TestInternalIVar < Test::Unit::TestCase
def test_marshal def test_marshal
v = InternalIVar.new("hello", "world") v = InternalIVar.new("hello", "world", "bye")
assert_equal("hello", v.normal) assert_equal("hello", v.normal)
assert_equal("world", v.internal) assert_equal("world", v.internal)
dump = ::Marshal.dump(v) assert_equal("bye", v.encoding_short)
dump = assert_warn(/instance variable `E' on class \S+ is not dumped/) {
::Marshal.dump(v)
}
v = assert_nothing_raised {break ::Marshal.load(dump)} v = assert_nothing_raised {break ::Marshal.load(dump)}
assert_instance_of(InternalIVar, v) assert_instance_of(InternalIVar, v)
assert_equal("hello", v.normal) assert_equal("hello", v.normal)
assert_nil(v.internal) assert_nil(v.internal)
assert_nil(v.encoding_short)
end end
end end
end end