Skip to content

Fix method_receiver for self at class body level#662

Merged
alexcrocha merged 2 commits intomainfrom
03-12-fix_method_receiver_for_self_at_class_body_level
Mar 13, 2026
Merged

Fix method_receiver for self at class body level#662
alexcrocha merged 2 commits intomainfrom
03-12-fix_method_receiver_for_self_at_class_body_level

Conversation

@alexcrocha
Copy link
Contributor

@alexcrocha alexcrocha commented Mar 12, 2026

Fixes #657

At class body level, self is the class object. Method calls on it (self.bar, bar, Foo.bar) all resolve through the singleton class. method_receiver was not setting is_singleton_name in the SelfNode | None arm for Class, Module, and SingletonClass definitions, so self.bar inside class Foo returned receiver Foo while Foo.bar returned <Foo>.

Now all three produce <Foo> consistently.

Tests

Test Ruby source Expected receiver
self_at_class_level_matches_constant self.bar inside class Foo vs Foo.baz outside Both <Foo>
implicit_at_class_level bar inside class Foo <Foo>
self_at_module_level self.bar inside module Foo <Foo>
self_inside_singleton_class self.bar inside class << self <<Foo>>
implicit_inside_singleton_class bar inside class << self <<Foo>>
at_top_level self.bar at top level Object

Count assertions in indexing_the_same_document_twice updated: alias_method calls at class body level now create singleton names and constant references.

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@alexcrocha alexcrocha force-pushed the 03-12-fix_method_receiver_for_self_at_class_body_level branch from 8187d55 to e7f7901 Compare March 12, 2026 22:36
@alexcrocha alexcrocha marked this pull request as ready for review March 12, 2026 22:36
@alexcrocha alexcrocha requested a review from a team as a code owner March 12, 2026 22:36
@alexcrocha alexcrocha self-assigned this Mar 12, 2026
@alexcrocha alexcrocha force-pushed the 03-12-fix_method_receiver_for_self_at_class_body_level branch from e7f7901 to b5d7bb1 Compare March 12, 2026 23:06
context.index_uri("file:///foo.rb", source);
assert_eq!(44, context.graph().definitions.len());
assert_eq!(7, context.graph().constant_references.len());
assert_eq!(9, context.graph().constant_references.len());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create singleton class for attr_reader calls too? If not, should we stop creating one for alias_method calls? Can be a follow up PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair question. Let's do a follow up PR for this. I don't know the answer yet but I'll leave it for tomorrow

At class body level, self is the class object. Method calls on it
(self.bar, bar, Foo.bar) all resolve through the singleton class.
method_receiver now sets is_singleton_name for Class, Module, and
SingletonClass definitions, producing <Foo> consistently
@alexcrocha alexcrocha force-pushed the 03-12-fix_method_receiver_for_self_at_class_body_level branch from b5d7bb1 to 3f5e7fa Compare March 13, 2026 03:39
@alexcrocha alexcrocha merged commit 187201e into main Mar 13, 2026
31 checks passed
@alexcrocha alexcrocha deleted the 03-12-fix_method_receiver_for_self_at_class_body_level branch March 13, 2026 04:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

method_receiver returns inconsistent receiver for self.method

2 participants