Skip to content

Support def self.method_name singleton method in inline parser#2935

Open
RYOTA-KOBA wants to merge 1 commit intoruby:masterfrom
RYOTA-KOBA:support-singleton-method-def-in-inline-parser
Open

Support def self.method_name singleton method in inline parser#2935
RYOTA-KOBA wants to merge 1 commit intoruby:masterfrom
RYOTA-KOBA:support-singleton-method-def-in-inline-parser

Conversation

@RYOTA-KOBA
Copy link
Copy Markdown

@RYOTA-KOBA RYOTA-KOBA commented Apr 18, 2026

Previously, the inline parser emitted a diagnostic error for any method definition with a receiver (def self.foo, def obj.foo, etc.) and skipped processing it entirely. This meant that singleton methods defined with def self.method_name could not have their type annotations recognized.

This change adds support for def self.method_name by:

  • Checking Prism::SelfNode specifically in visit_def_node and only rejecting non-self receivers (e.g., def obj.foo)
  • Adding a kind attribute (:instance | :singleton) to DefMember to distinguish between instance and singleton methods
  • Propagating kind through Environment#resolve_type_names
  • Routing singleton DefMembers to build_singleton in MethodBuilder

All existing inline annotation formats (#:, # @rbs, # @rbs param:) work with singleton methods as well.

In addition

Could you also add the appropriate milestone to this PR? I do not have permission to set milestones from the GitHub UI. If this PR does not need one, please add the no-milestone label instead.

Previously, the inline parser emitted a diagnostic error for any method
definition with a receiver (`def self.foo`, `def obj.foo`, etc.) and
skipped processing it entirely. This meant that singleton methods defined
with `def self.method_name` could not have their type annotations
recognized.

This change adds support for `def self.method_name` by:

- Checking `Prism::SelfNode` specifically in `visit_def_node` and only
  rejecting non-self receivers (e.g., `def obj.foo`)
- Adding a `kind` attribute (`:instance` | `:singleton`) to `DefMember`
  to distinguish between instance and singleton methods
- Propagating `kind` through `Environment#resolve_type_names`
- Routing singleton `DefMember`s to `build_singleton` in `MethodBuilder`

All existing inline annotation formats (`#:`, `# @rbs`, `# @RBS param:`)
work with singleton methods as well.
@RYOTA-KOBA RYOTA-KOBA marked this pull request as ready for review April 18, 2026 05: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.

1 participant