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

Fix keyword argument separation issues in Proc#{<<,>>}

This requires adding rb_proc_call_with_block_kw.
This commit is contained in:
Jeremy Evans 2019-09-25 11:22:14 -07:00
parent 0aa267f985
commit 760893d2f8
Notes: git 2019-09-27 00:02:33 +09:00
3 changed files with 155 additions and 2 deletions

View file

@ -453,6 +453,7 @@ VALUE rb_proc_new(rb_block_call_func_t, VALUE);
VALUE rb_obj_is_proc(VALUE);
VALUE rb_proc_call(VALUE, VALUE);
VALUE rb_proc_call_with_block(VALUE, int argc, const VALUE *argv, VALUE);
VALUE rb_proc_call_with_block_kw(VALUE, int argc, const VALUE *argv, VALUE, int);
int rb_proc_arity(VALUE);
VALUE rb_proc_lambda_p(VALUE);
VALUE rb_binding_new(void);

20
proc.c
View file

@ -954,6 +954,22 @@ proc_to_block_handler(VALUE procval)
return NIL_P(procval) ? VM_BLOCK_HANDLER_NONE : procval;
}
extern VALUE rb_adjust_argv_kw_splat(int *argc, const VALUE **argv, int *kw_splat);
VALUE
rb_proc_call_with_block_kw(VALUE self, int argc, const VALUE *argv, VALUE passed_procval, int kw_splat)
{
rb_execution_context_t *ec = GET_EC();
VALUE vret;
rb_proc_t *proc;
VALUE v = rb_adjust_argv_kw_splat(&argc, &argv, &kw_splat);
GetProcPtr(self, proc);
vret = rb_vm_invoke_proc(ec, proc, argc, argv, kw_splat, proc_to_block_handler(passed_procval));
rb_free_tmp_buffer(&v);
RB_GC_GUARD(self);
return vret;
}
VALUE
rb_proc_call_with_block(VALUE self, int argc, const VALUE *argv, VALUE passed_procval)
{
@ -3215,9 +3231,9 @@ compose(RB_BLOCK_CALL_FUNC_ARGLIST(_, args))
g = RARRAY_AREF(args, 1);
if (rb_obj_is_proc(g))
fargs = rb_proc_call_with_block(g, argc, argv, blockarg);
fargs = rb_proc_call_with_block_kw(g, argc, argv, blockarg, RB_PASS_CALLED_KEYWORDS);
else
fargs = rb_funcall_with_block(g, idCall, argc, argv, blockarg);
fargs = rb_funcall_with_block_kw(g, idCall, argc, argv, blockarg, RB_PASS_CALLED_KEYWORDS);
if (rb_obj_is_proc(f))
return rb_proc_call(f, rb_ary_new3(1, fargs));

View file

@ -1512,3 +1512,139 @@ class TestProc < Test::Unit::TestCase
def m1(&b) b end; def m2(); m1 { next 42 } end }.m2.call)
end
end
class TestProcKeywords < Test::Unit::TestCase
def test_compose_keywords
f = ->(**kw) { kw.merge(:a=>1) }
g = ->(kw) { kw.merge(:a=>2) }
assert_equal(2, (f >> g).call(a: 3)[:a])
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (f << g).call(a: 3)[:a])
end
assert_equal(2, (f >> g).call(a: 3)[:a])
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (f << g).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(2, (f >> g).call({a: 3})[:a])
end
assert_equal(2, (g << f).call(a: 3)[:a])
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (g >> f).call(a: 3)[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(2, (g << f).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (g >> f).call({a: 3})[:a])
end
assert_warn(/The keyword argument is passed as the last hash parameter.*The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (f << g).call(**{})[:a])
end
assert_equal(2, (f >> g).call(**{})[:a])
end
def test_compose_keywords_method
f = ->(**kw) { kw.merge(:a=>1) }.method(:call)
g = ->(kw) { kw.merge(:a=>2) }.method(:call)
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (f << g).call(a: 3)[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(2, (f >> g).call(a: 3)[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (f << g).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(2, (f >> g).call({a: 3})[:a])
end
assert_equal(2, (g << f).call(a: 3)[:a])
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (g >> f).call(a: 3)[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(2, (g << f).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (g >> f).call({a: 3})[:a])
end
assert_warn(/The keyword argument is passed as the last hash parameter.*The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (f << g).call(**{})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(2, (f >> g).call(**{})[:a])
end
end
def test_compose_keywords_non_proc
f = ->(**kw) { kw.merge(:a=>1) }
g = Object.new
def g.call(kw) kw.merge(:a=>2) end
def g.to_proc; method(:call).to_proc; end
def g.<<(f) to_proc << f end
def g.>>(f) to_proc >> f end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (f << g).call(a: 3)[:a])
end
assert_equal(2, (f >> g).call(a: 3)[:a])
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (f << g).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(2, (f >> g).call({a: 3})[:a])
end
assert_equal(2, (g << f).call(a: 3)[:a])
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (g >> f).call(a: 3)[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(2, (g << f).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for method/m) do
assert_equal(1, (g >> f).call({a: 3})[:a])
end
assert_warn(/The keyword argument is passed as the last hash parameter.*for `call'/m) do
assert_equal(1, (f << g).call(**{})[:a])
end
assert_equal(2, (f >> g).call(**{})[:a])
f = ->(kw) { kw.merge(:a=>1) }
g = Object.new
def g.call(**kw) kw.merge(:a=>2) end
def g.to_proc; method(:call).to_proc; end
def g.<<(f) to_proc << f end
def g.>>(f) to_proc >> f end
assert_equal(1, (f << g).call(a: 3)[:a])
assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
assert_equal(2, (f >> g).call(a: 3)[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
assert_equal(1, (f << g).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
assert_equal(2, (f >> g).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
assert_equal(2, (g << f).call(a: 3)[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
assert_equal(1, (g >> f).call(a: 3)[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
assert_equal(2, (g << f).call({a: 3})[:a])
end
assert_warn(/The last argument is used as the keyword parameter.*for `call'/m) do
assert_equal(1, (g >> f).call({a: 3})[:a])
end
assert_equal(1, (f << g).call(**{})[:a])
assert_warn(/The keyword argument is passed as the last hash parameter.*The last argument is used as the keyword parameter.*for `call'/m) do
assert_equal(2, (f >> g).call(**{})[:a])
end
end
end