Skip to content

Commit

Permalink
Add keyrest to ruby2_keywords parameters [Bug #18011]
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Aug 3, 2021
1 parent 731315b commit 4c3140d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
13 changes: 11 additions & 2 deletions iseq.c
Expand Up @@ -3116,9 +3116,18 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
rb_ary_push(args, a);
}
}
if (body->param.flags.has_kwrest) {
if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
ID param;
CONST_ID(keyrest, "keyrest");
rb_ary_push(args, PARAM(keyword->rest_start, keyrest));
PARAM_TYPE(keyrest);
if (body->param.flags.has_kwrest &&
rb_id2str(param = PARAM_ID(keyword->rest_start))) {
rb_ary_push(a, ID2SYM(param));
}
else if (body->param.flags.ruby2_keywords) {
rb_ary_push(a, ID2SYM(idPow));
}
rb_ary_push(args, a);
}
if (body->param.flags.has_block) {
CONST_ID(block, "block");
Expand Down
7 changes: 6 additions & 1 deletion proc.c
Expand Up @@ -3097,7 +3097,12 @@ method_inspect(VALUE method)
}
}
else if (kind == keyrest) {
rb_str_catf(str, "**%"PRIsVALUE, name);
if (name != ID2SYM(idPow)) {
rb_str_catf(str, "**%"PRIsVALUE, name);
}
else if (i > 0) {
rb_str_set_len(str, RSTRING_LEN(str) - 2);
}
}
else if (kind == block) {
if (name == ID2SYM('&')) {
Expand Down
4 changes: 2 additions & 2 deletions test/ruby/test_method.rb
Expand Up @@ -578,7 +578,7 @@ def test_bound_parameters
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyreq, :e], [:key, :f], [:keyrest, :o]], method(:mk8).parameters)
assert_equal([[:nokey]], method(:mnk).parameters)
# pending
assert_equal([[:rest, :*], [:block, :&]], method(:mf).parameters)
assert_equal([[:rest, :*], [:keyrest, :**], [:block, :&]], method(:mf).parameters)
end

def test_unbound_parameters
Expand All @@ -604,7 +604,7 @@ def test_unbound_parameters
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyreq, :e], [:key, :f], [:keyrest, :o]], self.class.instance_method(:mk8).parameters)
assert_equal([[:nokey]], self.class.instance_method(:mnk).parameters)
# pending
assert_equal([[:rest, :*], [:block, :&]], self.class.instance_method(:mf).parameters)
assert_equal([[:rest, :*], [:keyrest, :**], [:block, :&]], self.class.instance_method(:mf).parameters)
end

def test_bmethod_bound_parameters
Expand Down
3 changes: 2 additions & 1 deletion test/ruby/test_syntax.rb
Expand Up @@ -1638,7 +1638,8 @@ def obj3.bar(*args, &block)
assert_equal(-1, obj.method(:foo).arity)
parameters = obj.method(:foo).parameters
assert_equal(:rest, parameters.dig(0, 0))
assert_equal(:block, parameters.dig(1, 0))
assert_equal(:keyrest, parameters.dig(1, 0))
assert_equal(:block, parameters.dig(2, 0))
end
end

Expand Down

0 comments on commit 4c3140d

Please sign in to comment.