diff options
author | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-03-14 21:10:16 +0000 |
---|---|---|
committer | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-03-14 21:10:16 +0000 |
commit | 35c356ea72094dbbf1397d8f5acd065bd23d1177 (patch) | |
tree | c83c5a6b70d44f461e021643990a53c0942dd3ee /vm_insnhelper.c | |
parent | 74bdcfe81643866c5d72383f5b5b16d70123fdee (diff) | |
download | ruby-35c356ea72094dbbf1397d8f5acd065bd23d1177.tar.gz |
* vm_insnhelper.c: improve number of arguments error in case of
optional parameters (issue #6085)
* include/ruby/intern.h: define UNLIMITED_ARGUMENTS
* test/ruby/test_arity.rb: test for above
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35023 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r-- | vm_insnhelper.c | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 5f7de4a199..f471ca5ccb 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -105,13 +105,26 @@ vm_pop_frame(rb_thread_t *th) } /* method dispatch */ +static inline VALUE +rb_arg_error_new(int argc, int min, int max) { + VALUE err_mess = 0; + if (min == max) { + err_mess = rb_sprintf("wrong number of arguments (%d for %d)", argc, min); + } + else if (max == UNLIMITED_ARGUMENTS) { + err_mess = rb_sprintf("wrong number of arguments (%d for %d+)", argc, min); + } + else { + err_mess = rb_sprintf("wrong number of arguments (%d for %d..%d)", argc, min, max); + } + return rb_exc_new3(rb_eArgError, err_mess); +} -NORETURN(static void argument_error(const rb_iseq_t *iseq, int miss_argc, int correct_argc)); +NORETURN(static void argument_error(const rb_iseq_t *iseq, int miss_argc, int min_argc, int max_argc)); static void -argument_error(const rb_iseq_t *iseq, int miss_argc, int correct_argc) +argument_error(const rb_iseq_t *iseq, int miss_argc, int min_argc, int max_argc) { - VALUE mesg = rb_sprintf("wrong number of arguments (%d for %d)", miss_argc, correct_argc); - VALUE exc = rb_exc_new3(rb_eArgError, mesg); + VALUE exc = rb_arg_error_new(miss_argc, min_argc, max_argc); VALUE bt = rb_make_backtrace(); VALUE err_line = 0; @@ -154,7 +167,7 @@ unknown_keyword_error(const rb_iseq_t *iseq, VALUE hash) if (LIKELY((iseq)->arg_simple & 0x01)) { \ /* simple check */ \ if ((orig_argc) != (iseq)->argc) { \ - argument_error((iseq), (orig_argc), (iseq)->argc); \ + argument_error((iseq), (orig_argc), (iseq)->argc, (iseq)->argc); \ } \ (ret) = 0; \ } \ @@ -168,6 +181,9 @@ vm_callee_setup_arg_complex(rb_thread_t *th, const rb_iseq_t * iseq, const rb_block_t **block) { const int m = iseq->argc; + const int opts = iseq->arg_opts - (iseq->arg_opts > 0); + const int min = m + iseq->arg_post_len; + const int max = (iseq->arg_rest == -1) ? m + opts + iseq->arg_post_len : UNLIMITED_ARGUMENTS; int argc = orig_argc; VALUE *argv = orig_argv; rb_num_t opt_pc = 0; @@ -196,8 +212,8 @@ vm_callee_setup_arg_complex(rb_thread_t *th, const rb_iseq_t * iseq, } /* mandatory */ - if (argc < (m + iseq->arg_post_len)) { /* check with post arg */ - argument_error(iseq, argc, m + iseq->arg_post_len); + if ((argc < min) || (argc > max && max != UNLIMITED_ARGUMENTS)) { + argument_error(iseq, argc, min, max); } argv += m; @@ -217,12 +233,6 @@ vm_callee_setup_arg_complex(rb_thread_t *th, const rb_iseq_t * iseq, /* opt arguments */ if (iseq->arg_opts) { - const int opts = iseq->arg_opts - 1 /* no opt */; - - if (iseq->arg_rest == -1 && argc > opts) { - argument_error(iseq, orig_argc, m + opts + iseq->arg_post_len); - } - if (argc > opts) { argc -= opts; argv += opts; @@ -254,10 +264,6 @@ vm_callee_setup_arg_complex(rb_thread_t *th, const rb_iseq_t * iseq, VALUE blockval = Qnil; const rb_block_t *blockptr = *block; - if (argc != 0) { - argument_error(iseq, orig_argc, m + iseq->arg_post_len); - } - if (blockptr) { /* make Proc object */ if (blockptr->proc == 0) { @@ -274,10 +280,6 @@ vm_callee_setup_arg_complex(rb_thread_t *th, const rb_iseq_t * iseq, orig_argv[iseq->arg_block] = blockval; /* Proc or nil */ } - if (iseq->arg_keyword && argc != 0) { - argument_error(iseq, orig_argc, m + iseq->arg_post_len); - } - th->mark_stack_len = 0; return (int)opt_pc; } |