8000 [Feature #19979] Method definition with `&nil` by nobu · Pull Request #10020 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

[Feature #19979] Method definition with &nil #10020

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[PRISM] [Feature #19979] Method definition with &nil
  • Loading branch information
nobu committed Mar 18, 2024
commit ef94878116565fd1da9a79a3fa109299075dcc3f
13 changes: 12 additions & 1 deletion prism/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2662,6 +2662,18 @@ nodes:

nil
^^^
- name: NoBlockParameterNode
fields:
- name: operator_loc
type: location
- name: keyword_loc
type: location
comment: |
Represents the use of `&nil` inside method arguments.

def a(&nil)
^^^^
end
- name: NoKeywordsParameterNode
fields:
- name: operator_loc
Expand Down Expand Up @@ -2806,7 +2818,6 @@ nodes:
- NoKeywordsParameterNode
- name: block
type: node?
kind: BlockParameterNode
comment: |
Represents the list of parameters on a method, block, or lambda definition.

Expand Down
63 changes: 47 additions & 16 deletions prism/prism.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -5060,6 +5060,30 @@ pm_nil_node_create(pm_parser_t *parser, const pm_token_t *token) {
return node;
}

/**
* Allocate and initialize a new NoKeywordsParameterNode node.
*/
static pm_no_block_parameter_node_t *
pm_no_block_parameter_node_create(pm_parser_t *parser, const pm_token_t *operator, const pm_token_t *keyword) {
assert(operator->type == PM_TOKEN_UAMPERSAND || operator->type == PM_TOKEN_AMPERSAND);
assert(keyword->type == PM_TOKEN_KEYWORD_NIL);
pm_no_block_parameter_node_t *node = PM_ALLOC_NODE(parser, pm_no_block_parameter_node_t);

*node = (pm_no_block_parameter_node_t) {
{
.type = PM_NO_BLOCK_PARAMETER_NODE,
.location = {
.start = operator->start,
.end = keyword->end
}
},
.operator_loc = PM_LOCATION_TOKEN_VALUE(operator),
.keyword_loc = PM_LOCATION_TOKEN_VALUE(keyword)
};

return node;
}

/**
* Allocate and initialize a new NoKeywordsParameterNode node.
*/
Expand Down Expand Up @@ -5317,9 +5341,9 @@ pm_parameters_node_keyword_rest_set(pm_parameters_node_t *params, pm_node_t *par
* Set the block parameter on a ParametersNode node.
*/
static void
pm_parameters_node_block_set(pm_parameters_node_t *params, pm_block_parameter_node_t *param) {
pm_parameters_node_block_set(pm_parameters_node_t *params, pm_node_t *param) {
assert(params->block == NULL);
pm_parameters_node_location_set(params, (pm_node_t *) param);
pm_parameters_node_location_set(params, param);
params->block = param;
}

Expand Down Expand Up @@ -12925,27 +12949,34 @@ parse_parameters(
parser_lex(parser);

pm_token_t operator = parser->previous;
pm_token_t name;
pm_node_t *param;

bool repeated = false;
if (accept1(parser, PM_TOKEN_IDENTIFIER)) {
name = parser->previous;
repeated = pm_parser_parameter_name_check(parser, &name);
pm_parser_local_add_token(parser, &name);
if (accept1(parser, PM_TOKEN_KEYWORD_NIL)) {
param = (pm_node_t *) pm_no_block_parameter_node_create(parser, &operator, &parser->previous);
} else {
name = not_provided(parser);
parser->current_scope->parameters |= PM_SCOPE_PARAMETERS_FORWARDING_BLOCK;
}
pm_token_t name;

pm_block_parameter_node_t *param = pm_block_parameter_node_create(parser, &name, &operator);
if (repeated) {
pm_node_flag_set_repeated_parameter((pm_node_t *)param);
bool repeated = false;
if (accept1(parser, PM_TOKEN_IDENTIFIER)) {
name = parser->previous;
repeated = pm_parser_parameter_name_check(parser, &name);
pm_parser_local_add_token(parser, &name);
} else {
name = not_provided(parser);
parser->current_scope->parameters |= PM_SCOPE_PARAMETERS_FORWARDING_BLOCK;
}

param = (pm_node_t *) pm_block_parameter_node_create(parser, &name, &operator);
if (repeated) {
pm_node_flag_set_repeated_parameter(param);
}
}

if (params->block == NULL) {
pm_parameters_node_block_set(params, param);
} else {
pm_parser_err_node(parser, (pm_node_t *) param, PM_ERR_PARAMETER_BLOCK_MULTI);
pm_parameters_node_posts_append(params, (pm_node_t *) param);
pm_parser_err_node(parser, param, PM_ERR_PARAMETER_BLOCK_MULTI);
pm_parameters_node_posts_append(params, param);
}

break;
Expand Down
36 changes: 24 additions & 12 deletions prism_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6548,6 +6548,13 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,

return;
}
case PM_NO_BLOCK_PARAMETER_NODE: {
// def foo(&nil); end
// ^^^^
ISEQ_BODY(iseq)->param.block_start = -1;
ISEQ_BODY(iseq)->param.flags.has_block = TRUE;
return;
}
case PM_NO_KEYWORDS_PARAMETER_NODE: {
// def foo(**nil); end
// ^^^^^
Expand Down Expand Up @@ -7504,25 +7511,30 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
// def foo(a, (b, *c, d), e = 1, *f, g, (h, *i, j), k:, l: 1, **m, &n)
// ^^
if (parameters_node->block) {
body->param.block_start = local_index;
body->param.flags.has_block = true;
if (PM_NODE_TYPE(parameters_node->block) == PM_NO_BLOCK_PARAMETER_NODE) {
body->param.block_start = -1;
}
else {
body->param.block_start = local_index;

pm_constant_id_t name = ((pm_block_parameter_node_t *) parameters_node->block)->name;
pm_constant_id_t name = ((pm_block_parameter_node_t *) parameters_node->block)->name;

if (name) {
if (PM_NODE_FLAG_P(parameters_node->block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
ID local = pm_constant_id_lookup(scope_node, name);
local_table_for_iseq->ids[local_index] = local;
if (name) {
if (PM_NODE_FLAG_P(parameters_node->block, PM_PARAMETER_FLAGS_REPEATED_PARAMETER)) {
ID local = pm_constant_id_lookup(scope_node, name);
local_table_for_iseq->ids[local_index] = local;
}
else {
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
}
}
else {
pm_insert_local_index(name, local_index, index_lookup_table, local_table_for_iseq, scope_node);
pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
}
}
else {
pm_insert_local_special(idAnd, local_index, index_lookup_table, local_table_for_iseq);
}

local_index++;
local_index++;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/prism/location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ def test_NilNode
assert_location(NilNode, "nil")
end

def test_NoBlockParameterNode
assert_location(NoBlockParameterNode, "def foo(&nil); end", 8...12) { |node| node.parameters.block }
end

def test_NoKeywordsParameterNode
assert_location(NoKeywordsParameterNode, "def foo(**nil); end", 8...13) { |node| node.parameters.keyword_rest }
end
Expand Down
FF
0