Skip to content
Merged
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
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ errors:
- CONDITIONAL_WHILE_PREDICATE
- CONSTANT_PATH_COLON_COLON_CONSTANT
- DEF_ENDLESS
- DEF_ENDLESS_PARAMETERS
- DEF_ENDLESS_SETTER
- DEF_NAME
- DEF_PARAMS_TERM
Expand Down
32 changes: 32 additions & 0 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -14612,6 +14612,18 @@ update_parameter_state(pm_parser_t *parser, pm_token_t *token, pm_parameters_ord
return true;
}

/**
* Ensures that after parsing a parameter, the next token is not `=`.
* Some parameters like `def(* = 1)` cannot become optional. When no parens
* are present like in `def * = 1`, this creates ambiguity with endless method definitions.
*/
static inline void
refute_optional_parameter(pm_parser_t *parser) {
if (match1(parser, PM_TOKEN_EQUAL)) {
pm_parser_err_previous(parser, PM_ERR_DEF_ENDLESS_PARAMETERS);
}
}

/**
* Parse a list of parameters on a method definition.
*/
Expand Down Expand Up @@ -14664,6 +14676,10 @@ parse_parameters(
parser->current_scope->parameters |= PM_SCOPE_PARAMETERS_FORWARDING_BLOCK;
}

if (!uses_parentheses) {
refute_optional_parameter(parser);
}

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);
Expand All @@ -14685,6 +14701,10 @@ parse_parameters(
bool succeeded = update_parameter_state(parser, &parser->current, &order);
parser_lex(parser);

if (!uses_parentheses) {
refute_optional_parameter(parser);
}

parser->current_scope->parameters |= PM_SCOPE_PARAMETERS_FORWARDING_ALL;
pm_forwarding_parameter_node_t *param = pm_forwarding_parameter_node_create(parser, &parser->previous);

Expand Down Expand Up @@ -14866,6 +14886,10 @@ parse_parameters(
context_pop(parser);
pm_parameters_node_keywords_append(params, param);

if (!uses_parentheses) {
refute_optional_parameter(parser);
}

// If parsing the value of the parameter resulted in error recovery,
// then we can put a missing node in its place and stop parsing the
// parameters entirely now.
Expand Down Expand Up @@ -14897,6 +14921,10 @@ parse_parameters(
parser->current_scope->parameters |= PM_SCOPE_PARAMETERS_FORWARDING_POSITIONALS;
}

if (!uses_parentheses) {
refute_optional_parameter(parser);
}

pm_node_t *param = (pm_node_t *) pm_rest_parameter_node_create(parser, &operator, &name);
if (repeated) {
pm_node_flag_set_repeated_parameter(param);
Expand Down Expand Up @@ -14945,6 +14973,10 @@ parse_parameters(
}
}

if (!uses_parentheses) {
refute_optional_parameter(parser);
}

if (params->keyword_rest == NULL) {
pm_parameters_node_keyword_rest_set(params, param);
} else {
Expand Down
1 change: 1 addition & 0 deletions templates/src/diagnostic.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
[PM_ERR_CONDITIONAL_WHILE_PREDICATE] = { "expected a predicate expression for the `while` statement", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_CONSTANT_PATH_COLON_COLON_CONSTANT] = { "expected a constant after the `::` operator", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_DEF_ENDLESS] = { "could not parse the endless method body", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_DEF_ENDLESS_PARAMETERS] = { "could not parse the endless method parameters", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_DEF_ENDLESS_SETTER] = { "invalid method name; a setter method cannot be defined in an endless method definition", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_DEF_NAME] = { "unexpected %s; expected a method name", PM_ERROR_LEVEL_SYNTAX },
[PM_ERR_DEF_PARAMS_TERM] = { "expected a delimiter to close the parameters", PM_ERROR_LEVEL_SYNTAX },
Expand Down
6 changes: 6 additions & 0 deletions test/prism/errors/def_with_optional_splat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def foo(*bar = nil); end
^ unexpected '='; expected a `)` to close the parameters
^ unexpected ')', expecting end-of-input
^ unexpected ')', ignoring it
^~~ unexpected 'end', ignoring it

24 changes: 24 additions & 0 deletions test/prism/errors/endless_method_command_call_parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def f x: = 1
^~ could not parse the endless method parameters

def f ... = 1
^~~ could not parse the endless method parameters

def f * = 1
^ could not parse the endless method parameters

def f ** = 1
^~ could not parse the endless method parameters

def f & = 1
^ could not parse the endless method parameters

def f *a = 1
^ could not parse the endless method parameters

def f **a = 1
^ could not parse the endless method parameters

def f &a = 1
^ could not parse the endless method parameters