mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[Bug #18813] Warn when autoload has to lookup in parent namespace
This is a verbose mode only warning.
This commit is contained in:
parent
e711711539
commit
eca31d24d6
Notes:
git
2022-06-18 21:49:23 +09:00
3 changed files with 63 additions and 0 deletions
|
@ -577,6 +577,36 @@ describe "Module#autoload" do
|
|||
end
|
||||
end
|
||||
|
||||
ruby_version_is "3.2" do
|
||||
it "warns once in verbose mode if the constant was defined in a parent scope" do
|
||||
ScratchPad.record -> {
|
||||
ModuleSpecs::DeclaredInCurrentDefinedInParent = :declared_in_current_defined_in_parent
|
||||
}
|
||||
|
||||
module ModuleSpecs
|
||||
module Autoload
|
||||
autoload :DeclaredInCurrentDefinedInParent, fixture(__FILE__, "autoload_callback.rb")
|
||||
self.autoload?(:DeclaredInCurrentDefinedInParent).should == fixture(__FILE__, "autoload_callback.rb")
|
||||
const_defined?(:DeclaredInCurrentDefinedInParent).should == true
|
||||
|
||||
-> {
|
||||
DeclaredInCurrentDefinedInParent
|
||||
}.should complain(
|
||||
/Expected .*autoload_callback.rb to define ModuleSpecs::Autoload::DeclaredInCurrentDefinedInParent but it didn't/,
|
||||
verbose: true,
|
||||
)
|
||||
|
||||
-> {
|
||||
DeclaredInCurrentDefinedInParent
|
||||
}.should_not complain(/.*/, verbose: true)
|
||||
self.autoload?(:DeclaredInCurrentDefinedInParent).should == nil
|
||||
const_defined?(:DeclaredInCurrentDefinedInParent).should == false
|
||||
ModuleSpecs.const_defined?(:DeclaredInCurrentDefinedInParent).should == true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ruby_version_is "3.1" do
|
||||
it "looks up in parent scope after failed autoload" do
|
||||
@remove << :DeclaredInCurrentDefinedInParent
|
||||
|
|
|
@ -479,6 +479,7 @@ p Foo::Bar
|
|||
File.write(autoload_path, '')
|
||||
|
||||
assert_separately(%W[-I #{tmpdir}], <<-RUBY)
|
||||
$VERBOSE = nil
|
||||
path = #{File.realpath(autoload_path).inspect}
|
||||
autoload :X, path
|
||||
assert_equal(path, Object.autoload?(:X))
|
||||
|
@ -557,4 +558,20 @@ p Foo::Bar
|
|||
RUBY
|
||||
end
|
||||
end
|
||||
|
||||
def test_autoload_parent_namespace
|
||||
Dir.mktmpdir('autoload') do |tmpdir|
|
||||
autoload_path = File.join(tmpdir, "some_const.rb")
|
||||
File.write(autoload_path, 'class SomeConst; end')
|
||||
|
||||
assert_separately(%W[-I #{tmpdir}], <<-RUBY)
|
||||
module SomeNamespace
|
||||
autoload :SomeConst, #{File.realpath(autoload_path).inspect}
|
||||
assert_warning(%r{/some_const\.rb to define SomeNamespace::SomeConst but it didn't}) do
|
||||
assert_not_nil SomeConst
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
16
variable.c
16
variable.c
|
@ -2672,6 +2672,22 @@ autoload_try_load(VALUE _arguments)
|
|||
result = Qfalse;
|
||||
|
||||
rb_const_remove(arguments->module, arguments->name);
|
||||
|
||||
if (arguments->module == rb_cObject) {
|
||||
rb_warning(
|
||||
"Expected %"PRIsVALUE" to define %"PRIsVALUE" but it didn't",
|
||||
arguments->autoload_data->feature,
|
||||
ID2SYM(arguments->name)
|
||||
);
|
||||
}
|
||||
else {
|
||||
rb_warning(
|
||||
"Expected %"PRIsVALUE" to define %"PRIsVALUE"::%"PRIsVALUE" but it didn't",
|
||||
arguments->autoload_data->feature,
|
||||
arguments->module,
|
||||
ID2SYM(arguments->name)
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Otherwise, it was loaded, copy the flags from the autoload constant:
|
||||
|
|
Loading…
Reference in a new issue