Changed numbered parameters semantics
* `_1` (and no other numbered parameters) to work as `|x|`. * giving up `_0`. [ruby-core:95074] [Bug #16178]
This commit is contained in:
parent
e663299a5f
commit
55e1e22b2d
34
parse.y
34
parse.y
@ -168,7 +168,6 @@ struct local_vars {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
IMPLICIT_PARAM = -2,
|
|
||||||
ORDINAL_PARAM = -1,
|
ORDINAL_PARAM = -1,
|
||||||
NO_PARAM = 0,
|
NO_PARAM = 0,
|
||||||
NUMPARAM_MAX = 9,
|
NUMPARAM_MAX = 9,
|
||||||
@ -8494,38 +8493,20 @@ parser_numbered_param(struct parser_params *p, int n)
|
|||||||
if (n < 0) return false;
|
if (n < 0) return false;
|
||||||
|
|
||||||
if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
|
if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
|
||||||
compile_error(p, "implicit parameter outside block");
|
compile_error(p, "numbered parameter outside block");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (p->max_numparam < NO_PARAM) {
|
|
||||||
if (p->max_numparam == ORDINAL_PARAM) {
|
if (p->max_numparam == ORDINAL_PARAM) {
|
||||||
compile_error(p, "ordinary parameter is defined");
|
compile_error(p, "ordinary parameter is defined");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if (n > 0) {
|
|
||||||
compile_error(p, "implicit parameter is used");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (p->max_numparam > NO_PARAM) {
|
|
||||||
if (n == 0) {
|
|
||||||
compile_error(p, "numbered parameter is used");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
struct vtable *args = p->lvtbl->args;
|
struct vtable *args = p->lvtbl->args;
|
||||||
if (n == 0) {
|
|
||||||
p->max_numparam = IMPLICIT_PARAM;
|
|
||||||
vtable_add(args, idNUMPARAM_0);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (p->max_numparam < n) {
|
if (p->max_numparam < n) {
|
||||||
p->max_numparam = n;
|
p->max_numparam = n;
|
||||||
}
|
}
|
||||||
while (n > args->pos) {
|
while (n > args->pos) {
|
||||||
vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
|
vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -9825,9 +9806,8 @@ numparam_nested_p(struct parser_params *p)
|
|||||||
NODE *inner = p->numparam.inner;
|
NODE *inner = p->numparam.inner;
|
||||||
if (outer || inner) {
|
if (outer || inner) {
|
||||||
NODE *used = outer ? outer : inner;
|
NODE *used = outer ? outer : inner;
|
||||||
compile_error(p, "%s parameter is already used in\n"
|
compile_error(p, "numbered parameter is already used in\n"
|
||||||
"%s:%d: %s block here",
|
"%s:%d: %s block here",
|
||||||
used->nd_vid == idNUMPARAM_0 ? "implicit" : "numbered",
|
|
||||||
p->ruby_sourcefile, nd_line(used),
|
p->ruby_sourcefile, nd_line(used),
|
||||||
outer ? "outer" : "inner");
|
outer ? "outer" : "inner");
|
||||||
parser_show_error_line(p, &used->nd_loc);
|
parser_show_error_line(p, &used->nd_loc);
|
||||||
@ -11277,15 +11257,9 @@ new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block,
|
|||||||
static NODE *
|
static NODE *
|
||||||
args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
|
args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
|
||||||
{
|
{
|
||||||
if (max_numparam > NO_PARAM || max_numparam == IMPLICIT_PARAM) {
|
if (max_numparam > NO_PARAM) {
|
||||||
if (!args) args = new_args_tail(p, 0, 0, 0, 0);
|
if (!args) args = new_args_tail(p, 0, 0, 0, 0);
|
||||||
if (max_numparam == IMPLICIT_PARAM) {
|
|
||||||
args->nd_ainfo->pre_args_num = 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
args->nd_ainfo->pre_args_num = max_numparam;
|
args->nd_ainfo->pre_args_num = max_numparam;
|
||||||
args->nd_ainfo->rest_arg = NODE_SPECIAL_EXCESSIVE_COMMA;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
@ -11827,7 +11801,7 @@ numparam_pop(struct parser_params *p, NODE *prev_inner)
|
|||||||
/* current and inner are exclusive */
|
/* current and inner are exclusive */
|
||||||
p->numparam.inner = p->numparam.current;
|
p->numparam.inner = p->numparam.current;
|
||||||
}
|
}
|
||||||
if (p->max_numparam > NO_PARAM || p->max_numparam == IMPLICIT_PARAM) {
|
if (p->max_numparam > NO_PARAM) {
|
||||||
/* current and outer are exclusive */
|
/* current and outer are exclusive */
|
||||||
p->numparam.current = p->numparam.outer;
|
p->numparam.current = p->numparam.outer;
|
||||||
p->numparam.outer = 0;
|
p->numparam.outer = 0;
|
||||||
|
@ -1442,33 +1442,23 @@ eom
|
|||||||
assert_valid_syntax('proc {_1}')
|
assert_valid_syntax('proc {_1}')
|
||||||
assert_equal(3, eval('[1,2].then {_1+_2}'))
|
assert_equal(3, eval('[1,2].then {_1+_2}'))
|
||||||
assert_equal("12", eval('[1,2].then {"#{_1}#{_2}"}'))
|
assert_equal("12", eval('[1,2].then {"#{_1}#{_2}"}'))
|
||||||
assert_equal([1, 2], eval('[1,2].then {_0}'))
|
assert_equal([1, 2], eval('[1,2].then {_1}'))
|
||||||
assert_equal(3, eval('->{_1+_2}.call(1,2)'))
|
assert_equal(3, eval('->{_1+_2}.call(1,2)'))
|
||||||
assert_equal(4, eval('->(a=->{_1}){a}.call.call(4)'))
|
assert_equal(4, eval('->(a=->{_1}){a}.call.call(4)'))
|
||||||
assert_equal(5, eval('-> a: ->{_1} {a}.call.call(5)'))
|
assert_equal(5, eval('-> a: ->{_1} {a}.call.call(5)'))
|
||||||
assert_syntax_error('proc {|| _0}', /ordinary parameter is defined/)
|
|
||||||
assert_syntax_error('proc {|| _1}', /ordinary parameter is defined/)
|
assert_syntax_error('proc {|| _1}', /ordinary parameter is defined/)
|
||||||
assert_syntax_error('proc {|;a| _1}', /ordinary parameter is defined/)
|
assert_syntax_error('proc {|;a| _1}', /ordinary parameter is defined/)
|
||||||
assert_syntax_error("proc {|\n| _1}", /ordinary parameter is defined/)
|
assert_syntax_error("proc {|\n| _1}", /ordinary parameter is defined/)
|
||||||
assert_syntax_error('proc {|x| _1}', /ordinary parameter is defined/)
|
assert_syntax_error('proc {|x| _1}', /ordinary parameter is defined/)
|
||||||
assert_syntax_error('proc {_0+_1}', /implicit parameter is used/)
|
|
||||||
assert_syntax_error('proc {_1+_0}', /numbered parameter is used/)
|
|
||||||
assert_syntax_error('proc {_1; proc {_2}}', /numbered parameter is already used/)
|
assert_syntax_error('proc {_1; proc {_2}}', /numbered parameter is already used/)
|
||||||
assert_syntax_error('proc {proc {_1}; _2}', /numbered parameter is already used/)
|
assert_syntax_error('proc {proc {_1}; _2}', /numbered parameter is already used/)
|
||||||
assert_syntax_error('proc {_0; proc {_1}}', /implicit parameter is already used/)
|
|
||||||
assert_syntax_error('proc {proc {_0}; _1}', /implicit parameter is already used/)
|
|
||||||
assert_syntax_error('->(){_0}', /ordinary parameter is defined/)
|
|
||||||
assert_syntax_error('->(){_1}', /ordinary parameter is defined/)
|
assert_syntax_error('->(){_1}', /ordinary parameter is defined/)
|
||||||
assert_syntax_error('->(x){_1}', /ordinary parameter is defined/)
|
assert_syntax_error('->(x){_1}', /ordinary parameter is defined/)
|
||||||
assert_syntax_error('->x{_1}', /ordinary parameter is defined/)
|
assert_syntax_error('->x{_1}', /ordinary parameter is defined/)
|
||||||
assert_syntax_error('->x:_2{}', /ordinary parameter is defined/)
|
assert_syntax_error('->x:_2{}', /ordinary parameter is defined/)
|
||||||
assert_syntax_error('->x=_1{}', /ordinary parameter is defined/)
|
assert_syntax_error('->x=_1{}', /ordinary parameter is defined/)
|
||||||
assert_syntax_error('-> {_0+_1}', /implicit parameter is used/)
|
|
||||||
assert_syntax_error('-> {_1+_0}', /numbered parameter is used/)
|
|
||||||
assert_syntax_error('-> {_1; -> {_2}}', /numbered parameter is already used/)
|
assert_syntax_error('-> {_1; -> {_2}}', /numbered parameter is already used/)
|
||||||
assert_syntax_error('-> {-> {_1}; _2}', /numbered parameter is already used/)
|
assert_syntax_error('-> {-> {_1}; _2}', /numbered parameter is already used/)
|
||||||
assert_syntax_error('-> {_0; -> {_1}}', /implicit parameter is already used/)
|
|
||||||
assert_syntax_error('-> {-> {_0}; _1}', /implicit parameter is already used/)
|
|
||||||
assert_warn(/`_1' is used as numbered parameter/) {eval('proc {_1 = nil}')}
|
assert_warn(/`_1' is used as numbered parameter/) {eval('proc {_1 = nil}')}
|
||||||
assert_warn(/`_2' is used as numbered parameter/) {eval('_2=1')}
|
assert_warn(/`_2' is used as numbered parameter/) {eval('_2=1')}
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user