From 2898367b3a1de00ca78067cc17dd4d1f8df37778 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Fri, 20 Dec 2019 08:18:19 +0900 Subject: [PATCH] Warn also numbered parameter like methods --- parse.y | 3 +++ test/ruby/test_syntax.rb | 2 ++ 2 files changed, 5 insertions(+) diff --git a/parse.y b/parse.y index 33c1c89c3d..7247afafa3 100644 --- a/parse.y +++ b/parse.y @@ -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 @@ -2981,6 +2982,7 @@ primary : literal } | k_def fname { + numparam_name(p, get_id($2)); local_push(p, 0); $$ = p->cur_arg; p->cur_arg = 0; @@ -3007,6 +3009,7 @@ primary : literal } | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname { + numparam_name(p, get_id($5)); $4 = p->in_def; p->in_def = 1; SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */ diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index b292b24894..5eb69c5144 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1441,6 +1441,8 @@ eom 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') }