mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Switch reserved for numbered parameter warning to SyntaxError
This commit is contained in:
parent
50aac2ffd5
commit
d47e124857
Notes:
git
2020-07-23 00:35:29 +09:00
3 changed files with 37 additions and 19 deletions
4
parse.y
4
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue