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

Avoid translating implicit rest nodes in parameters #408

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions parser/prism/Translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,11 @@ unique_ptr<parser::Node> Translator::translate(pm_node_t *node) {
translateMultiInto(params, requireds);
translateMultiInto(params, optionals);

if (paramsNode->rest != nullptr)
params.emplace_back(translate(paramsNode->rest));
auto prismRestNode = paramsNode->rest;
// Implicit rest node in parameters don't need to be translated

Choose a reason for hiding this comment

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

Could you please elaborate on the "why"? What was the behaviour before?

Copy link
Member Author

Choose a reason for hiding this comment

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

Previously it simply entered the unreachable function and crash. And Sorbet parser's parse tree doesn't have a corresponding node for it. So I'm not sure how to expand the comment more 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Or do you think I should update it to be can NOT be translated?

Copy link

@amomchilov amomchilov Feb 12, 2025

Choose a reason for hiding this comment

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

Ah I see. Yes "cannot" fit better than "don't need to be" (which implies "can be, but isn't")

Playing around with this, it seems that handles it incorrectly. Found this bug report: sorbet#6407

So I'd suggest explaining that it's a known limitation:

Suggested change
// Implicit rest node in parameters don't need to be translated
// Known limitation: Sorbet doesn't handle implicit rest nodes for restructuring arguments to blocks.
// https://github.com/sorbet/sorbet/issues/6407

if (prismRestNode != nullptr && !PM_NODE_TYPE_P(prismRestNode, PM_IMPLICIT_REST_NODE)) {
params.emplace_back(translate(prismRestNode));
}

translateMultiInto(params, posts);
translateMultiInto(params, keywords);
Expand Down
19 changes: 19 additions & 0 deletions test/prism_regression/call_block_param.parse-tree.exp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ Begin {
}
body = NULL
}
Block {
send = Send {
receiver = NULL
method = <U foo>
args = [
]
}
args = Args {
args = [
Arg {
name = <U bar>
}
Arg {
name = <U baz>
}
]
}
body = NULL
}
Send {
receiver = NULL
method = <U foo>
Expand Down
2 changes: 2 additions & 0 deletions test/prism_regression/call_block_param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

foo { |bar; baz, qux| }

foo { |bar, baz,| }

foo(&forwarded_block)

foo&.bar {}
Expand Down
Loading