From: "Eregon (Benoit Daloze) via ruby-core" Date: 2024-01-18T17:25:10+00:00 Subject: [ruby-core:116306] [Ruby master Bug#20188] `Module#const_source_location` returns wrong information when real constant was defined but autoload is still ongoing Issue #20188 has been updated by Eregon (Benoit Daloze). fxn (Xavier Noria) wrote in #note-8: > @Eregon I see what you mean, but you can't make that consistent, because by that logic > > ```ruby > Bar = 1 > p Bar > ``` > > should not print 1, right? The constant was created, the lookup for `Bar` found it, therefore its source location is not the autoload. The same way its value is no longer unknown. I assumed you mean that code would be in the autoloaded file (`/tmp/bar.rb`), and there could still be a `raise` after it. Yes it's true that the thread autoloading sees Bar as "defined" while it's not published globally yet. That's necessary though. But then is it better to be consistent for that one thread only during the autoload, or consistent after the autoload? I'd say the second matters more, the state during the autoload for the thread doing the autoload is always a bit weird and special, and it needs to be (e.g. `defined?(Bar)` would be nil there). After the autoload is done (exception or not), I believe `const_source_location` and `autoload?` should be consistent (either the autoload is still there or it's not, not a mix according to which method), and this change would break that (for the edge case of assigning the constant + exception after). ---------------------------------------- Bug #20188: `Module#const_source_location` returns wrong information when real constant was defined but autoload is still ongoing https://bugs.ruby-lang.org/issues/20188#change-106328 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal * ruby -v: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23] * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Ref: https://github.com/fxn/zeitwerk/issues/281 `const_source_location` keeps returning the location of the `autoload` call, even after the real constant was defined. It only starts returning the real constant location once the autoload was fully completed. ```ruby # /tmp/autoload.rb File.write("/tmp/const.rb", <<~RUBY) module Const LOCATION = Object.const_source_location(:Const) end RUBY autoload :Const, "/tmp/const" p Const::LOCATION ``` Expected Output: ```ruby ["/tmp/const.rb", 1] ``` Actual Output: ```ruby ["/tmp/autoload.rb", 8] ``` Potential patch: https://github.com/ruby/ruby/pull/9549 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/