1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Don't pass an empty keyword hash when double splatting empty hash

This commit is contained in:
Jeremy Evans 2019-08-30 23:50:50 -07:00
parent 4868ad7e5b
commit 1f18b578ce
3 changed files with 56 additions and 0 deletions

View file

@ -177,6 +177,55 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_equal(["bar", 111111], f[str: "bar", num: 111111])
end
def test_lambda_kwsplat_call
kw = {}
h = {'a'=>1}
h2 = {'a'=>1}
h3 = {'a'=>1, :a=>1}
f = -> { true }
assert_equal(true, f[**{}])
assert_equal(true, f[**kw])
assert_raise(ArgumentError) { f[**h] }
assert_raise(ArgumentError) { f[**h2] }
assert_raise(ArgumentError) { f[**h3] }
f = ->(a) { a }
assert_raise(ArgumentError) { f[**{}] }
assert_raise(ArgumentError) { f[**kw] }
assert_equal(h, f[**h])
assert_equal(h2, f[**h2])
assert_equal(h3, f[**h3])
f = ->(**x) { x }
assert_equal(kw, f[**{}])
assert_equal(kw, f[**kw])
assert_equal(h, f[**h])
assert_equal(h2, f[**h2])
assert_equal(h3, f[**h3])
f = ->(a, **x) { [a,x] }
assert_raise(ArgumentError) { f[**{}] }
assert_warn(/The keyword argument for `\[\]' .* is passed as the last hash parameter/) do
assert_equal([{}, {}], f[**kw])
end
assert_warn(/The keyword argument for `\[\]' .* is passed as the last hash parameter/) do
assert_equal([h, {}], f[**h])
end
assert_warn(/The keyword argument for `\[\]' .* is passed as the last hash parameter/) do
assert_equal([h2, {}], f[**h2])
end
assert_warn(/The keyword argument for `\[\]' .* is passed as the last hash parameter/) do
assert_equal([h3, {}], f[**h3])
end
f = ->(a=1, **x) { [a, x] }
assert_equal([1, kw], f[**{}])
assert_equal([1, kw], f[**kw])
assert_equal([1, h], f[**h])
assert_equal([1, h2], f[**h2])
assert_equal([1, h3], f[**h3])
end
def p1
Proc.new do |str: "foo", num: 424242|

View file

@ -2912,6 +2912,12 @@ vm_callee_setup_block_arg(rb_execution_context_t *ec, struct rb_calling_info *ca
CALLER_SETUP_ARG(cfp, calling, ci, 1); /* splat arg */
if (UNLIKELY(IS_ARGS_KW_SPLAT(ci))) {
if (RHASH_EMPTY_P(argv[calling->argc-1])) {
calling->argc--;
}
}
if (arg_setup_type == arg_setup_block &&
calling->argc == 1 &&
iseq->body->param.flags.has_lead &&

View file

@ -240,6 +240,7 @@ THROW_DATA_CONSUMED_SET(struct vm_throw_data *obj)
#define IS_ARGS_SPLAT(ci) ((ci)->flag & VM_CALL_ARGS_SPLAT)
#define IS_ARGS_KEYWORD(ci) ((ci)->flag & VM_CALL_KWARG)
#define IS_ARGS_KW_SPLAT(ci) ((ci)->flag & VM_CALL_KW_SPLAT)
/* If this returns true, an optimized function returned by `vm_call_iseq_setup_func`
can be used as a fastpath. */