From 431132f037aecc8c9bc783fea257db653c4f8cb0 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 21 Oct 2019 12:20:19 +0900 Subject: [PATCH] Pass the called keyword arguments if `keyword_init` --- struct.c | 21 +++++++++++++++------ test/ruby/test_struct.rb | 4 ++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/struct.c b/struct.c index b2935747ed..8294e9fa21 100644 --- a/struct.c +++ b/struct.c @@ -311,15 +311,24 @@ rb_struct_s_inspect(VALUE klass) } static VALUE -setup_struct(VALUE nstr, VALUE members) +struct_new_kw(int argc, const VALUE *argv, VALUE klass) +{ + return rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS); +} + +static VALUE +setup_struct(VALUE nstr, VALUE members, int keyword_init) { long i, len; + VALUE (*new_func)(int, const VALUE *, VALUE) = rb_class_new_instance; + + if (keyword_init) new_func = struct_new_kw; members = struct_set_members(nstr, members); rb_define_alloc_func(nstr, struct_alloc); - rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1); - rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1); + rb_define_singleton_method(nstr, "new", new_func, -1); + rb_define_singleton_method(nstr, "[]", new_func, -1); rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0); rb_define_singleton_method(nstr, "inspect", rb_struct_s_inspect, 0); len = RARRAY_LEN(members); @@ -434,7 +443,7 @@ rb_struct_define(const char *name, ...) if (!name) st = anonymous_struct(rb_cStruct); else st = new_struct(rb_str_new2(name), rb_cStruct); - return setup_struct(st, ary); + return setup_struct(st, ary, 0); } VALUE @@ -447,7 +456,7 @@ rb_struct_define_under(VALUE outer, const char *name, ...) ary = struct_make_members_list(ar); va_end(ar); - return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary); + return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary, 0); } /* @@ -566,7 +575,7 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass) else { st = new_struct(name, klass); } - setup_struct(st, rest); + setup_struct(st, rest, (int)keyword_init); rb_ivar_set(st, id_keyword_init, keyword_init); if (rb_block_given_p()) { rb_mod_module_eval(0, 0, st); diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index 6ba28153e9..ec7728b197 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -112,6 +112,10 @@ module TestStruct assert_equal @Struct::KeywordInitTrue.new(a: 1, b: 2).values, @Struct::KeywordInitFalse.new(1, 2).values assert_equal "#{@Struct}::KeywordInitFalse", @Struct::KeywordInitFalse.inspect assert_equal "#{@Struct}::KeywordInitTrue(keyword_init: true)", @Struct::KeywordInitTrue.inspect + k = Class.new(@Struct::KeywordInitFalse) {def initialize(**) end} + assert_warn(/The last argument is used as the keyword parameter/) {k.new(a: 1, b: 2)} + k = Class.new(@Struct::KeywordInitTrue) {def initialize(**) end} + assert_warn('') {k.new(a: 1, b: 2)} @Struct.instance_eval do remove_const(:KeywordInitTrue)