summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-08-10 13:18:41 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-08-10 13:18:41 +0900
commitffdef3674af921d7ddd550dc492dcdbed97f7ba5 (patch)
treec2575b5307453e22085feb2cd586315c585f7266
parent3c3783ac88f8f8fbbad016a2daf3e35a682c4b3e (diff)
Warn instance variable `E`
It is not dumped, as it is a short alias for `:encoding`.
-rw-r--r--ext/-test-/marshal/internal_ivar/internal_ivar.c15
-rw-r--r--marshal.c8
-rw-r--r--test/-ext-/marshal/test_internal_ivar.rb8
3 files changed, 25 insertions, 6 deletions
diff --git a/ext/-test-/marshal/internal_ivar/internal_ivar.c b/ext/-test-/marshal/internal_ivar/internal_ivar.c
index 299da27..de0cf71 100644
--- a/ext/-test-/marshal/internal_ivar/internal_ivar.c
+++ b/ext/-test-/marshal/internal_ivar/internal_ivar.c
@@ -1,12 +1,13 @@
#include <ruby.h>
-static ID id_normal_ivar, id_internal_ivar;
+static ID id_normal_ivar, id_internal_ivar, id_encoding_short;
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_internal_ivar, arg2);
+ rb_ivar_set(self, id_encoding_short, arg3);
return self;
}
@@ -22,6 +23,12 @@ get_internal(VALUE self)
return rb_attr_get(self, id_internal_ivar);
}
+static VALUE
+get_encoding_short(VALUE self)
+{
+ return rb_attr_get(self, id_encoding_short);
+}
+
void
Init_internal_ivar(void)
{
@@ -33,7 +40,9 @@ Init_internal_ivar(void)
/* leave id_internal_ivar being 0 */
id_internal_ivar = rb_make_internal_id();
#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, "internal", get_internal, 0);
+ rb_define_method(newclass, "encoding_short", get_encoding_short, 0);
}
diff --git a/marshal.c b/marshal.c
index 5e0c30b..b6de650 100644
--- a/marshal.c
+++ b/marshal.c
@@ -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 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) {
rb_raise(rb_eRuntimeError, "instance variable added to %"PRIsVALUE" instance",
CLASS_OF(arg->obj));
diff --git a/test/-ext-/marshal/test_internal_ivar.rb b/test/-ext-/marshal/test_internal_ivar.rb
index 5152966..a32138f 100644
--- a/test/-ext-/marshal/test_internal_ivar.rb
+++ b/test/-ext-/marshal/test_internal_ivar.rb
@@ -7,14 +7,18 @@ module Bug end
module Bug::Marshal
class TestInternalIVar < Test::Unit::TestCase
def test_marshal
- v = InternalIVar.new("hello", "world")
+ v = InternalIVar.new("hello", "world", "bye")
assert_equal("hello", v.normal)
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)}
assert_instance_of(InternalIVar, v)
assert_equal("hello", v.normal)
assert_nil(v.internal)
+ assert_nil(v.encoding_short)
end
end
end