Fixed a bug in argument number check with kwargs; fix
MODIFY
src/vm.c
src/vm.c
@@ -1780,13 +1780,11 @@ RETRY_TRY_BLOCK:
/* strict argument check */
if (mrb->c->ci->proc && MRB_PROC_STRICT_P(mrb->c->ci->proc)) {
- if (argc >= 0 && !(argc <= 1 && kd)) {
- if (argc < m1 + m2 + kd || (r == 0 && argc > len + kd)) {
+ if (argc < m1 + m2 || (r == 0 && argc > len + kd)) {
argnum_error(mrb, m1+m2);
goto L_RAISE;
}
}
- }
/* extract first argument array to arguments */
else if (len > 1 && argc == 1 && mrb_array_p(argv[0])) {
mrb_gc_protect(mrb, argv[0]);
@@ -1810,8 +1808,7 @@ RETRY_TRY_BLOCK:
kargs = 0;
}
else {
- mrb_value str = mrb_str_new_lit(mrb, "Excepcted `Hash` as last argument for keyword arguments");
- mrb_exc_set(mrb, mrb_exc_new_str(mrb, E_ARGUMENT_ERROR, str));
+ argnum_error(mrb, m1+m2);
goto L_RAISE;
}
if (MRB_ASPEC_KEY(a) > 0) {

MODIFY
test/t/syntax.rb
test/t/syntax.rb
@@ -471,6 +471,10 @@ this is a comment that has extra after =begin and =end with tabs after it
end
assert 'keyword arguments' do
+ def m(a, b:1) [a, b] end
+ assert_equal [1, 1], m(1)
+ assert_equal [1, 2], m(1, b: 2)
+
def m(a, b:) [a, b] end
assert_equal [1, 2], m(1, b: 2)
assert_raise(ArgumentError) { m b: 1 }

Fixed a bug in argument number check with kwargs; fix #4159