Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signature matching for positional parameters #2541

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

vinistock
Copy link
Member

Motivation

Step towards #2363

To determine which signature overload to show, we are going to need to match a declaration signature to the arguments of a method call. Doing it all in a single PR felt a bit too complex, so this PR only adds the method matches? to Signature and only implements matching for positional arguments. I will follow up with the implementation for keyword arguments.

Implementation

For matching positional arguments, we essentially need to check if the number of arguments in the method call are within the range of possible arguments that the method accepts. For example:

def bar(a, b = 2)
end

# The range for valid calls here is 1..2 because you can call `bar` with 1 or 2 arguments
bar(1) # ok!
bar(1, 2) # ok!

There are also special rules for splats and forwarding arguments. In general, these are not statically analyzable and we just bail out saying that the signature does match, but there are a few scenarios where we can do at least partial matching. For example:

def bar(a, *b)
end

# We can't statically analyze anything related to the splat parameter, but we know
# that `bar` requires at least one positional parameter. In this case, the range is
# 1..INFINITY

bar # not ok!
bar(1) # ok!
bar(1, 2) # ok!
bar(*a) # ok!

See the tests for more examples.

Automated Tests

Added tests.

@vinistock vinistock added enhancement New feature or request server This pull request should be included in the server gem's release notes labels Sep 11, 2024
@vinistock vinistock self-assigned this Sep 11, 2024
@vinistock vinistock requested a review from a team as a code owner September 11, 2024 19:21
@vinistock vinistock requested review from andyw8 and st0012 September 11, 2024 19:21
Base automatically changed from vs-index-forwarding-parameters to main September 12, 2024 13:10
@vinistock vinistock force-pushed the vs-add-signature-matching-for-positional-parameters branch from c55e16c to e89d6ba Compare September 12, 2024 13:22
@vinistock vinistock enabled auto-merge (squash) September 12, 2024 13:24
@vinistock vinistock merged commit 332b464 into main Sep 12, 2024
36 checks passed
@vinistock vinistock deleted the vs-add-signature-matching-for-positional-parameters branch September 12, 2024 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants