* 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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59626 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-08-20 06:08:25 +00:00
Родитель 9191d9e7db
Коммит 17b3441ac4
3 изменённых файлов: 36 добавлений и 14 удалений

19
class.c
Просмотреть файл

@ -1913,8 +1913,8 @@ rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
va_list vargs; va_list vargs;
int f_var = 0, f_hash = 0, f_block = 0; int f_var = 0, f_hash = 0, f_block = 0;
int n_lead = 0, n_opt = 0, n_trail = 0, n_mand; int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
int argi = 0; int argi = 0, last_idx = -1;
VALUE hash = Qnil; VALUE hash = Qnil, last_hash = 0;
if (ISDIGIT(*p)) { if (ISDIGIT(*p)) {
n_lead = *p - '0'; n_lead = *p - '0';
@ -1965,7 +1965,8 @@ rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
hash = rb_check_hash_type(last); hash = rb_check_hash_type(last);
if (!NIL_P(hash)) { if (!NIL_P(hash)) {
VALUE opts = rb_extract_keywords(&hash); VALUE opts = rb_extract_keywords(&hash);
if (!hash) argc--; if (!(last_hash = hash)) argc--;
else last_idx = argc - 1;
hash = opts ? opts : Qnil; hash = opts ? opts : Qnil;
} }
} }
@ -1973,14 +1974,14 @@ rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
/* capture leading mandatory arguments */ /* capture leading mandatory arguments */
for (i = n_lead; i-- > 0; ) { for (i = n_lead; i-- > 0; ) {
var = va_arg(vargs, VALUE *); var = va_arg(vargs, VALUE *);
if (var) *var = argv[argi]; if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
argi++; argi++;
} }
/* capture optional arguments */ /* capture optional arguments */
for (i = n_opt; i-- > 0; ) { for (i = n_opt; i-- > 0; ) {
var = va_arg(vargs, VALUE *); var = va_arg(vargs, VALUE *);
if (argi < argc - n_trail) { if (argi < argc - n_trail) {
if (var) *var = argv[argi]; if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
argi++; argi++;
} }
else { else {
@ -1993,7 +1994,11 @@ rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
var = va_arg(vargs, VALUE *); var = va_arg(vargs, VALUE *);
if (0 < n_var) { 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; argi += n_var;
} }
else { else {
@ -2003,7 +2008,7 @@ rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
/* capture trailing mandatory arguments */ /* capture trailing mandatory arguments */
for (i = n_trail; i-- > 0; ) { for (i = n_trail; i-- > 0; ) {
var = va_arg(vargs, VALUE *); var = va_arg(vargs, VALUE *);
if (var) *var = argv[argi]; if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
argi++; argi++;
} }
/* capture an option hash - phase 2: assignment */ /* capture an option hash - phase 2: assignment */

Просмотреть файл

@ -2351,8 +2351,8 @@ rb_scan_args_set(int argc, const VALUE *argv,
int f_var, int f_hash, int f_block, int f_var, int f_hash, int f_block,
VALUE *vars[]) VALUE *vars[])
{ {
int i, argi = 0, vari = 0; int i, argi = 0, vari = 0, last_idx = -1;
VALUE *var, hash = Qnil; VALUE *var, hash = Qnil, last_hash = 0;
const int n_mand = n_lead + n_trail; const int n_mand = n_lead + n_trail;
/* capture an option hash - phase 1: pop */ /* capture an option hash - phase 1: pop */
@ -2370,7 +2370,8 @@ rb_scan_args_set(int argc, const VALUE *argv,
hash = rb_check_hash_type(last); hash = rb_check_hash_type(last);
if (!RB_NIL_P(hash)) { if (!RB_NIL_P(hash)) {
VALUE opts = rb_extract_keywords(&hash); VALUE opts = rb_extract_keywords(&hash);
if (!hash) argc--; if (!(last_hash = hash)) argc--;
else last_idx = argc - 1;
hash = opts ? opts : Qnil; hash = opts ? opts : Qnil;
} }
} }
@ -2381,14 +2382,14 @@ rb_scan_args_set(int argc, const VALUE *argv,
/* capture leading mandatory arguments */ /* capture leading mandatory arguments */
for (i = n_lead; i-- > 0; ) { for (i = n_lead; i-- > 0; ) {
var = vars[vari++]; var = vars[vari++];
if (var) *var = argv[argi]; if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
argi++; argi++;
} }
/* capture optional arguments */ /* capture optional arguments */
for (i = n_opt; i-- > 0; ) { for (i = n_opt; i-- > 0; ) {
var = vars[vari++]; var = vars[vari++];
if (argi < argc - n_trail) { if (argi < argc - n_trail) {
if (var) *var = argv[argi]; if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
argi++; argi++;
} }
else { else {
@ -2401,7 +2402,11 @@ rb_scan_args_set(int argc, const VALUE *argv,
var = vars[vari++]; var = vars[vari++];
if (0 < n_var) { 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; argi += n_var;
} }
else { else {
@ -2411,7 +2416,7 @@ rb_scan_args_set(int argc, const VALUE *argv,
/* capture trailing mandatory arguments */ /* capture trailing mandatory arguments */
for (i = n_trail; i-- > 0; ) { for (i = n_trail; i-- > 0; ) {
var = vars[vari++]; var = vars[vari++];
if (var) *var = argv[argi]; if (var) *var = (argi == last_idx) ? last_hash : argv[argi];
argi++; argi++;
} }
/* capture an option hash - phase 2: assignment */ /* capture an option hash - phase 2: assignment */

Просмотреть файл

@ -102,6 +102,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([0, nil, {b: 1}], Bug::ScanArgs.opt_hash(b: 1)) 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_equal([1, "a", {b: 1}], Bug::ScanArgs.opt_hash("a", b: 1))
assert_raise(ArgumentError) {Bug::ScanArgs.opt_hash("a", "b")} 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 end
def test_lead_opt_hash def test_lead_opt_hash
@ -111,6 +112,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.lead_opt_hash("a", "b", c: 1)) 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_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_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 end
def test_var_hash def test_var_hash
@ -118,6 +120,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([1, ["a"], nil], Bug::ScanArgs.var_hash("a")) 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([1, ["a"], {b: 1}], Bug::ScanArgs.var_hash("a", b: 1))
assert_equal([0, [], {b: 1}], Bug::ScanArgs.var_hash(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 end
def test_lead_var_hash def test_lead_var_hash
@ -128,6 +131,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([1, "a", [], {c: 1}], Bug::ScanArgs.lead_var_hash("a", c: 1)) 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([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([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 end
def test_opt_var_hash def test_opt_var_hash
@ -138,6 +142,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([1, "a", [], {c: 1}], Bug::ScanArgs.opt_var_hash("a", c: 1)) 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([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([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 end
def test_lead_opt_var_hash def test_lead_opt_var_hash
@ -149,6 +154,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([1, {c: 1}, nil, [], nil], Bug::ScanArgs.lead_opt_var_hash(c: 1)) 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"], 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"], {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 end
def test_opt_trail_hash def test_opt_trail_hash
@ -159,6 +165,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, "a", "b", {c: 1}], Bug::ScanArgs.opt_trail_hash("a", "b", c: 1)) 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_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_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 end
def test_lead_opt_trail_hash def test_lead_opt_trail_hash
@ -171,6 +178,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([3, "a", "b", "c", nil], Bug::ScanArgs.lead_opt_trail_hash("a", "b", "c")) 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_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_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 end
def test_var_trail_hash def test_var_trail_hash
@ -182,6 +190,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([1, [], {c: 1}, nil], Bug::ScanArgs.var_trail_hash(c: 1)) 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", 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", {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 end
def test_lead_var_trail_hash def test_lead_var_trail_hash
@ -193,6 +202,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, "a", [], "b", {c: 1}], Bug::ScanArgs.lead_var_trail_hash("a", "b", c: 1)) 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", 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", {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 end
def test_opt_var_trail_hash def test_opt_var_trail_hash
@ -204,6 +214,7 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([2, "a", [], "b", {c: 1}], Bug::ScanArgs.opt_var_trail_hash("a", "b", c: 1)) 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", 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", {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 end
def test_lead_opt_var_trail_hash def test_lead_opt_var_trail_hash
@ -215,5 +226,6 @@ class TestScanArgs < Test::Unit::TestCase
assert_equal([3, "a", "b", [], "c", nil], Bug::ScanArgs.lead_opt_var_trail_hash("a", "b", "c")) 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([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", 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
end end