Resolve constants against enclosing lexical scope in class << self#764
Merged
Resolve constants against enclosing lexical scope in class << self#764
class << self#764Conversation
Member
|
Oh, wow I can't believe we forgot to add a test for that. Thanks for the PR! The scenarios are all great, but the fix is actually significantly simpler. The resolution algorithm already walks lexical scopes correctly, but we're mistakenly not associating the nesting with the name objects for singleton. Here's the fix (needs some test updates too, but logic changes it's just this): |
e749f3b to
72890a9
Compare
vinistock
approved these changes
Apr 24, 2026
Member
vinistock
left a comment
There was a problem hiding this comment.
The failure for lint is fixed on main, so a rebase will probably fix it. Thanks for the contribution!
Constants referenced inside a `class << self` body (or inside methods
defined in one) did not resolve against the enclosing lexical scope.
For example, `Sibling` in this snippet was left unresolved:
module A
module B
class Sibling; end
class Main
class << self
def m
Sibling
end
end
end
end
end
The resolution algorithm already walks lexical scopes correctly — it
follows the `nesting` linked list on each `Name`. The bug was in the
indexer: when the singleton class `Name` was created, its `nesting`
was set to `None`, so the walk terminated at the singleton class frame
and never reached `A::B`.
Populate the singleton class `Name`'s `nesting` with the current
lexical scope at the point of the `class << self` block, matching how
regular classes and modules are indexed.
Two existing tests are updated to reflect the new name identity:
`<Foo>` with a nesting is a different interned `Name` than `<Foo>`
without one.
72890a9 to
998687f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a constant-resolution bug in
class << selfbodies. Constants referenced from inside a singleton class block (or from methods defined inside one) were not resolving against the enclosing lexical scope. For example:Root cause
The resolution algorithm already walks lexical scopes correctly; it follows the
nestinglinked list on eachName. The bug was in the indexer: when the singleton classNamewas created, itsnestingwas set toNone, so the walk terminated at the singleton class frame and never reachedA::BorA.Fix
One-line change in
RubyIndexer::visit_singleton_class_node: populate the singleton className'snestingwith the current lexical scope at the point of theclass << selfblock, matching how regular classes and modules are indexed.Two existing tests are updated to reflect the new
Nameidentity:<Foo>with a nesting interns to a differentNamethan<Foo>without one.Tests
Added 5 tests covering:
Siblingused inside a method inclass << selfSiblingused insideclass << selfnested under an outer classSiblingused directly in theclass << selfbody (not inside a method)All 528 rubydex tests pass; clippy and fmt are clean.