summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-19 01:11:12 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-19 01:11:12 (GMT)
commit976becf7eb18aa1592c703ac4d86a2cf9dfa701e (patch)
treeea2cf60b1ebf58b756e896484440036f5f3ba2cd
parenta9d575e507e1ce181ed71b9a7d4c3ad4f2f23e59 (diff)
vm_args.c: arity check of lambda
* vm_eval.c (rb_yield_lambda): new function which yields an array to a proc and splat to a lambda. mainly for Enumerable only. * vm_args.c (setup_parameters_complex): remove special lambda splatting for [Bug #9605]. [ruby-core:77065] [Bug #12705] * vm_insnhelper.c (vm_callee_setup_block_arg): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58019 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--KNOWNBUGS.rb9
-rw-r--r--enum.c70
-rw-r--r--internal.h1
-rw-r--r--test/ruby/test_lambda.rb6
-rw-r--r--test/ruby/test_yield.rb2
-rw-r--r--vm.c27
-rw-r--r--vm_args.c8
-rw-r--r--vm_eval.c7
-rw-r--r--vm_insnhelper.c6
9 files changed, 82 insertions, 54 deletions
diff --git a/KNOWNBUGS.rb b/KNOWNBUGS.rb
index 5e3c5dd..35a8e75 100644
--- a/KNOWNBUGS.rb
+++ b/KNOWNBUGS.rb
@@ -5,12 +5,3 @@
# This test file includes tests which point out known bugs.
# So all tests will cause failure.
#
-
-assert_equal "ArgumentError", %{
- def s(a) yield a; end
- begin
- s([1, 2], &lambda { |a,b| [a,b] })
- rescue ArgumentError => e
- e.class
- end
-}, '[Bug #12705]'
diff --git a/enum.c b/enum.c
index 0c35efc..4e14e82 100644
--- a/enum.c
+++ b/enum.c
@@ -39,7 +39,27 @@ rb_enum_values_pack(int argc, const VALUE *argv)
i = rb_enum_values_pack(argc, argv); \
} while (0)
-#define enum_yield rb_yield_values2
+static VALUE
+enum_yield(int argc, VALUE ary)
+{
+ if (argc > 1)
+ return rb_yield_lambda(ary);
+ if (argc == 1)
+ return rb_yield(ary);
+ return rb_yield_values2(0, 0);
+}
+
+static VALUE
+enum_yield_array(VALUE ary)
+{
+ long len = RARRAY_LEN(ary);
+
+ if (len > 1)
+ return rb_yield_lambda(ary);
+ if (len == 1)
+ return rb_yield(RARRAY_AREF(ary, 0));
+ return rb_yield_values2(0, 0);
+}
static VALUE
grep_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
@@ -60,7 +80,7 @@ grep_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
if (RTEST(rb_funcall(memo->v1, id_eqq, 1, i)) == RTEST(memo->u3.value)) {
- rb_ary_push(memo->v2, rb_yield(i));
+ rb_ary_push(memo->v2, enum_yield(argc, i));
}
return Qnil;
}
@@ -138,7 +158,7 @@ count_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
{
struct MEMO *memo = MEMO_CAST(memop);
- if (RTEST(enum_yield(argc, argv))) {
+ if (RTEST(rb_yield_values2(argc, argv))) {
memo->u3.cnt++;
}
return Qnil;
@@ -204,7 +224,7 @@ find_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
{
ENUM_WANT_SVALUE();
- if (RTEST(rb_yield(i))) {
+ if (RTEST(enum_yield(argc, i))) {
struct MEMO *memo = MEMO_CAST(memop);
MEMO_V1_SET(memo, i);
memo->u3.cnt = 1;
@@ -276,7 +296,7 @@ find_index_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memop))
{
struct MEMO *memo = MEMO_CAST(memop);
- if (RTEST(enum_yield(argc, argv))) {
+ if (RTEST(rb_yield_values2(argc, argv))) {
MEMO_V1_SET(memo, UINT2NUM(memo->u3.cnt));
rb_iter_break();
}
@@ -332,7 +352,7 @@ find_all_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
{
ENUM_WANT_SVALUE();
- if (RTEST(rb_yield(i))) {
+ if (RTEST(enum_yield(argc, i))) {
rb_ary_push(ary, i);
}
return Qnil;
@@ -402,7 +422,7 @@ reject_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
{
ENUM_WANT_SVALUE();
- if (!RTEST(rb_yield(i))) {
+ if (!RTEST(enum_yield(argc, i))) {
rb_ary_push(ary, i);
}
return Qnil;
@@ -441,7 +461,7 @@ enum_reject(VALUE obj)
static VALUE
collect_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
{
- rb_ary_push(ary, enum_yield(argc, argv));
+ rb_ary_push(ary, rb_yield_values2(argc, argv));
return Qnil;
}
@@ -490,7 +510,7 @@ flat_map_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
{
VALUE tmp;
- i = enum_yield(argc, argv);
+ i = rb_yield_values2(argc, argv);
tmp = rb_check_array_type(i);
if (NIL_P(tmp)) {
@@ -787,7 +807,7 @@ partition_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arys))
VALUE ary;
ENUM_WANT_SVALUE();
- if (RTEST(rb_yield(i))) {
+ if (RTEST(enum_yield(argc, i))) {
ary = memo->v1;
}
else {
@@ -833,7 +853,7 @@ group_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
ENUM_WANT_SVALUE();
- group = rb_yield(i);
+ group = enum_yield(argc, i);
values = rb_hash_aref(hash, group);
if (!RB_TYPE_P(values, T_ARRAY)) {
values = rb_ary_new3(1, i);
@@ -967,7 +987,7 @@ sort_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _data))
ENUM_WANT_SVALUE();
- v = rb_yield(i);
+ v = enum_yield(argc, i);
if (RBASIC(ary)->klass) {
rb_raise(rb_eRuntimeError, "sort_by reentered");
@@ -1141,7 +1161,7 @@ name##_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
static VALUE \
name##_iter_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, memo)) \
{ \
- return enum_##name##_func(enum_yield(argc, argv), MEMO_CAST(memo)); \
+ return enum_##name##_func(rb_yield_values2(argc, argv), MEMO_CAST(memo)); \
} \
\
static VALUE \
@@ -1360,7 +1380,7 @@ nmin_i(VALUE i, VALUE *_data, int argc, VALUE *argv)
ENUM_WANT_SVALUE();
if (data->by)
- cmpv = rb_yield(i);
+ cmpv = enum_yield(argc, i);
else
cmpv = i;
@@ -1856,7 +1876,7 @@ min_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
- v = rb_yield(i);
+ v = enum_yield(argc, i);
if (memo->v1 == Qundef) {
MEMO_V1_SET(memo, v);
MEMO_V2_SET(memo, i);
@@ -1917,7 +1937,7 @@ max_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
ENUM_WANT_SVALUE();
- v = rb_yield(i);
+ v = enum_yield(argc, i);
if (memo->v1 == Qundef) {
MEMO_V1_SET(memo, v);
MEMO_V2_SET(memo, i);
@@ -2054,7 +2074,7 @@ minmax_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, _memo))
ENUM_WANT_SVALUE();
- vi = rb_yield(i);
+ vi = enum_yield(argc, i);
if (memo->last_bv == Qundef) {
memo->last_bv = vi;
@@ -2237,7 +2257,7 @@ static VALUE
each_val_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, p))
{
ENUM_WANT_SVALUE();
- rb_yield(i);
+ enum_yield(argc, i);
return Qnil;
}
@@ -2484,7 +2504,7 @@ zip_ary(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
}
}
if (NIL_P(result)) {
- rb_yield(tmp);
+ enum_yield_array(tmp);
}
else {
rb_ary_push(result, tmp);
@@ -2535,7 +2555,7 @@ zip_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
}
}
if (NIL_P(result)) {
- rb_yield(tmp);
+ enum_yield_array(tmp);
}
else {
rb_ary_push(result, tmp);
@@ -2657,7 +2677,7 @@ enum_take(VALUE obj, VALUE n)
static VALUE
take_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
{
- if (!RTEST(enum_yield(argc, argv))) rb_iter_break();
+ if (!RTEST(rb_yield_values2(argc, argv))) rb_iter_break();
rb_ary_push(ary, rb_enum_values_pack(argc, argv));
return Qnil;
}
@@ -2737,7 +2757,7 @@ drop_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
struct MEMO *memo = MEMO_CAST(args);
ENUM_WANT_SVALUE();
- if (!memo->u3.state && !RTEST(rb_yield(i))) {
+ if (!memo->u3.state && !RTEST(enum_yield(argc, i))) {
memo->u3.state = TRUE;
}
if (memo->u3.state) {
@@ -2780,8 +2800,8 @@ cycle_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
{
ENUM_WANT_SVALUE();
- rb_ary_push(ary, i);
- rb_yield(i);
+ rb_ary_push(ary, argc > 1 ? i : rb_ary_new_from_values(argc, argv));
+ enum_yield(argc, i);
return Qnil;
}
@@ -2848,7 +2868,7 @@ enum_cycle(int argc, VALUE *argv, VALUE obj)
if (len == 0) return Qnil;
while (n < 0 || 0 < --n) {
for (i=0; i<len; i++) {
- rb_yield(RARRAY_AREF(ary, i));
+ enum_yield_array(RARRAY_AREF(ary, i));
}
}
return Qnil;
diff --git a/internal.h b/internal.h
index f011b7a..9537411 100644
--- a/internal.h
+++ b/internal.h
@@ -1698,6 +1698,7 @@ VALUE rb_check_funcall_with_hook(VALUE recv, ID mid, int argc, const VALUE *argv
VALUE rb_check_funcall_default(VALUE, ID, int, const VALUE *, VALUE);
VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, int *stateptr);
VALUE rb_yield_1(VALUE val);
+VALUE rb_yield_lambda(VALUE values);
/* vm_insnhelper.c */
VALUE rb_equal_opt(VALUE obj1, VALUE obj2);
diff --git a/test/ruby/test_lambda.rb b/test/ruby/test_lambda.rb
index 826bfcf..3ac2e4c 100644
--- a/test/ruby/test_lambda.rb
+++ b/test/ruby/test_lambda.rb
@@ -101,6 +101,12 @@ class TestLambdaParameters < Test::Unit::TestCase
assert_equal(:ok, x, bug13090)
end
+ def test_arity_error
+ assert_raise(ArgumentError, '[Bug #12705]') do
+ [1, 2].tap(&lambda {|a, b|})
+ end
+ end
+
def foo
assert_equal(nil, ->(&b){ b }.call)
end
diff --git a/test/ruby/test_yield.rb b/test/ruby/test_yield.rb
index 0690d3c..9b2b2f3 100644
--- a/test/ruby/test_yield.rb
+++ b/test/ruby/test_yield.rb
@@ -245,7 +245,7 @@ class TestRubyYieldGen < Test::Unit::TestCase
throw :emuerror, ArgumentError
end
else
- if args.length != params.length and !(args.length == 1 and Array === args[0] and args[0].length == params.length)
+ if args.length != params.length
throw :emuerror, ArgumentError
end
end
diff --git a/vm.c b/vm.c
index afdc5be..1eefee1 100644
--- a/vm.c
+++ b/vm.c
@@ -1019,14 +1019,16 @@ static inline VALUE
invoke_block_from_c_splattable(rb_thread_t *th, VALUE block_handler,
int argc, const VALUE *argv,
VALUE passed_block_handler, const rb_cref_t *cref,
- int is_lambda)
+ int splattable, int is_lambda)
{
again:
switch (vm_block_handler_type(block_handler)) {
case block_handler_type_iseq:
{
const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
- return invoke_iseq_block_from_c(th, captured, captured->self, argc, argv, passed_block_handler, cref, TRUE, is_lambda);
+ return invoke_iseq_block_from_c(th, captured, captured->self,
+ argc, argv, passed_block_handler,
+ cref, splattable, is_lambda);
}
case block_handler_type_ifunc:
return vm_yield_with_cfunc(th, VM_BH_TO_IFUNC_BLOCK(block_handler), VM_BH_TO_IFUNC_BLOCK(block_handler)->self,
@@ -1034,7 +1036,8 @@ invoke_block_from_c_splattable(rb_thread_t *th, VALUE block_handler,
case block_handler_type_symbol:
return vm_yield_with_symbol(th, VM_BH_TO_SYMBOL(block_handler), argc, argv, passed_block_handler);
case block_handler_type_proc:
- is_lambda = block_proc_is_lambda(VM_BH_TO_PROC(block_handler));
+ if (!splattable)
+ is_lambda = block_proc_is_lambda(VM_BH_TO_PROC(block_handler));
block_handler = vm_proc_to_block_handler(VM_BH_TO_PROC(block_handler));
goto again;
}
@@ -1057,19 +1060,31 @@ check_block_handler(rb_thread_t *th)
static VALUE
vm_yield_with_cref(rb_thread_t *th, int argc, const VALUE *argv, const rb_cref_t *cref, int is_lambda)
{
- return invoke_block_from_c_splattable(th, check_block_handler(th), argc, argv, VM_BLOCK_HANDLER_NONE, cref, is_lambda);
+ return invoke_block_from_c_splattable(th, check_block_handler(th),
+ argc, argv, VM_BLOCK_HANDLER_NONE,
+ cref, FALSE, is_lambda);
}
static VALUE
vm_yield(rb_thread_t *th, int argc, const VALUE *argv)
{
- return invoke_block_from_c_splattable(th, check_block_handler(th), argc, argv, VM_BLOCK_HANDLER_NONE, NULL, FALSE);
+ return invoke_block_from_c_splattable(th, check_block_handler(th),
+ argc, argv, VM_BLOCK_HANDLER_NONE,
+ NULL, FALSE, FALSE);
}
static VALUE
vm_yield_with_block(rb_thread_t *th, int argc, const VALUE *argv, VALUE block_handler)
{
- return invoke_block_from_c_splattable(th, check_block_handler(th), argc, argv, block_handler, NULL, FALSE);
+ return invoke_block_from_c_splattable(th, check_block_handler(th),
+ argc, argv, block_handler,
+ NULL, FALSE, FALSE);
+}
+
+static VALUE
+vm_yield_lambda_splattable(rb_thread_t *th, VALUE args)
+{
+ return invoke_block_from_c_splattable(th, check_block_handler(th), 1, &args, VM_BLOCK_HANDLER_NONE, NULL, TRUE, FALSE);
}
static inline VALUE
diff --git a/vm_args.c b/vm_args.c
index 9249984..6df30a4 100644
--- a/vm_args.c
+++ b/vm_args.c
@@ -596,13 +596,7 @@ setup_parameters_complex(rb_thread_t * const th, const rb_iseq_t * const iseq,
}
break;
case arg_setup_lambda:
- if (given_argc == 1 &&
- given_argc != iseq->body->param.lead_num &&
- !iseq->body->param.flags.has_opt &&
- !iseq->body->param.flags.has_rest &&
- args_check_block_arg0(args, th)) {
- given_argc = RARRAY_LENINT(args->rest);
- }
+ break;
}
/* argc check */
diff --git a/vm_eval.c b/vm_eval.c
index 19905e0..d820f62 100644
--- a/vm_eval.c
+++ b/vm_eval.c
@@ -19,6 +19,7 @@ static inline VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv
static inline VALUE vm_yield_with_cref(rb_thread_t *th, int argc, const VALUE *argv, const rb_cref_t *cref, int is_lambda);
static inline VALUE vm_yield(rb_thread_t *th, int argc, const VALUE *argv);
static inline VALUE vm_yield_with_block(rb_thread_t *th, int argc, const VALUE *argv, VALUE block_handler);
+static inline VALUE vm_yield_lambda_splattable(rb_thread_t *th, VALUE args);
static VALUE vm_exec(rb_thread_t *th);
static void vm_set_eval_stack(rb_thread_t * th, const rb_iseq_t *iseq, const rb_cref_t *cref, const struct rb_block *base_block);
static int vm_collect_local_variables_in_heap(rb_thread_t *th, const VALUE *dfp, const struct local_var_list *vars);
@@ -1068,6 +1069,12 @@ rb_yield_splat(VALUE values)
}
VALUE
+rb_yield_lambda(VALUE values)
+{
+ return vm_yield_lambda_splattable(GET_THREAD(), values);
+}
+
+VALUE
rb_yield_block(VALUE val, VALUE arg, int argc, const VALUE *argv, VALUE blockarg)
{
return vm_yield_with_block(GET_THREAD(), argc, argv,
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 695567f..21a358c 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -2543,12 +2543,6 @@ vm_callee_setup_block_arg(rb_thread_t *th, struct rb_calling_info *calling, cons
calling->argc = iseq->body->param.lead_num; /* simply truncate arguments */
}
}
- else if (arg_setup_type == arg_setup_lambda &&
- calling->argc == 1 &&
- !NIL_P(arg0 = vm_callee_setup_block_arg_arg0_check(argv)) &&
- RARRAY_LEN(arg0) == iseq->body->param.lead_num) {
- calling->argc = vm_callee_setup_block_arg_arg0_splat(cfp, iseq, argv, arg0);
- }
else {
argument_arity_error(th, iseq, calling->argc, iseq->body->param.lead_num, iseq->body->param.lead_num);
}