diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-09-18 12:59:01 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-09-20 07:45:18 -0700 |
commit | c9f2b790adcff8df48e3192d18ee8afa02f5530c (patch) | |
tree | 76cd3f6cd1de86b9d6d947f0a2237615074d7bc9 | |
parent | 27b67468724dc48ed8305d8cb33484a4af98fc05 (diff) |
Handle keyword argument separation for Enumerator#size
When Object#to_enum is passed a block, the block is called to get
a size with the arguments given to to_enum. This calls the block
with the same keyword flag as to_enum is called with.
This requires adding rb_check_funcall_kw and
rb_check_funcall_default_kw to handle keyword flags.
-rw-r--r-- | enumerator.c | 2 | ||||
-rw-r--r-- | include/ruby/intern.h | 1 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | test/ruby/test_keyword.rb | 99 | ||||
-rw-r--r-- | vm_eval.c | 28 |
5 files changed, 124 insertions, 7 deletions
diff --git a/enumerator.c b/enumerator.c index 2a94aa388f..af9dc0fd2b 100644 --- a/enumerator.c +++ b/enumerator.c @@ -1223,7 +1223,7 @@ enumerator_size(VALUE obj) argc = (int)RARRAY_LEN(e->args); argv = RARRAY_CONST_PTR(e->args); } - size = rb_check_funcall(e->size, id_call, argc, argv); + size = rb_check_funcall_kw(e->size, id_call, argc, argv, e->kw_splat); if (size != Qundef) return size; return e->size; } diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 998260f2ee..caeccca22f 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -319,6 +319,7 @@ void rb_check_copyable(VALUE obj, VALUE orig); int rb_sourceline(void); const char *rb_sourcefile(void); VALUE rb_check_funcall(VALUE, ID, int, const VALUE*); +VALUE rb_check_funcall_kw(VALUE, ID, int, const VALUE*, int); NORETURN(MJIT_STATIC void rb_error_arity(int, int, int)); static inline int diff --git a/internal.h b/internal.h index 628cfd9912..6845ba825d 100644 --- a/internal.h +++ b/internal.h @@ -2305,6 +2305,7 @@ VALUE rb_check_funcall_with_hook_kw(VALUE recv, ID mid, int argc, const VALUE *a rb_check_funcall_hook *hook, VALUE arg, int kw_splat); const char *rb_type_str(enum ruby_value_type type); VALUE rb_check_funcall_default(VALUE, ID, int, const VALUE *, VALUE); +VALUE rb_check_funcall_default_kw(VALUE, ID, int, const VALUE *, VALUE, int); VALUE rb_yield_1(VALUE val); VALUE rb_yield_force_blockarg(VALUE values); VALUE rb_lambda_call(VALUE obj, ID mid, int argc, const VALUE *argv, diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb index 0b81179817..62ba0bd0de 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -2424,6 +2424,105 @@ class TestKeywordArguments < Test::Unit::TestCase assert_equal([h3, kw], [c].dig(0, h3, **{})) end + def test_enumerator_size_kwsplat + kw = {} + h = {:a=>1} + h2 = {'a'=>1} + h3 = {'a'=>1, :a=>1} + + c = Object.new + c.to_enum(:each){|*args| args}.size + m = ->(*args){ args } + assert_equal([], c.to_enum(:each, **{}, &m).size) + assert_equal([], c.to_enum(:each, **kw, &m).size) + assert_equal([h], c.to_enum(:each, **h, &m).size) + assert_equal([h], c.to_enum(:each, a: 1, &m).size) + assert_equal([h2], c.to_enum(:each, **h2, &m).size) + assert_equal([h3], c.to_enum(:each, **h3, &m).size) + assert_equal([h3], c.to_enum(:each, a: 1, **h2, &m).size) + + m = ->(){ } + assert_nil(c.to_enum(:each, **{}, &m).size) + assert_nil(c.to_enum(:each, **kw, &m).size) + assert_raise(ArgumentError) { c.to_enum(:each, **h, &m).size } + assert_raise(ArgumentError) { c.to_enum(:each, a: 1, &m).size } + assert_raise(ArgumentError) { c.to_enum(:each, **h2, &m).size } + assert_raise(ArgumentError) { c.to_enum(:each, **h3, &m).size } + assert_raise(ArgumentError) { c.to_enum(:each, a: 1, **h2, &m).size } + + m = ->(args){ args } + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + assert_equal(kw, c.to_enum(:each, **{}, &m).size) + end + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + assert_equal(kw, c.to_enum(:each, **kw, &m).size) + end + assert_equal(kw, c.to_enum(:each, kw, **kw, &m).size) + assert_equal(h, c.to_enum(:each, **h, &m).size) + assert_equal(h, c.to_enum(:each, a: 1, &m).size) + assert_equal(h2, c.to_enum(:each, **h2, &m).size) + assert_equal(h3, c.to_enum(:each, **h3, &m).size) + assert_equal(h3, c.to_enum(:each, a: 1, **h2, &m).size) + + m = ->(**args){ args } + assert_equal(kw, c.to_enum(:each, **{}, &m).size) + assert_equal(kw, c.to_enum(:each, **kw, &m).size) + assert_equal(h, c.to_enum(:each, **h, &m).size) + assert_equal(h, c.to_enum(:each, a: 1, &m).size) + assert_equal(h2, c.to_enum(:each, **h2, &m).size) + assert_equal(h3, c.to_enum(:each, **h3, &m).size) + assert_equal(h3, c.to_enum(:each, a: 1, **h2, &m).size) + assert_warn(/The last argument is used as the keyword parameter/m) do + assert_equal(h, c.to_enum(:each, h, &m).size) + end + assert_raise(ArgumentError) { c.to_enum(:each, h2, &m).size } + assert_warn(/The last argument is split into positional and keyword parameters/m) do + assert_raise(ArgumentError) { c.to_enum(:each, h3, &m).size } + end + + m = ->(arg, **args){ [arg, args] } + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + c.to_enum(:each, **{}, &m).size + end + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + c.to_enum(:each, **kw, &m).size + end + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + assert_equal([h, kw], c.to_enum(:each, **h, &m).size) + end + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + assert_equal([h, kw], c.to_enum(:each, a: 1, &m).size) + end + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + assert_equal([h2, kw], c.to_enum(:each, **h2, &m).size) + end + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + assert_equal([h3, kw], c.to_enum(:each, **h3, &m).size) + end + assert_warn(/The keyword argument is passed as the last hash parameter/m) do + assert_equal([h3, kw], c.to_enum(:each, a: 1, **h2, &m).size) + end + assert_equal([h, kw], c.to_enum(:each, h, &m).size) + assert_equal([h2, kw], c.to_enum(:each, h2, &m).size) + assert_equal([h3, kw], c.to_enum(:each, h3, &m).size) + + m = ->(arg=1, **args){ [arg, args] } + assert_equal([1, kw], c.to_enum(:each, **{}, &m).size) + assert_equal([1, kw], c.to_enum(:each, **kw, &m).size) + assert_equal([1, h], c.to_enum(:each, **h, &m).size) + assert_equal([1, h], c.to_enum(:each, a: 1, &m).size) + assert_equal([1, h2], c.to_enum(:each, **h2, &m).size) + assert_equal([1, h3], c.to_enum(:each, **h3, &m).size) + assert_equal([1, h3], c.to_enum(:each, a: 1, **h2, &m).size) + assert_warn(/The last argument is used as the keyword parameter/m) do + assert_equal([1, h], c.to_enum(:each, h, &m).size) + end + assert_equal([h2, kw], c.to_enum(:each, h2, &m).size) + assert_warn(/The last argument is split into positional and keyword parameters/m) do + assert_equal([h2, h], c.to_enum(:each, h3, &m).size) + end + end + def p1 Proc.new do |str: "foo", num: 424242| [str, num] @@ -509,15 +509,22 @@ check_funcall_missing(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mi } VALUE +rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat) +{ + return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, kw_splat); +} + +VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv) { - return rb_check_funcall_default(recv, mid, argc, argv, Qundef); + return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, RB_NO_KEYWORDS); } VALUE -rb_check_funcall_default(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def) +rb_check_funcall_default_kw(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def, int kw_splat) { VALUE klass = CLASS_OF(recv); + VALUE v, ret; const rb_callable_method_entry_t *me; rb_execution_context_t *ec = GET_EC(); int respond = check_funcall_respond_to(ec, klass, recv, mid); @@ -527,13 +534,22 @@ rb_check_funcall_default(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE me = rb_search_method_entry(recv, mid); if (!check_funcall_callable(ec, me)) { - VALUE ret = check_funcall_missing(ec, klass, recv, mid, argc, argv, - respond, def, RB_NO_KEYWORDS); + ret = check_funcall_missing(ec, klass, recv, mid, argc, argv, + respond, def, kw_splat); if (ret == Qundef) ret = def; return ret; } stack_check(ec); - return rb_vm_call0(ec, recv, mid, argc, argv, me, RB_NO_KEYWORDS); + v = rb_adjust_argv_kw_splat(&argc, &argv, &kw_splat); + ret = rb_vm_call0(ec, recv, mid, argc, argv, me, kw_splat); + rb_free_tmp_buffer(&v); + return ret; +} + +VALUE +rb_check_funcall_default(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def) +{ + return rb_check_funcall_default_kw(recv, mid, argc, argv, def, RB_NO_KEYWORDS); } VALUE @@ -554,7 +570,7 @@ rb_check_funcall_with_hook_kw(VALUE recv, ID mid, int argc, const VALUE *argv, me = rb_search_method_entry(recv, mid); if (!check_funcall_callable(ec, me)) { ret = check_funcall_missing(ec, klass, recv, mid, argc, argv, - respond, Qundef, kw_splat); + respond, Qundef, kw_splat); (*hook)(ret != Qundef, recv, mid, argc, argv, arg); return ret; } |