From: "Eregon (Benoit Daloze)" Date: 2022-03-13T12:53:24+00:00 Subject: [ruby-core:107881] [Ruby master Bug#18622] const_get still looks in Object, while lexical constant lookup no longer does Issue #18622 has been updated by Eregon (Benoit Daloze). That makes sense, I think we should improve `const_get` docs to says it's like `module A; B; end` and not `A::B` (which I'd think I'm not the only one to assume). For example I think it's nice to be able to implement `mod.const_lookup("B::C")` (i.e., before const_get handled `::` paths) as `path.split('::').inject(mod) { _1.const_get(_2) }` but that's actually not fully equivalent. Regarding Object in the nesting, I feel it's consistency with the general situation for the top-level: * `default definee`: Object (clearly the case) * `self`: main, an Object (clearly the case) * `cref`: Object (clearly the case, but that's not same as being in the nesting unfortunately) So the top-level is very similar to: ```ruby class Object private self = main # not instance_eval, that would change the default definee end ``` But indeed with more subtleties for constant lookup so `Object` is looked last, after the module/class's ancestors. I wish we could model constant lookup with just a nesting and have the invariant `cref == nesting.first`, but that doesn't hold for the top-level (AFAIK only for that case), unless the root constant scope is treated specially (as it is in TruffleRuby, and as we see the top-level constant scope is also special semantically anyway). ---------------------------------------- Bug #18622: const_get still looks in Object, while lexical constant lookup no longer does https://bugs.ruby-lang.org/issues/18622#change-96823 * Author: Eregon (Benoit Daloze) * Status: Open * Priority: Normal * ruby -v: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux] * Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- There is some inconsistency here between literal constant lookup and the meta API (const_get). Lexical constant lookup no longer uses a special case for Object, and this is good as it avoids surprises: #11547 However, `const_get` still looks in Object, even though that's confusing, inconsistent and IMHO shouldn't really happen. ```ruby module ConstantSpecsTwo Foo = :cs_two_foo end module ConstantSpecs end p ConstantSpecs.const_get("ConstantSpecsTwo::Foo") # => :cs_two_foo p ConstantSpecs::ConstantSpecsTwo::Foo # => const_get.rb:9:in `
': uninitialized constant ConstantSpecs::ConstantSpecsTwo (NameError) ``` I think we should change it so both behave the same (i.e., NameError). It's like if `cd /foo/bar` would go to `/bar` if `/foo/bar` does not exist and `/bar` does. `const_get` is a meta API so it cannot know the surrounding `Module.nesting`, but so I think it should consider the receiver of `const_get` as the only nesting (so just `ConstantSpecs` in this case, not `Object`). Note this does not affect nested constants inside the `const_get` like `Foo` above (another way to look at the inconsistency that the first component is treated differently). From https://bugs.ruby-lang.org/issues/11547#note-19 -- https://bugs.ruby-lang.org/ Unsubscribe: