Fix constant names set using const_set on a singleton class
Fixes [Bug #14895]
This commit is contained in:
parent
11922b5e03
commit
5e16857315
Notes:
git
2020-09-02 13:05:39 +09:00
@ -20,10 +20,20 @@ describe "Module#const_set" do
|
|||||||
m.name.should == "ConstantSpecs::CS_CONST1000"
|
m.name.should == "ConstantSpecs::CS_CONST1000"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not set the name of a module scoped by an anonymous module" do
|
ruby_version_is ""..."3.0" do
|
||||||
a, b = Module.new, Module.new
|
it "does not set the name of a module scoped by an anonymous module" do
|
||||||
a.const_set :B, b
|
a, b = Module.new, Module.new
|
||||||
b.name.should be_nil
|
a.const_set :B, b
|
||||||
|
b.name.should be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is "3.0" do
|
||||||
|
it "sets the name of a module scoped by an anonymous module" do
|
||||||
|
a, b = Module.new, Module.new
|
||||||
|
a.const_set :B, b
|
||||||
|
b.name.should.end_with? '::B'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sets the name of contained modules when assigning a toplevel anonymous module" do
|
it "sets the name of contained modules when assigning a toplevel anonymous module" do
|
||||||
|
@ -6,10 +6,20 @@ describe "Module#name" do
|
|||||||
Module.new.name.should be_nil
|
Module.new.name.should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is nil when assigned to a constant in an anonymous module" do
|
ruby_version_is ""..."3.0" do
|
||||||
m = Module.new
|
it "is nil when assigned to a constant in an anonymous module" do
|
||||||
m::N = Module.new
|
m = Module.new
|
||||||
m::N.name.should be_nil
|
m::N = Module.new
|
||||||
|
m::N.name.should be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is "3.0" do
|
||||||
|
it "is not nil when assigned to a constant in an anonymous module" do
|
||||||
|
m = Module.new
|
||||||
|
m::N = Module.new
|
||||||
|
m::N.name.should.end_with? '::N'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is not nil for a nested module created with the module keyword" do
|
it "is not nil for a nested module created with the module keyword" do
|
||||||
|
@ -69,10 +69,20 @@ describe "Assigning an anonymous module to a constant" do
|
|||||||
mod.name.should == "ModuleSpecs_CS1"
|
mod.name.should == "ModuleSpecs_CS1"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not set the name of a module scoped by an anonymous module" do
|
ruby_version_is ""..."3.0" do
|
||||||
a, b = Module.new, Module.new
|
it "does not set the name of a module scoped by an anonymous module" do
|
||||||
a::B = b
|
a, b = Module.new, Module.new
|
||||||
b.name.should be_nil
|
a::B = b
|
||||||
|
b.name.should be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ruby_version_is "3.0" do
|
||||||
|
it "sets the name of a module scoped by an anonymous module" do
|
||||||
|
a, b = Module.new, Module.new
|
||||||
|
a::B = b
|
||||||
|
b.name.should.end_with? '::B'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sets the name of contained modules when assigning a toplevel anonymous module" do
|
it "sets the name of contained modules when assigning a toplevel anonymous module" do
|
||||||
|
@ -767,13 +767,13 @@ class TestModule < Test::Unit::TestCase
|
|||||||
n = Module.new
|
n = Module.new
|
||||||
m.const_set(:N, n)
|
m.const_set(:N, n)
|
||||||
assert_nil(m.name)
|
assert_nil(m.name)
|
||||||
assert_nil(n.name)
|
assert_match(/::N$/, n.name)
|
||||||
assert_equal([:N], m.constants)
|
assert_equal([:N], m.constants)
|
||||||
m.module_eval("module O end")
|
m.module_eval("module O end")
|
||||||
assert_equal([:N, :O], m.constants.sort)
|
assert_equal([:N, :O], m.constants.sort)
|
||||||
m.module_eval("class C; end")
|
m.module_eval("class C; end")
|
||||||
assert_equal([:C, :N, :O], m.constants.sort)
|
assert_equal([:C, :N, :O], m.constants.sort)
|
||||||
assert_nil(m::N.name)
|
assert_match(/::N$/, m::N.name)
|
||||||
assert_match(/\A#<Module:.*>::O\z/, m::O.name)
|
assert_match(/\A#<Module:.*>::O\z/, m::O.name)
|
||||||
assert_match(/\A#<Module:.*>::C\z/, m::C.name)
|
assert_match(/\A#<Module:.*>::C\z/, m::C.name)
|
||||||
self.class.const_set(:M, m)
|
self.class.const_set(:M, m)
|
||||||
@ -2724,6 +2724,12 @@ class TestModule < Test::Unit::TestCase
|
|||||||
assert_not_predicate m.clone(freeze: false), :frozen?
|
assert_not_predicate m.clone(freeze: false), :frozen?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_module_name_in_singleton_method
|
||||||
|
s = Object.new.singleton_class
|
||||||
|
mod = s.const_set(:Foo, Module.new)
|
||||||
|
assert_match(/::Foo$/, mod.name, '[Bug #14895]')
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def assert_top_method_is_private(method)
|
def assert_top_method_is_private(method)
|
||||||
|
17
variable.c
17
variable.c
@ -2854,14 +2854,15 @@ rb_const_set(VALUE klass, ID id, VALUE val)
|
|||||||
else {
|
else {
|
||||||
int parental_path_permanent;
|
int parental_path_permanent;
|
||||||
VALUE parental_path = classname(klass, &parental_path_permanent);
|
VALUE parental_path = classname(klass, &parental_path_permanent);
|
||||||
if (!NIL_P(parental_path)) {
|
if (NIL_P(parental_path)) {
|
||||||
if (parental_path_permanent && !val_path_permanent) {
|
parental_path = rb_funcall(klass, rb_intern("to_s"), 0);
|
||||||
set_namespace_path(val, build_const_path(parental_path, id));
|
}
|
||||||
}
|
if (parental_path_permanent && !val_path_permanent) {
|
||||||
else if (!parental_path_permanent && NIL_P(val_path)) {
|
set_namespace_path(val, build_const_path(parental_path, id));
|
||||||
rb_ivar_set(val, tmp_classpath, build_const_path(parental_path, id));
|
}
|
||||||
}
|
else if (!parental_path_permanent && NIL_P(val_path)) {
|
||||||
}
|
rb_ivar_set(val, tmp_classpath, build_const_path(parental_path, id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user