summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2019-09-16 13:19:06 -0700
committerJeremy Evans <code@jeremyevans.net>2019-09-17 16:22:44 -0700
commit9b35dc38644c10eed008f9ba19a7224f2fb49af2 (patch)
tree9c6c97cfd1cd828d8796145956621d6ac08ea6e8
parent0785469a400b00eb4576fedbf93b614c70eaf760 (diff)
Pass keyword argument flag when rb_call_super_kw calls method_missing
This makes method_missing take a flag for whether keyword arguments were passed. Adds tests both for rb_call_super_kw usage as well as general usage of super calling method_missing in Ruby methods.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2462
-rw-r--r--ext/-test-/rb_call_super_kw/depend14
-rwxr-xr-xext/-test-/rb_call_super_kw/extconf.rb1
-rw-r--r--ext/-test-/rb_call_super_kw/rb_call_super_kw.c14
-rw-r--r--test/ruby/test_keyword.rb194
-rw-r--r--vm_args.c4
-rw-r--r--vm_eval.c23
6 files changed, 237 insertions, 13 deletions
diff --git a/ext/-test-/rb_call_super_kw/depend b/ext/-test-/rb_call_super_kw/depend
new file mode 100644
index 0000000000..f65dcf9694
--- /dev/null
+++ b/ext/-test-/rb_call_super_kw/depend
@@ -0,0 +1,14 @@
+# AUTOGENERATED DEPENDENCIES START
+rb_call_super_kw.o: $(RUBY_EXTCONF_H)
+rb_call_super_kw.o: $(arch_hdrdir)/ruby/config.h
+rb_call_super_kw.o: $(hdrdir)/ruby.h
+rb_call_super_kw.o: $(hdrdir)/ruby/assert.h
+rb_call_super_kw.o: $(hdrdir)/ruby/backward.h
+rb_call_super_kw.o: $(hdrdir)/ruby/defines.h
+rb_call_super_kw.o: $(hdrdir)/ruby/intern.h
+rb_call_super_kw.o: $(hdrdir)/ruby/missing.h
+rb_call_super_kw.o: $(hdrdir)/ruby/ruby.h
+rb_call_super_kw.o: $(hdrdir)/ruby/st.h
+rb_call_super_kw.o: $(hdrdir)/ruby/subst.h
+rb_call_super_kw.o: rb_call_super_kw.c
+# AUTOGENERATED DEPENDENCIES END
diff --git a/ext/-test-/rb_call_super_kw/extconf.rb b/ext/-test-/rb_call_super_kw/extconf.rb
new file mode 100755
index 0000000000..c6a5c720d7
--- /dev/null
+++ b/ext/-test-/rb_call_super_kw/extconf.rb
@@ -0,0 +1 @@
+create_makefile("-test-/rb_call_super_kw")
diff --git a/ext/-test-/rb_call_super_kw/rb_call_super_kw.c b/ext/-test-/rb_call_super_kw/rb_call_super_kw.c
new file mode 100644
index 0000000000..7f094545d2
--- /dev/null
+++ b/ext/-test-/rb_call_super_kw/rb_call_super_kw.c
@@ -0,0 +1,14 @@
+#include <ruby.h>
+
+static VALUE
+rb_call_super_kw_m(int argc, VALUE *argv, VALUE self)
+{
+ return rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
+}
+
+void
+Init_rb_call_super_kw(void) {
+ VALUE module = rb_define_module("Bug");
+ module = rb_define_module_under(module, "RbCallSuperKw");
+ rb_define_method(module, "m", rb_call_super_kw_m, -1);
+}
diff --git a/test/ruby/test_keyword.rb b/test/ruby/test_keyword.rb
index 5a85b85f47..d99a73ff0c 100644
--- a/test/ruby/test_keyword.rb
+++ b/test/ruby/test_keyword.rb
@@ -1,5 +1,6 @@
# frozen_string_literal: false
require 'test/unit'
+require '-test-/rb_call_super_kw'
class TestKeywordArguments < Test::Unit::TestCase
def f1(str: "foo", num: 424242)
@@ -1518,6 +1519,199 @@ class TestKeywordArguments < Test::Unit::TestCase
assert_equal([1, h3], c.m(a: 1, **h2))
end
+ def test_super_method_missing_kwsplat
+ kw = {}
+ h = {:a=>1}
+ h2 = {'a'=>1}
+ h3 = {'a'=>1, :a=>1}
+
+ c = Class.new do
+ def m(*args, **kw)
+ super
+ end
+ end.new
+ def c.method_missing(_, *args)
+ args
+ end
+ assert_equal([], c.m(**{}))
+ assert_equal([], c.m(**kw))
+ assert_equal([h], c.m(**h))
+ assert_equal([h], c.m(a: 1))
+ assert_equal([h2], c.m(**h2))
+ assert_equal([h3], c.m(**h3))
+ assert_equal([h3], c.m(a: 1, **h2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_); end
+ assert_nil(c.m(**{}))
+ assert_nil(c.m(**kw))
+ assert_raise(ArgumentError) { c.m(**h) }
+ assert_raise(ArgumentError) { c.m(a: 1) }
+ assert_raise(ArgumentError) { c.m(**h2) }
+ assert_raise(ArgumentError) { c.m(**h3) }
+ assert_raise(ArgumentError) { c.m(a: 1, **h2) }
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, args)
+ args
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal(kw, c.m(**{}))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal(kw, c.m(**kw))
+ end
+ assert_equal(h, c.m(**h))
+ assert_equal(h, c.m(a: 1))
+ assert_equal(h2, c.m(**h2))
+ assert_equal(h3, c.m(**h3))
+ assert_equal(h3, c.m(a: 1, **h2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, **args)
+ args
+ end
+ assert_equal(kw, c.m(**{}))
+ assert_equal(kw, c.m(**kw))
+ assert_equal(h, c.m(**h))
+ assert_equal(h, c.m(a: 1))
+ assert_equal(h2, c.m(**h2))
+ assert_equal(h3, c.m(a: 1, **h2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, arg, **args)
+ [arg, args]
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([kw, kw], c.m(**{}))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([kw, kw], c.m(**kw))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h, kw], c.m(**h))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h, kw], c.m(a: 1))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h2, kw], c.m(**h2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h3, kw], c.m(**h3))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h3, kw], c.m(a: 1, **h2))
+ end
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, arg=1, **args)
+ [arg=1, args]
+ end
+ assert_equal([1, kw], c.m(**{}))
+ assert_equal([1, kw], c.m(**kw))
+ assert_equal([1, h], c.m(**h))
+ assert_equal([1, h], c.m(a: 1))
+ assert_equal([1, h2], c.m(**h2))
+ assert_equal([1, h3], c.m(**h3))
+ assert_equal([1, h3], c.m(a: 1, **h2))
+ end
+
+ def test_rb_call_super_kw_method_missing_kwsplat
+ kw = {}
+ h = {:a=>1}
+ h2 = {'a'=>1}
+ h3 = {'a'=>1, :a=>1}
+
+ c = Object.new
+ c.extend Bug::RbCallSuperKw
+ def c.method_missing(_, *args)
+ args
+ end
+ assert_equal([], c.m(**{}))
+ assert_equal([], c.m(**kw))
+ assert_equal([h], c.m(**h))
+ assert_equal([h], c.m(a: 1))
+ assert_equal([h2], c.m(**h2))
+ assert_equal([h3], c.m(**h3))
+ assert_equal([h3], c.m(a: 1, **h2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_); end
+ assert_nil(c.m(**{}))
+ assert_nil(c.m(**kw))
+ assert_raise(ArgumentError) { c.m(**h) }
+ assert_raise(ArgumentError) { c.m(a: 1) }
+ assert_raise(ArgumentError) { c.m(**h2) }
+ assert_raise(ArgumentError) { c.m(**h3) }
+ assert_raise(ArgumentError) { c.m(a: 1, **h2) }
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, args)
+ args
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal(kw, c.m(**{}))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal(kw, c.m(**kw))
+ end
+ assert_equal(h, c.m(**h))
+ assert_equal(h, c.m(a: 1))
+ assert_equal(h2, c.m(**h2))
+ assert_equal(h3, c.m(**h3))
+ assert_equal(h3, c.m(a: 1, **h2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, **args)
+ args
+ end
+ assert_equal(kw, c.m(**{}))
+ assert_equal(kw, c.m(**kw))
+ assert_equal(h, c.m(**h))
+ assert_equal(h, c.m(a: 1))
+ assert_equal(h2, c.m(**h2))
+ assert_equal(h3, c.m(a: 1, **h2))
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, arg, **args)
+ [arg, args]
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([kw, kw], c.m(**{}))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([kw, kw], c.m(**kw))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h, kw], c.m(**h))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h, kw], c.m(a: 1))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h2, kw], c.m(**h2))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h3, kw], c.m(**h3))
+ end
+ assert_warn(/The keyword argument is passed as the last hash parameter.* for `method_missing'/m) do
+ assert_equal([h3, kw], c.m(a: 1, **h2))
+ end
+
+ c.singleton_class.remove_method(:method_missing)
+ def c.method_missing(_, arg=1, **args)
+ [arg=1, args]
+ end
+ assert_equal([1, kw], c.m(**{}))
+ assert_equal([1, kw], c.m(**kw))
+ assert_equal([1, h], c.m(**h))
+ assert_equal([1, h], c.m(a: 1))
+ assert_equal([1, h2], c.m(**h2))
+ assert_equal([1, h3], c.m(**h3))
+ assert_equal([1, h3], c.m(a: 1, **h2))
+ end
+
def test_define_method_kwsplat
kw = {}
h = {:a=>1}
diff --git a/vm_args.c b/vm_args.c
index 0f4f95e4cd..459f60ea76 100644
--- a/vm_args.c
+++ b/vm_args.c
@@ -13,7 +13,7 @@ NORETURN(static void argument_arity_error(rb_execution_context_t *ec, const rb_i
NORETURN(static void argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const char *error, const VALUE keys));
VALUE rb_keyword_error_new(const char *error, VALUE keys); /* class.c */
static VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv,
- enum method_missing_reason call_status);
+ enum method_missing_reason call_status, int kw_splat);
struct args_info {
/* basic args info */
@@ -1072,7 +1072,7 @@ refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
vm_passed_block_handler_set(ec, blockarg);
}
if (!me) {
- return method_missing(obj, mid, argc, argv, MISSING_NOENTRY);
+ return method_missing(obj, mid, argc, argv, MISSING_NOENTRY, VM_NO_KEYWORDS);
}
return rb_vm_call0(ec, obj, mid, argc, argv, me, VM_NO_KEYWORDS);
}
diff --git a/vm_eval.c b/vm_eval.c
index c896a44f79..22a45095b4 100644
--- a/vm_eval.c
+++ b/vm_eval.c
@@ -15,7 +15,7 @@ struct local_var_list {
VALUE tbl;
};
-static inline VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status);
+static inline VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat);
static inline VALUE vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, const rb_cref_t *cref, int is_lambda);
static inline VALUE vm_yield(rb_execution_context_t *ec, int argc, const VALUE *argv);
static inline VALUE vm_yield_with_block(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE block_handler);
@@ -193,7 +193,7 @@ vm_call0_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const
if (!super_class || !(cc->me = rb_callable_method_entry(super_class, ci->mid))) {
enum method_missing_reason ex = (type == VM_METHOD_TYPE_ZSUPER) ? MISSING_SUPER : 0;
- ret = method_missing(calling->recv, ci->mid, calling->argc, argv, ex);
+ ret = method_missing(calling->recv, ci->mid, calling->argc, argv, ex, calling->kw_splat);
goto success;
}
RUBY_VM_CHECK_INTS(ec);
@@ -206,7 +206,7 @@ vm_call0_body(rb_execution_context_t *ec, struct rb_calling_info *calling, const
{
vm_passed_block_handler_set(ec, calling->block_handler);
return method_missing(calling->recv, ci->mid, calling->argc,
- argv, MISSING_NOENTRY);
+ argv, MISSING_NOENTRY, calling->kw_splat);
}
case VM_METHOD_TYPE_OPTIMIZED:
switch (cc->me->def->body.optimize_type) {
@@ -284,6 +284,7 @@ vm_call_super(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_sp
{
VALUE recv = ec->cfp->self;
VALUE klass;
+ VALUE v, ret;
ID id;
rb_control_frame_t *cfp = ec->cfp;
const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
@@ -297,15 +298,15 @@ vm_call_super(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_sp
id = me->def->original_id;
me = rb_callable_method_entry(klass, id);
+ v = add_empty_keyword(&argc, &argv, &kw_splat);
if (!me) {
- return method_missing(recv, id, argc, argv, MISSING_SUPER);
+ ret = method_missing(recv, id, argc, argv, MISSING_SUPER, kw_splat);
}
else {
- VALUE v = add_empty_keyword(&argc, &argv, &kw_splat);
- VALUE ret = rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
- rb_free_tmp_buffer(&v);
- return ret;
+ ret = rb_vm_call0(ec, recv, id, argc, argv, me, kw_splat);
}
+ rb_free_tmp_buffer(&v);
+ return ret;
}
VALUE
@@ -400,7 +401,7 @@ rb_call0(rb_execution_context_t *ec,
call_status = rb_method_call_status(ec, me, scope, self);
if (call_status != MISSING_NONE) {
- return method_missing(recv, mid, argc, argv, call_status);
+ return method_missing(recv, mid, argc, argv, call_status, kw_splat);
}
stack_check(ec);
return rb_vm_call0(ec, recv, mid, argc, argv, me, kw_splat);
@@ -830,7 +831,7 @@ vm_raise_method_missing(rb_execution_context_t *ec, int argc, const VALUE *argv,
}
static inline VALUE
-method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status)
+method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missing_reason call_status, int kw_splat)
{
VALUE *nargv, result, work, klass;
rb_execution_context_t *ec = GET_EC();
@@ -862,7 +863,7 @@ method_missing(VALUE obj, ID id, int argc, const VALUE *argv, enum method_missin
me = rb_callable_method_entry(klass, idMethodMissing);
if (!me || METHOD_ENTRY_BASIC(me)) goto missing;
vm_passed_block_handler_set(ec, block_handler);
- result = rb_vm_call0(ec, obj, idMethodMissing, argc, argv, me, VM_NO_KEYWORDS);
+ result = rb_vm_call0(ec, obj, idMethodMissing, argc, argv, me, kw_splat);
if (work) ALLOCV_END(work);
return result;
}