ruby-changes:57846
From: Jeremy <ko1@a...>
Date: Fri, 20 Sep 2019 23:49:25 +0900 (JST)
Subject: [ruby-changes:57846] c9f2b790ad (master): Handle keyword argument separation for Enumerator#size
https://git.ruby-lang.org/ruby.git/commit/?id=c9f2b790ad From c9f2b790adcff8df48e3192d18ee8afa02f5530c Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Wed, 18 Sep 2019 12:59:01 -0700 Subject: 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. diff --git a/enumerator.c b/enumerator.c index 2a94aa3..af9dc0f 100644 --- a/enumerator.c +++ b/enumerator.c @@ -1223,7 +1223,7 @@ enumerator_size(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L1223 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 998260f..caeccca 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -319,6 +319,7 @@ void rb_check_copyable(VALUE obj, VALUE orig); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L319 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 628cfd9..6845ba8 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 https://github.com/ruby/ruby/blob/trunk/internal.h#L2305 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 0b81179..62ba0bd 100644 --- a/test/ruby/test_keyword.rb +++ b/test/ruby/test_keyword.rb @@ -2424,6 +2424,105 @@ class TestKeywordArguments < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_keyword.rb#L2424 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] diff --git a/vm_eval.c b/vm_eval.c index 3a0645d..43c2042 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -509,15 +509,22 @@ check_funcall_missing(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mi https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L509 } 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 https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L534 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, https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L570 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; } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/