Skip to content

Commit 0f47e18

Browse files
committed
Fix up endless method definition with do/end
[Bug #21714]
1 parent 6701ffe commit 0f47e18

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ errors:
6464
- DEF_ENDLESS
6565
- DEF_ENDLESS_PARAMETERS
6666
- DEF_ENDLESS_SETTER
67+
- DEF_ENDLESS_DO_BLOCK
6768
- DEF_NAME
6869
- DEF_PARAMS_TERM
6970
- DEF_PARAMS_TERM_PAREN

src/prism.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18998,6 +18998,20 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1899818998

1899918999
pm_node_t *statement = parse_expression(parser, PM_BINDING_POWER_DEFINED + 1, allow_command_call, false, PM_ERR_DEF_ENDLESS, (uint16_t) (depth + 1));
1900019000

19001+
// In an endless method definition, the body is not allowed to
19002+
// be a command with a do..end block.
19003+
if (PM_NODE_TYPE_P(statement, PM_CALL_NODE)) {
19004+
pm_call_node_t *call = (pm_call_node_t *) statement;
19005+
19006+
if (call->arguments != NULL && call->block != NULL && PM_NODE_TYPE_P(call->block, PM_BLOCK_NODE)) {
19007+
pm_block_node_t *block = (pm_block_node_t *) call->block;
19008+
19009+
if (parser->start[block->opening_loc.start] != '{') {
19010+
pm_parser_err_node(parser, call->block, PM_ERR_DEF_ENDLESS_DO_BLOCK);
19011+
}
19012+
}
19013+
}
19014+
1900119015
if (accept1(parser, PM_TOKEN_KEYWORD_RESCUE_MODIFIER)) {
1900219016
context_push(parser, PM_CONTEXT_RESCUE_MODIFIER);
1900319017

templates/src/diagnostic.c.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
148148
[PM_ERR_DEF_ENDLESS] = { "could not parse the endless method body", PM_ERROR_LEVEL_SYNTAX },
149149
[PM_ERR_DEF_ENDLESS_PARAMETERS] = { "could not parse the endless method parameters", PM_ERROR_LEVEL_SYNTAX },
150150
[PM_ERR_DEF_ENDLESS_SETTER] = { "invalid method name; a setter method cannot be defined in an endless method definition", PM_ERROR_LEVEL_SYNTAX },
151+
[PM_ERR_DEF_ENDLESS_DO_BLOCK] = { "unexpected `do` for block in an endless method definition", PM_ERROR_LEVEL_SYNTAX },
151152
[PM_ERR_DEF_NAME] = { "unexpected %s; expected a method name", PM_ERROR_LEVEL_SYNTAX },
152153
[PM_ERR_DEF_PARAMS_TERM] = { "expected a delimiter to close the parameters", PM_ERROR_LEVEL_SYNTAX },
153154
[PM_ERR_DEF_PARAMS_TERM_PAREN] = { "unexpected %s; expected a `)` to close the parameters", PM_ERROR_LEVEL_SYNTAX },
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def a = a b do 1 end
2+
^~~~~~~~ unexpected `do` for block in an endless method definition
3+

0 commit comments

Comments
 (0)