Skip to content

Commit

Permalink
Merge pull request ruby#1800 from ruby/no-forwarding-in-block
Browse files Browse the repository at this point in the history
Forwarding parameter fixes
  • Loading branch information
kddnewton authored Nov 10, 2023
2 parents 0efbc7d + b7850f2 commit 884496c
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 50 deletions.
4 changes: 4 additions & 0 deletions lib/prism/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def self.prism_locals(source)
*params.keywords.grep(OptionalKeywordParameterNode).map(&:name),
]

if params.keyword_rest.is_a?(ForwardingParameterNode)
sorted.push(:*, :&, :"...")
end

sorted << AnonymousLocal if params.keywords.any?

# Recurse down the parameter tree to find any destructured
Expand Down
32 changes: 15 additions & 17 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -5569,15 +5569,6 @@ pm_parser_local_add(pm_parser_t *parser, pm_constant_id_t constant_id) {
}
}

/**
* Add a local variable from a constant string to the current scope.
*/
static inline void
pm_parser_local_add_constant(pm_parser_t *parser, const char *start, size_t length) {
pm_constant_id_t constant_id = pm_parser_constant_id_constant(parser, start, length);
if (constant_id != 0) pm_parser_local_add(parser, constant_id);
}

/**
* Add a local variable from a location to the current scope.
*/
Expand Down Expand Up @@ -11029,7 +11020,7 @@ parse_parameters(
pm_binding_power_t binding_power,
bool uses_parentheses,
bool allows_trailing_comma,
bool allows_forwarding_parameter
bool allows_forwarding_parameters
) {
pm_parameters_node_t *params = pm_parameters_node_create(parser);
bool looping = true;
Expand Down Expand Up @@ -11064,7 +11055,10 @@ parse_parameters(
pm_parser_local_add_token(parser, &name);
} else {
name = not_provided(parser);
pm_parser_local_add_token(parser, &operator);

if (allows_forwarding_parameters) {
pm_parser_local_add_token(parser, &operator);
}
}

pm_block_parameter_node_t *param = pm_block_parameter_node_create(parser, &name, &operator);
Expand All @@ -11078,17 +11072,15 @@ parse_parameters(
break;
}
case PM_TOKEN_UDOT_DOT_DOT: {
if (!allows_forwarding_parameter) {
if (!allows_forwarding_parameters) {
pm_parser_err_current(parser, PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES);
}

if (order > PM_PARAMETERS_ORDER_NOTHING_AFTER) {
update_parameter_state(parser, &parser->current, &order);
parser_lex(parser);

if (allows_forwarding_parameter) {
pm_parser_local_add_constant(parser, "*", 1);
pm_parser_local_add_constant(parser, "&", 1);
if (allows_forwarding_parameters) {
pm_parser_local_add_token(parser, &parser->previous);
}

Expand Down Expand Up @@ -11244,7 +11236,10 @@ parse_parameters(
pm_parser_local_add_token(parser, &name);
} else {
name = not_provided(parser);
pm_parser_local_add_token(parser, &operator);

if (allows_forwarding_parameters) {
pm_parser_local_add_token(parser, &operator);
}
}

pm_rest_parameter_node_t *param = pm_rest_parameter_node_create(parser, &operator, &name);
Expand Down Expand Up @@ -11276,7 +11271,10 @@ parse_parameters(
pm_parser_local_add_token(parser, &name);
} else {
name = not_provided(parser);
pm_parser_local_add_token(parser, &operator);

if (allows_forwarding_parameters) {
pm_parser_local_add_token(parser, &operator);
}
}

param = (pm_node_t *) pm_keyword_rest_parameter_node_create(parser, &operator, &name);
Expand Down
4 changes: 2 additions & 2 deletions test/prism/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ def test_method_parameters_after_arguments_forwarding
nil
),
nil,
[:*, :&, :"...", :a],
[:"...", :a],
Location(),
nil,
Location(),
Expand Down Expand Up @@ -800,7 +800,7 @@ def test_double_arguments_forwarding
nil,
ParametersNode([], [], nil, [], [], ForwardingParameterNode(), nil),
nil,
[:*, :&, :"..."],
[:"..."],
Location(),
nil,
Location(),
Expand Down
8 changes: 4 additions & 4 deletions test/prism/snapshots/methods.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/defn_arg_forward_args.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/defn_args_forward_args.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/defn_forward_args.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/prism/snapshots/unparser/corpus/literal/block.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions test/prism/snapshots/whitequark/blockargs.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/prism/snapshots/whitequark/forward_arg.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions test/prism/snapshots/whitequark/forward_arg_with_open_args.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test/prism/snapshots/whitequark/forward_args_legacy.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/prism/snapshots/whitequark/send_lambda.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/prism/snapshots/whitequark/trailing_forward_arg.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 884496c

Please sign in to comment.