mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
5 changed files with 59 additions and 22 deletions
|
@ -20,10 +20,20 @@ describe "Module#const_set" do
|
|||
m.name.should == "ConstantSpecs::CS_CONST1000"
|
||||
end
|
||||
|
||||
it "does not set 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 be_nil
|
||||
ruby_version_is ""..."3.0" do
|
||||
it "does not set 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 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
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
it "is nil when assigned to a constant in an anonymous module" do
|
||||
m = Module.new
|
||||
m::N = Module.new
|
||||
m::N.name.should be_nil
|
||||
ruby_version_is ""..."3.0" do
|
||||
it "is nil when assigned to a constant in an anonymous module" do
|
||||
m = Module.new
|
||||
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
|
||||
|
||||
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"
|
||||
end
|
||||
|
||||
it "does not set the name of a module scoped by an anonymous module" do
|
||||
a, b = Module.new, Module.new
|
||||
a::B = b
|
||||
b.name.should be_nil
|
||||
ruby_version_is ""..."3.0" do
|
||||
it "does not set the name of a module scoped by an anonymous module" do
|
||||
a, b = Module.new, Module.new
|
||||
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
|
||||
|
||||
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
|
||||
m.const_set(:N, n)
|
||||
assert_nil(m.name)
|
||||
assert_nil(n.name)
|
||||
assert_match(/::N$/, n.name)
|
||||
assert_equal([:N], m.constants)
|
||||
m.module_eval("module O end")
|
||||
assert_equal([:N, :O], m.constants.sort)
|
||||
m.module_eval("class C; end")
|
||||
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:.*>::C\z/, m::C.name)
|
||||
self.class.const_set(:M, m)
|
||||
|
@ -2724,6 +2724,12 @@ class TestModule < Test::Unit::TestCase
|
|||
assert_not_predicate m.clone(freeze: false), :frozen?
|
||||
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
|
||||
|
||||
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 {
|
||||
int parental_path_permanent;
|
||||
VALUE parental_path = classname(klass, &parental_path_permanent);
|
||||
if (!NIL_P(parental_path)) {
|
||||
if (parental_path_permanent && !val_path_permanent) {
|
||||
set_namespace_path(val, 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));
|
||||
}
|
||||
}
|
||||
if (NIL_P(parental_path)) {
|
||||
parental_path = rb_funcall(klass, rb_intern("to_s"), 0);
|
||||
}
|
||||
if (parental_path_permanent && !val_path_permanent) {
|
||||
set_namespace_path(val, 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…
Add table
Reference in a new issue