summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/ruby/intern.h1
-rw-r--r--proc.c20
-rw-r--r--test/ruby/test_proc.rb136
3 files changed, 155 insertions, 2 deletions
diff --git a/include/ruby/intern.h b/include/ruby/intern.h
index dc9abbf52b..a44d7fd2a0 100644
--- a/include/ruby/intern.h
+++ b/include/ruby/intern.h
@@ -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);
diff --git a/proc.c b/proc.c
index 62b8eadb8f..91f58785d9 100644
--- a/proc.c
+++ b/proc.c
@@ -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));
diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb
index fbd3a6c68a..b7d644345d 100644
--- a/test/ruby/test_proc.rb
+++ b/test/ruby/test_proc.rb
@@ -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
+