зеркало из https://github.com/github/ruby.git
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.
This commit is contained in:
Родитель
27b6746872
Коммит
c9f2b790ad
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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]
|
||||
|
|
30
vm_eval.c
30
vm_eval.c
|
@ -509,15 +509,22 @@ check_funcall_missing(rb_execution_context_t *ec, VALUE klass, VALUE recv, ID mi
|
|||
}
|
||||
|
||||
VALUE
|
||||
rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
|
||||
rb_check_funcall_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
|
||||
{
|
||||
return rb_check_funcall_default(recv, mid, argc, argv, Qundef);
|
||||
return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, kw_splat);
|
||||
}
|
||||
|
||||
VALUE
|
||||
rb_check_funcall_default(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE def)
|
||||
rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
|
||||
{
|
||||
return rb_check_funcall_default_kw(recv, mid, argc, argv, Qundef, RB_NO_KEYWORDS);
|
||||
}
|
||||
|
||||
VALUE
|
||||
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;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче