Fix modiying class variable to frozen class/module

This commit is contained in:
KOBAYASHI Shuji
2019-04-24 21:05:44 +09:00
parent 163a6d01e0
commit 4720648137
3 changed files with 28 additions and 1 deletions
+7 -1
View File
@@ -171,7 +171,6 @@ assert('Module#class_variable_set', '15.2.2.4.18') do
@@foo
end
end
assert_equal 99, Test4ClassVariableSet.class_variable_set(:@@cv, 99)
assert_equal 101, Test4ClassVariableSet.class_variable_set(:@@foo, 101)
assert_true Test4ClassVariableSet.class_variables.include? :@@cv
@@ -180,6 +179,13 @@ assert('Module#class_variable_set', '15.2.2.4.18') do
%w[@@ @@1 @@x= @x @ x 1].each do |n|
assert_raise(NameError) { Test4ClassVariableSet.class_variable_set(n, 1) }
end
m = Module.new.freeze
assert_raise(FrozenError) { m.class_variable_set(:@@cv, 1) }
parent = Class.new{ class_variable_set(:@@a, nil) }.freeze
child = Class.new(parent)
assert_raise(FrozenError) { child.class_variable_set(:@@a, 1) }
end
assert('Module#class_variables', '15.2.2.4.19') do
+2
View File
@@ -671,6 +671,7 @@ mrb_mod_cv_set(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value v)
iv_tbl *t = c->iv;
if (iv_get(mrb, t, sym, NULL)) {
mrb_check_frozen(mrb, c);
iv_put(mrb, t, sym, v);
mrb_write_barrier(mrb, (struct RBasic*)c);
return;
@@ -698,6 +699,7 @@ mrb_mod_cv_set(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value v)
c = cls;
}
mrb_check_frozen(mrb, c);
if (!c->iv) {
c->iv = iv_new(mrb);
}
+19
View File
@@ -433,6 +433,25 @@ assert('overriding class variable with a module (#3235)') do
end
end
assert('class variable for frozen class/module') do
module CVarForFrozenModule
freeze
assert_raise(FrozenError) { @@cv = 1 }
end
class CVarForFrozenClassA
@@a = nil
freeze
end
class CVarForFrozenClassB < CVarForFrozenClassA
def a=(v)
@@a = v
end
end
b = CVarForFrozenClassB.new
assert_raise(FrozenError) { b.a = 1 }
end
assert('class with non-class/module outer raises TypeError') do
assert_raise(TypeError) { class 0::C1; end }
assert_raise(TypeError) { class []::C2; end }