Skip to content

Commit 21af248

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Error for def ivar
ruby/prism@232a02acef
1 parent 3e2ee99 commit 21af248

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

prism/prism.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19212,6 +19212,10 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1921219212
context_push(parser, PM_CONTEXT_DEF_PARAMS);
1921319213
parser_lex(parser);
1921419214

19215+
// This will be false if the method name is not a valid identifier
19216+
// but could be followed by an operator.
19217+
bool valid_name = true;
19218+
1921519219
switch (parser->current.type) {
1921619220
case PM_CASE_OPERATOR:
1921719221
pm_parser_scope_push(parser, true);
@@ -19241,10 +19245,12 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1924119245

1924219246
break;
1924319247
}
19244-
case PM_TOKEN_CONSTANT:
1924519248
case PM_TOKEN_INSTANCE_VARIABLE:
1924619249
case PM_TOKEN_CLASS_VARIABLE:
1924719250
case PM_TOKEN_GLOBAL_VARIABLE:
19251+
valid_name = false;
19252+
/* fallthrough */
19253+
case PM_TOKEN_CONSTANT:
1924819254
case PM_TOKEN_KEYWORD_NIL:
1924919255
case PM_TOKEN_KEYWORD_SELF:
1925019256
case PM_TOKEN_KEYWORD_TRUE:
@@ -19302,6 +19308,10 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1930219308

1930319309
name = parse_method_definition_name(parser);
1930419310
} else {
19311+
if (!valid_name) {
19312+
PM_PARSER_ERR_TOKEN_FORMAT(parser, identifier, PM_ERR_DEF_NAME, pm_token_type_human(identifier.type));
19313+
}
19314+
1930519315
name = identifier;
1930619316
}
1930719317
break;

test/prism/errors/def_ivar.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def @foo; end
2+
^~~~ unexpected instance variable; expected a method name
3+

0 commit comments

Comments
 (0)