Skip to content

Commit

Permalink
Warn also numbered parameter like methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Dec 19, 2019
1 parent 3816cd9 commit 2898367
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions parse.y
Expand Up @@ -188,6 +188,7 @@ numparam_id_p(ID id)
unsigned int idx = NUMPARAM_ID_TO_IDX(id);
return idx > 0 && idx <= NUMPARAM_MAX;
}
static void numparam_name(struct parser_params *p, ID id);

#define DVARS_INHERIT ((void*)1)
#define DVARS_TOPSCOPE NULL
Expand Down Expand Up @@ -2981,6 +2982,7 @@ primary : literal
}
| k_def fname
{
numparam_name(p, get_id($2));
local_push(p, 0);
$<id>$ = p->cur_arg;
p->cur_arg = 0;
Expand All @@ -3007,6 +3009,7 @@ primary : literal
}
| k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname
{
numparam_name(p, get_id($5));
$<num>4 = p->in_def;
p->in_def = 1;
SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
Expand Down
2 changes: 2 additions & 0 deletions test/ruby/test_syntax.rb
Expand Up @@ -1441,6 +1441,8 @@ def test_numbered_parameter
assert_warn(/`_2' is used as numbered parameter/) {eval('_2=1')}
assert_warn(/`_3' is used as numbered parameter/) {eval('proc {|_3|}')}
assert_warn(/`_4' is used as numbered parameter/) {instance_eval('def x(_4) end')}
assert_warn(/`_5' is used as numbered parameter/) {instance_eval('def _5; end')}
assert_warn(/`_6' is used as numbered parameter/) {instance_eval('def self._6; end')}
assert_raise_with_message(NameError, /undefined local variable or method `_1'/) {
eval('_1')
}
Expand Down

0 comments on commit 2898367

Please sign in to comment.