diff --git a/parse.y b/parse.y index e580f7f78e..ab71469a06 100644 --- a/parse.y +++ b/parse.y @@ -12079,8 +12079,8 @@ static void numparam_name(struct parser_params *p, ID id) { if (!NUMPARAM_ID_P(id)) return; - rb_warn1("`_%d' is reserved for numbered parameter; consider another name", - WARN_I(NUMPARAM_ID_TO_IDX(id))); + compile_error(p, "_%d is reserved for numbered parameter", + NUMPARAM_ID_TO_IDX(id)); } static void diff --git a/spec/ruby/language/numbered_parameters_spec.rb b/spec/ruby/language/numbered_parameters_spec.rb index e7b2204d4a..5b0f74b3ab 100644 --- a/spec/ruby/language/numbered_parameters_spec.rb +++ b/spec/ruby/language/numbered_parameters_spec.rb @@ -32,19 +32,38 @@ ruby_version_is "2.7" do }.should raise_error(SyntaxError, /numbered parameter is already used in.+ outer block here/m) end - it "can be overwritten with local variable" do - suppress_warning do - eval <<~CODE - _1 = 0 - proc { _1 }.call("a").should == 0 - CODE + ruby_version_is '2.7'...'2.8' do + it "can be overwritten with local variable" do + suppress_warning do + eval <<~CODE + _1 = 0 + proc { _1 }.call("a").should == 0 + CODE + end + end + + it "warns when numbered parameter is overriten with local variable" do + -> { + eval("_1 = 0") + }.should complain(/warning: `_1' is reserved for numbered parameter; consider another name/) end end - it "warns when numbered parameter is overriten with local variable" do - -> { - eval("_1 = 0") - }.should complain(/warning: `_1' is reserved for numbered parameter; consider another name/) + ruby_version_is '2.8' do + it "cannot be overwritten with local variable" do + -> { + eval <<~CODE + _1 = 0 + proc { _1 }.call("a").should == 0 + CODE + }.should raise_error(SyntaxError, /_1 is reserved for numbered parameter/) + end + + it "errors when numbered parameter is overriten with local variable" do + -> { + eval("_1 = 0") + }.should raise_error(SyntaxError, /_1 is reserved for numbered parameter/) + end end it "raises SyntaxError when block parameters are specified explicitly" do diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 6245c73ec3..546b5a27e1 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -1468,13 +1468,12 @@ eom assert_syntax_error('-> {_1; -> {_2}}', /numbered parameter is already used/) assert_syntax_error('-> {-> {_1}; _2}', /numbered parameter is already used/) assert_syntax_error('proc {_1; _1 = nil}', /Can't assign to numbered parameter _1/) - mesg = proc {|n| /`_#{n}' is reserved for numbered parameter/} - assert_warn(mesg[1]) {eval('proc {_1 = nil}')} - assert_warn(mesg[2]) {eval('_2=1')} - assert_warn(mesg[3]) {eval('proc {|_3|}')} - assert_warn(mesg[4]) {instance_eval('def x(_4) end')} - assert_warn(mesg[5]) {instance_eval('def _5; end')} - assert_warn(mesg[6]) {instance_eval('def self._6; end')} + assert_syntax_error('proc {_1 = nil}', /_1 is reserved for numbered parameter/) + assert_syntax_error('_2=1', /_2 is reserved for numbered parameter/) + assert_syntax_error('proc {|_3|}', /_3 is reserved for numbered parameter/) + assert_syntax_error('def x(_4) end', /_4 is reserved for numbered parameter/) + assert_syntax_error('def _5; end', /_5 is reserved for numbered parameter/) + assert_syntax_error('def self._6; end', /_6 is reserved for numbered parameter/) assert_raise_with_message(NameError, /undefined local variable or method `_1'/) { eval('_1') }