A positional Hash is not keyword arguments [Bug #18632]

This commit is contained in:
Nobuyoshi Nakada 2022-03-17 18:54:49 +09:00
parent e9553a8cac
commit e660b934b9
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6
3 changed files with 14 additions and 8 deletions

View File

@ -60,10 +60,15 @@ describe "Struct.new" do
-> { Struct.new(:animal, nil) }.should raise_error(TypeError)
-> { Struct.new(:animal, true) }.should raise_error(TypeError)
-> { Struct.new(:animal, ['chris', 'evan']) }.should raise_error(TypeError)
ruby_version_is "3.2" do
-> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(TypeError)
end
end
it "raises a ArgumentError if passed a Hash with an unknown key" do
-> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(ArgumentError)
ruby_version_is ""..."3.2" do
it "raises a ArgumentError if passed a Hash with an unknown key" do
-> { Struct.new(:animal, { name: 'chris' }) }.should raise_error(ArgumentError)
end
end
it "raises ArgumentError when there is a duplicate member" do

View File

@ -576,8 +576,9 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
long i;
VALUE st;
st_table *tbl;
VALUE opt;
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
argc = rb_scan_args(argc, argv, "1*:", NULL, NULL, &opt);
name = argv[0];
if (SYMBOL_P(name)) {
name = Qnil;
@ -587,20 +588,19 @@ rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
++argv;
}
if (RB_TYPE_P(argv[argc-1], T_HASH)) {
if (!NIL_P(opt)) {
static ID keyword_ids[1];
if (!keyword_ids[0]) {
keyword_ids[0] = rb_intern("keyword_init");
}
rb_get_kwargs(argv[argc-1], keyword_ids, 0, 1, &keyword_init);
rb_get_kwargs(opt, keyword_ids, 0, 1, &keyword_init);
if (keyword_init == Qundef) {
keyword_init = Qnil;
}
else if (RTEST(keyword_init)) {
keyword_init = Qtrue;
}
--argc;
}
rest = rb_ident_hash_new();

View File

@ -100,8 +100,9 @@ module TestStruct
assert_equal([:utime, :stime, :cutime, :cstime], Process.times.members)
end
def test_struct_new_with_empty_hash
assert_equal({:a=>1}, Struct.new(:a, {}).new({:a=>1}).a)
def test_struct_new_with_hash
assert_raise_with_message(TypeError, /not a symbol/) {Struct.new(:a, {})}
assert_raise_with_message(TypeError, /not a symbol/) {Struct.new(:a, {name: "b"})}
end
def test_struct_new_with_keyword_init