8000 RBS: Check signature parameters kind match the actual Ruby definition by Morriar · Pull Request #8830 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

RBS: Check signature parameters kind match the actual Ruby definition #8830

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

Merged
merged 9 commits into from
May 7, 2025

Conversation

Morriar
Copy link
Collaborator
@Morriar Morriar commented May 5, 2025

Motivation

I found a few cases where the kind of parameter used in the RBS signature was not matching the actual kind expected.

For example, all these cases should error :

#: (x: String) -> void
def foo(x); end

#: (String) -> void
def bar(x:); end

#: (^() -> void) -> void
def baz(&blk); end

but they do not

Test plan

See included automated tests.

Morriar added 3 commits May 5, 2025 17:26
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar requested a review from a team as a code owner May 5, 2025 21:31
@Morriar Morriar requested review from neilparikh and removed request for a team May 5, 2025 21:31
return kind;
}

bool checkParameterKind(core::MutableContext ctx, const RBSDeclaration &declaration, const RBSArg &arg,
Copy link
Collaborator

Choose a reason for hiding this comment

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

declaration is unused.


bool checkParameterKind(core::MutableContext ctx, const RBSDeclaration &declaration, const RBSArg &arg,
const parser::Node *methodArg) {
auto error = false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we rename this to something like kindMatches? When I saw error, I expected it hold some sort of error object, not a boolean.


if (error) {
if (auto e = ctx.beginError(arg.loc, core::errors::Rewriter::RBSIncorrectParameterKind)) {
e.setHeader("Argument kind mismatch for `{}`, expected `{}`, got `{}`", nodeName(methodArg).show(ctx.state),
Copy link
Collaborator

Choose a reason for hiding this comment

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

The error message isn't super clear to me; it's not obvious that expected is the arg in the def, and got is the one in the sig. Maybe something like:

error: Argument kind mismatch for p, method declares positional, but RBS sig declares optional positional

#: (^() -> void) -> void
# ^^^^^^^^^^^ error: Argument kind mismatch for `blk`, expected `block`, got `positiona 8000 l`
def sig_mismatch11(&blk); end

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a test case where the mismatch is in one of the args to the block?

Morriar added 4 commits May 6, 2025 09:47
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar requested a review from neilparikh May 6, 2025 14:22
Comment on lines +304 to +306
// TODO: fix block location in RBS parser
loc.beginLoc = loc.beginLoc + 1;
loc.endLoc = loc.endLoc + 2;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The location given by the RBS parser is incorrectly offset, I'll fix this upstream and remove this adjustment in a follow up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Morriar added 2 commits May 6, 2025 16:24
TODO: fix the location in the RBS parser

Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
@Morriar Morriar force-pushed the at-rbs-check-params branch from 673db09 to a2fff8f Compare May 6, 2025 20:24
@neilparikh neilparikh merged commit ac1aa7a into sorbet:master May 7, 2025
14 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
0