From c85d1cda86d75ee2c3f7b42f22c543409cb5a186 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 11 Mar 2022 21:43:47 -0800 Subject: Fix Module#const_source_location for autoload constants with direct requires If an autoload exists for a constant, but the path for the autoload was required, const_source_location would return [false, 0] instead of the actual file and line. This fixes it by setting the appropriate file and line in rb_const_set, and saving the file and line in const_tbl_update before they get reset by current_autoload_data. Fixes [Bug #18624] --- test/ruby/test_autoload.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'test/ruby/test_autoload.rb') diff --git a/test/ruby/test_autoload.rb b/test/ruby/test_autoload.rb index bac6337eb9..bd875ec008 100644 --- a/test/ruby/test_autoload.rb +++ b/test/ruby/test_autoload.rb @@ -443,6 +443,23 @@ p Foo::Bar end end + def test_source_location_after_require + bug = "Bug18624" + Dir.mktmpdir('autoload') do |tmpdir| + path = "#{tmpdir}/test-#{bug}.rb" + File.write(path, "C::#{bug} = __FILE__\n") + assert_separately(%W[-I #{tmpdir}], "#{<<-"begin;"}\n#{<<-"end;"}") + begin; + class C; end + C.autoload(:Bug18624, #{path.dump}) + require #{path.dump} + assert_equal [#{path.dump}, 1], C.const_source_location(#{bug.dump}) + assert_equal #{path.dump}, C.const_get(#{bug.dump}) + assert_equal [#{path.dump}, 1], C.const_source_location(#{bug.dump}) + end; + end + end + def test_no_memory_leak assert_no_memory_leak([], '', "#{<<~"begin;"}\n#{<<~'end;'}", 'many autoloads', timeout: 60) begin; -- cgit v1.2.3