[前][次][番号順一覧][スレッド一覧]

ruby-changes:57893

From: Nobuyoshi <ko1@a...>
Date: Wed, 25 Sep 2019 13:02:46 +0900 (JST)
Subject: [ruby-changes:57893] 55e1e22b2d (master): Changed numbered parameters semantics

https://git.ruby-lang.org/ruby.git/commit/?id=55e1e22b2d

From 55e1e22b2d44a8a1eca0f6ed2b11dc0f564f7192 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 25 Sep 2019 13:01:03 +0900
Subject: Changed numbered parameters semantics

* `_1` (and no other numbered parameters) to work as `|x|`.
* giving up `_0`.

[ruby-core:95074] [Bug #16178]

diff --git a/parse.y b/parse.y
index 8686bd4..48dd44a 100644
--- a/parse.y
+++ b/parse.y
@@ -168,7 +168,6 @@ struct local_vars { https://github.com/ruby/ruby/blob/trunk/parse.y#L168
 };
 
 enum {
-    IMPLICIT_PARAM = -2,
     ORDINAL_PARAM = -1,
     NO_PARAM = 0,
     NUMPARAM_MAX = 9,
@@ -8494,37 +8493,19 @@ parser_numbered_param(struct parser_params *p, int n) https://github.com/ruby/ruby/blob/trunk/parse.y#L8493
     if (n < 0) return false;
 
     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;
     }
-    if (p->max_numparam < NO_PARAM) {
-	if (p->max_numparam == ORDINAL_PARAM) {
-	    compile_error(p, "ordinary parameter is defined");
-	    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;
-	}
+    if (p->max_numparam == ORDINAL_PARAM) {
+	compile_error(p, "ordinary parameter is defined");
+	return false;
     }
     struct vtable *args = p->lvtbl->args;
-    if (n == 0) {
-	p->max_numparam = IMPLICIT_PARAM;
-	vtable_add(args, idNUMPARAM_0);
+    if (p->max_numparam < n) {
+	p->max_numparam = n;
     }
-    else {
-	if (p->max_numparam < n) {
-	    p->max_numparam = n;
-	}
-	while (n > args->pos) {
-	    vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
-	}
+    while (n > args->pos) {
+	vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
     }
     return true;
 }
@@ -9825,9 +9806,8 @@ numparam_nested_p(struct parser_params *p) https://github.com/ruby/ruby/blob/trunk/parse.y#L9806
     NODE *inner = p->numparam.inner;
     if (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",
-		      used->nd_vid == idNUMPARAM_0 ? "implicit" : "numbered",
 		      p->ruby_sourcefile, nd_line(used),
 		      outer ? "outer" : "inner");
 	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, https://github.com/ruby/ruby/blob/trunk/parse.y#L11257
 static NODE *
 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 (max_numparam == IMPLICIT_PARAM) {
-	    args->nd_ainfo->pre_args_num = 1;
-	}
-	else {
-	    args->nd_ainfo->pre_args_num = max_numparam;
-	    args->nd_ainfo->rest_arg = NODE_SPECIAL_EXCESSIVE_COMMA;
-	}
+	args->nd_ainfo->pre_args_num = max_numparam;
     }
     return args;
 }
@@ -11827,7 +11801,7 @@ numparam_pop(struct parser_params *p, NODE *prev_inner) https://github.com/ruby/ruby/blob/trunk/parse.y#L11801
 	/* current and inner are exclusive */
 	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 */
 	p->numparam.current = p->numparam.outer;
 	p->numparam.outer = 0;
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 61bee81..a64d258 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -1442,33 +1442,23 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L1442
     assert_valid_syntax('proc {_1}')
     assert_equal(3, 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(4, eval('->(a=->{_1}){a}.call.call(4)'))
     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 {|;a| _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 {_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 {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('->(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=_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('-> {_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(/`_2' is used as numbered parameter/) {eval('_2=1')}
   end
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]