Skip to content

Index method definitions from RBS#633

Merged
soutaro merged 1 commit intomainfrom
index-methods
Mar 19, 2026
Merged

Index method definitions from RBS#633
soutaro merged 1 commit intomainfrom
index-methods

Conversation

@soutaro
Copy link
Copy Markdown
Contributor

@soutaro soutaro commented Mar 4, 2026

This PR implements indexing method definitions def from RBS.

  • Support all parameter kinds: required/optional positional, rest, post, required/optional keyword, rest keyword, block, and untyped parameters (?) -> void
  • Support overloaded method signatures (def foo: (String) -> Integer | (Integer) -> String)
  • Handle visibility (public/private), singleton methods (def self.foo), and module_function (def self?.foo)
  • Generate default names for unnamed RBS parameters (arg0, arg1, ..., args, kwargs, block)

@soutaro soutaro self-assigned this Mar 4, 2026
@soutaro soutaro added the enhancement New feature or request label Mar 4, 2026
Comment thread rust/rubydex/src/indexing/rbs_indexer.rs Outdated
Comment thread rust/rubydex/src/indexing/rbs_indexer.rs Outdated
Comment thread rust/rubydex/src/indexing/rbs_indexer.rs Outdated
Copy link
Copy Markdown
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

Left some non blocking comments

Comment thread rust/rubydex/src/resolution.rs Outdated
Comment thread rust/rubydex/src/indexing/rbs_indexer.rs
Comment on lines +251 to +252
// Include the ** prefix (2 bytes before the node location)
let start = location.start() - 2;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are we doing the same for the Ruby indexer? I have the gut feeling that we would actually prefer not including the **.

For example, if we want to rename the parameter, we wouldn't edit the **, but only what comes after.

Anyway, let's just ensure it's consistent between both indexers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm fine to drop the **. Will fix the code to align with Ruby indexer. 👍

The problem is RBS type declaration may not have the name of parameter, and the &block parameter syntax is not supported.

def foo: (String, *Integer) { (Integer) -> String } -> void
          ^^^^^^   ^^^^^^^                                     ==> Unnamed parameters
                            ^^^^^^^^^^^^^^^^^^^^^^^            ==> No &block parameter

Assigning parameter names automatically like arg0 and &block would make sense for now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting, I hadn't considered that. Let's indeed use arg0 and block for now, but later we may need to reconsider how we handle parameters.

I guess there are two main scenarios:

  • An RBS file defines a method we did not know about. This case may need to default to arg0 and block, although it's a bit unfortunate. It would be nice if we could have proper names for them
  • An RBS file only adds types to an existing method we discovered from Ruby. In this case, the RBS annotations are essentially only assigning types, so maybe this one is handled a bit differently

Anyway, a problem for later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm. Mixing RBS and Ruby method definitions is a bit complicated.

I think there are two directions for using method signatures:

  1. For callers — type checking call expressions, showing documentation, parameter hints, ...
  2. For def implementation — type checking the implementation body (not sure yet what rubydex will do here)

In Steep:

  1. For callers RBS is the source of truth. (Steep simply ignores Ruby methods if RBS is missing.)
  2. For def implementations, it reasons the type of each parameter in the Ruby def syntax from RBS method types to type check the method body.

I think rubydex can take a similar approach for callers: use RBS information when it exists, and fall back to signatures from the Ruby method definition when RBS is missing. When RBS type definition exists, the absence of parameter names may not be a big problem, since the parameter types itself would help very well.

Comment on lines +238 to +239
self.source_at(&key.location()),
self.source_at(&value.location())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems like something we should probably add to the RBS crate for convenience (which we can do separately if preferred).

Essentially a name method that returns the byte slice.

@soutaro
Copy link
Copy Markdown
Contributor Author

soutaro commented Mar 18, 2026

I'd like to finish working on this PR. Will fixup the commit and improve the signature registration. And then merge this PR leaving other things to successor PRs.

@soutaro soutaro changed the title Index methods Index method definitions from RBS Mar 19, 2026
@soutaro soutaro marked this pull request as ready for review March 19, 2026 04:46
@soutaro soutaro requested a review from a team as a code owner March 19, 2026 04:46
}

#[must_use]
pub fn source_at(&self, offset: &Offset) -> &str {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I want this utility to test where an offset points to.

@soutaro soutaro merged commit 2070001 into main Mar 19, 2026
32 checks passed
@soutaro soutaro deleted the index-methods branch March 19, 2026 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants