Skip to content

Commit

Permalink
Merge pull request #3197 from ruby/def-ivar
Browse files Browse the repository at this point in the history
Error for def ivar
  • Loading branch information
kddnewton authored Nov 3, 2024
2 parents ff26b40 + 232a02a commit 14a5e93
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -19213,6 +19213,10 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
context_push(parser, PM_CONTEXT_DEF_PARAMS);
parser_lex(parser);

// This will be false if the method name is not a valid identifier
// but could be followed by an operator.
bool valid_name = true;

switch (parser->current.type) {
case PM_CASE_OPERATOR:
pm_parser_scope_push(parser, true);
Expand Down Expand Up @@ -19242,10 +19246,12 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b

break;
}
case PM_TOKEN_CONSTANT:
case PM_TOKEN_INSTANCE_VARIABLE:
case PM_TOKEN_CLASS_VARIABLE:
case PM_TOKEN_GLOBAL_VARIABLE:
valid_name = false;
/* fallthrough */
case PM_TOKEN_CONSTANT:
case PM_TOKEN_KEYWORD_NIL:
case PM_TOKEN_KEYWORD_SELF:
case PM_TOKEN_KEYWORD_TRUE:
Expand Down Expand Up @@ -19303,6 +19309,10 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b

name = parse_method_definition_name(parser);
} else {
if (!valid_name) {
PM_PARSER_ERR_TOKEN_FORMAT(parser, identifier, PM_ERR_DEF_NAME, pm_token_type_human(identifier.type));
}

name = identifier;
}
break;
Expand Down
3 changes: 3 additions & 0 deletions test/prism/errors/def_ivar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def @foo; end
^~~~ unexpected instance variable; expected a method name

0 comments on commit 14a5e93

Please sign in to comment.