Skip to content

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

Merged
soutaro merged 1 commit into
ruby:masterfrom
RYOTA-KOBA:support-singleton-method-def-in-inline-parser
May 12, 2026
Merged

Support def self.method_name singleton method in inline parser#2935
soutaro merged 1 commit into
ruby:masterfrom
RYOTA-KOBA:support-singleton-method-def-in-inline-parser

Conversation

@RYOTA-KOBA
Copy link
Copy Markdown
Contributor

@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
@soutaro soutaro added this to the RBS 4.1 milestone May 12, 2026
Copy link
Copy Markdown
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

Thanks! 👍

@soutaro soutaro merged commit 01afaa8 into ruby:master May 12, 2026
24 of 26 checks passed
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.

2 participants