ruby-changes:47510
From: nobu <ko1@a...>
Date: Sun, 20 Aug 2017 15:08:30 +0900 (JST)
Subject: [ruby-changes:47510] nobu:r59626 (trunk): non-keywords hash
nobu 2017-08-20 15:08:25 +0900 (Sun, 20 Aug 2017) New Revision: 59626 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59626 Log: non-keywords hash * class.c (rb_scan_args), include/ruby/ruby.h (rb_scan_args_set): return non-keywords elements only in the last hash when keyword arguments are extracted from it, as well as methods defined in ruby level. [ruby-core:82427] [Bug #13830] Modified files: trunk/class.c trunk/include/ruby/ruby.h trunk/test/-ext-/test_scan_args.rb Index: test/-ext-/test_scan_args.rb =================================================================== --- test/-ext-/test_scan_args.rb (revision 59625) +++ test/-ext-/test_scan_args.rb (revision 59626) @@ -102,6 +102,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L102 assert_equal([0, nil, {b: 1}], Bug::ScanArgs.opt_hash(b: 1)) assert_equal([1, "a", {b: 1}], Bug::ScanArgs.opt_hash("a", b: 1)) assert_raise(ArgumentError) {Bug::ScanArgs.opt_hash("a", "b")} + assert_equal([1, {"a"=>0}, {b: 1}], Bug::ScanArgs.opt_hash("a"=>0, b: 1)) end def test_lead_opt_hash @@ -111,6 +112,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L112 assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.lead_opt_hash("a", "b", c: 1)) assert_equal([1, {c: 1}, nil, nil], Bug::ScanArgs.lead_opt_hash(c: 1)) assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_hash("a", "b", "c")} + assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_hash("a", "b"=>0, c: 1)) end def test_var_hash @@ -118,6 +120,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L120 assert_equal([1, ["a"], nil], Bug::ScanArgs.var_hash("a")) assert_equal([1, ["a"], {b: 1}], Bug::ScanArgs.var_hash("a", b: 1)) assert_equal([0, [], {b: 1}], Bug::ScanArgs.var_hash(b: 1)) + assert_equal([1, [{"a"=>0}], {b: 1}], Bug::ScanArgs.var_hash("a"=>0, b: 1)) end def test_lead_var_hash @@ -128,6 +131,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L131 assert_equal([1, "a", [], {c: 1}], Bug::ScanArgs.lead_var_hash("a", c: 1)) assert_equal([1, {c: 1}, [], nil], Bug::ScanArgs.lead_var_hash(c: 1)) assert_equal([3, "a", ["b", "c"], nil], Bug::ScanArgs.lead_var_hash("a", "b", "c")) + assert_equal([2, "a", [{"b"=>0}], {c: 1}], Bug::ScanArgs.lead_var_hash("a", "b"=>0, c: 1)) end def test_opt_var_hash @@ -138,6 +142,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L142 assert_equal([1, "a", [], {c: 1}], Bug::ScanArgs.opt_var_hash("a", c: 1)) assert_equal([0, nil, [], {c: 1}], Bug::ScanArgs.opt_var_hash(c: 1)) assert_equal([3, "a", ["b", "c"], nil], Bug::ScanArgs.opt_var_hash("a", "b", "c")) + assert_equal([2, "a", [{"b"=>0}], {c: 1}], Bug::ScanArgs.opt_var_hash("a", "b"=>0, c: 1)) end def test_lead_opt_var_hash @@ -149,6 +154,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L154 assert_equal([1, {c: 1}, nil, [], nil], Bug::ScanArgs.lead_opt_var_hash(c: 1)) assert_equal([3, "a", "b", ["c"], nil], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c")) assert_equal([3, "a", "b", ["c"], {d: 1}], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c", d: 1)) + assert_equal([3, "a", "b", [{"c"=>0}], {d: 1}], Bug::ScanArgs.lead_opt_var_hash("a", "b", "c"=>0, d: 1)) end def test_opt_trail_hash @@ -159,6 +165,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L165 assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.opt_trail_hash("a", "b", c: 1)) assert_equal([1, nil, {c: 1}, nil], Bug::ScanArgs.opt_trail_hash(c: 1)) assert_raise(ArgumentError) {Bug::ScanArgs.opt_trail_hash("a", "b", "c")} + assert_equal([2, "a", {"b"=>0}, {c: 1}], Bug::ScanArgs.opt_trail_hash("a", "b"=>0, c: 1)) end def test_lead_opt_trail_hash @@ -171,6 +178,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L178 assert_equal([3, "a", "b", "c", nil], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c")) assert_equal([3, "a", "b", "c", {c: 1}], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c", c: 1)) assert_raise(ArgumentError) {Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c", "d")} + assert_equal([3, "a", "b", {"c"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c"=>0, c: 1)) end def test_var_trail_hash @@ -182,6 +190,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L190 assert_equal([1, [], {c: 1}, nil], Bug::ScanArgs.var_trail_hash(c: 1)) assert_equal([3, ["a", "b"], "c", nil], Bug::ScanArgs.var_trail_hash("a", "b", "c")) assert_equal([3, ["a", "b"], "c", {c: 1}], Bug::ScanArgs.var_trail_hash("a", "b", "c", c: 1)) + assert_equal([3, ["a", "b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.var_trail_hash("a", "b", "c"=>0, c: 1)) end def test_lead_var_trail_hash @@ -193,6 +202,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L202 assert_equal([2, "a", [], "b", {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", c: 1)) assert_equal([3, "a", ["b"], "c", nil], Bug::ScanArgs.lead_var_trail_hash("a", "b", "c")) assert_equal([3, "a", ["b"], "c", {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", "c", c: 1)) + assert_equal([3, "a", ["b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", c: 1, "c"=>0)) end def test_opt_var_trail_hash @@ -204,6 +214,7 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L214 assert_equal([2, "a", [], "b", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", c: 1)) assert_equal([3, "a", ["b"], "c", nil], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c")) assert_equal([3, "a", ["b"], "c", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c", c: 1)) + assert_equal([3, "a", ["b"], {"c"=>0}, {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", "c"=>0, c: 1)) end def test_lead_opt_var_trail_hash @@ -215,5 +226,6 @@ class TestScanArgs < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_scan_args.rb#L226 assert_equal([3, "a", "b", [], "c", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c")) assert_equal([3, "a", "b", [], "c", {c: 1}], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", c: 1)) assert_equal([4, "a", "b", ["c"], "d", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", "d")) + assert_equal([4, "a", "b", ["c"], {"d"=>0}, {c: 1}], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c", "d"=>0, c: 1)) end end Index: class.c =================================================================== --- class.c (revision 59625) +++ class.c (revision 59626) @@ -1913,8 +1913,8 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/class.c#L1913 va_list vargs; int f_var = 0, f_hash = 0, f_block = 0; int n_lead = 0, n_opt = 0, n_trail = 0, n_mand; - int argi = 0; - VALUE hash = Qnil; + int argi = 0, last_idx = -1; + VALUE hash = Qnil, last_hash = 0; if (ISDIGIT(*p)) { n_lead = *p - '0'; @@ -1965,7 +1965,8 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/class.c#L1965 hash = rb_check_hash_type(last); if (!NIL_P(hash)) { VALUE opts = rb_extract_keywords(&hash); - if (!hash) argc--; + if (!(last_hash = hash)) argc--; + else last_idx = argc - 1; hash = opts ? opts : Qnil; } } @@ -1973,14 +1974,14 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/class.c#L1974 /* capture leading mandatory arguments */ for (i = n_lead; i-- > 0; ) { var = va_arg(vargs, VALUE *); - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } /* capture optional arguments */ for (i = n_opt; i-- > 0; ) { var = va_arg(vargs, VALUE *); if (argi < argc - n_trail) { - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } else { @@ -1993,7 +1994,11 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/class.c#L1994 var = va_arg(vargs, VALUE *); if (0 < n_var) { - if (var) *var = rb_ary_new4(n_var, &argv[argi]); + if (var) { + int f_last = (last_idx + 1 == argc - n_trail); + *var = rb_ary_new4(n_var-f_last, &argv[argi]); + if (f_last) rb_ary_push(*var, last_hash); + } argi += n_var; } else { @@ -2003,7 +2008,7 @@ rb_scan_args(int argc, const VALUE *argv https://github.com/ruby/ruby/blob/trunk/class.c#L2008 /* capture trailing mandatory arguments */ for (i = n_trail; i-- > 0; ) { var = va_arg(vargs, VALUE *); - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } /* capture an option hash - phase 2: assignment */ Index: include/ruby/ruby.h =================================================================== --- include/ruby/ruby.h (revision 59625) +++ include/ruby/ruby.h (revision 59626) @@ -2351,8 +2351,8 @@ rb_scan_args_set(int argc, const VALUE * https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L2351 int f_var, int f_hash, int f_block, VALUE *vars[]) { - int i, argi = 0, vari = 0; - VALUE *var, hash = Qnil; + int i, argi = 0, vari = 0, last_idx = -1; + VALUE *var, hash = Qnil, last_hash = 0; const int n_mand = n_lead + n_trail; /* capture an option hash - phase 1: pop */ @@ -2370,7 +2370,8 @@ rb_scan_args_set(int argc, const VALUE * https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L2370 hash = rb_check_hash_type(last); if (!RB_NIL_P(hash)) { VALUE opts = rb_extract_keywords(&hash); - if (!hash) argc--; + if (!(last_hash = hash)) argc--; + else last_idx = argc - 1; hash = opts ? opts : Qnil; } } @@ -2381,14 +2382,14 @@ rb_scan_args_set(int argc, const VALUE * https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L2382 /* capture leading mandatory arguments */ for (i = n_lead; i-- > 0; ) { var = vars[vari++]; - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } /* capture optional arguments */ for (i = n_opt; i-- > 0; ) { var = vars[vari++]; if (argi < argc - n_trail) { - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } else { @@ -2401,7 +2402,11 @@ rb_scan_args_set(int argc, const VALUE * https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L2402 var = vars[vari++]; if (0 < n_var) { - if (var) *var = rb_ary_new4(n_var, &argv[argi]); + if (var) { + int f_last = (last_idx + 1 == argc - n_trail); + *var = rb_ary_new4(n_var-f_last, &argv[argi]); + if (f_last) rb_ary_push(*var, last_hash); + } argi += n_var; } else { @@ -2411,7 +2416,7 @@ rb_scan_args_set(int argc, const VALUE * https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L2416 /* capture trailing mandatory arguments */ for (i = n_trail; i-- > 0; ) { var = vars[vari++]; - if (var) *var = argv[argi]; + if (var) *var = (argi == last_idx) ? last_hash : argv[argi]; argi++; } /* capture an option hash - phase 2: assignment */ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/