Allow anonymous memberless Struct

Previously, named memberless Structs were allowed, but anonymous
memberless Structs were not.

Fixes [Bug #19416]
This commit is contained in:
Jeremy Evans 2023-03-23 13:44:04 -07:00
parent 73fc81199d
commit f8e7048348
Notes: git 2023-04-24 14:37:49 +00:00
3 changed files with 18 additions and 8 deletions

View File

@ -78,6 +78,20 @@ describe "Struct.new" do
end
end
ruby_version_is ""..."3.3" do
it "raises ArgumentError if not provided any arguments" do
-> { Struct.new }.should raise_error(ArgumentError)
end
end
ruby_version_is "3.3" do
it "works when not provided any arguments" do
c = Struct.new
c.should be_kind_of(Class)
c.superclass.should == Struct
end
end
it "raises ArgumentError when there is a duplicate member" do
-> { Struct.new(:foo, :foo) }.should raise_error(ArgumentError, "duplicate member: foo")
end

View File

@ -637,17 +637,14 @@ rb_struct_define_under(VALUE outer, const char *name, ...)
static VALUE
rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
{
VALUE name, rest, keyword_init = Qnil;
VALUE name = Qnil, rest, keyword_init = Qnil;
long i;
VALUE st;
VALUE opt;
argc = rb_scan_args(argc, argv, "1*:", NULL, NULL, &opt);
name = argv[0];
if (SYMBOL_P(name)) {
name = Qnil;
}
else {
argc = rb_scan_args(argc, argv, "0*:", NULL, &opt);
if (argc >= 1 && !SYMBOL_P(argv[0])) {
name = argv[0];
--argc;
++argv;
}

View File

@ -65,6 +65,5 @@ class TestArity < Test::Unit::TestCase
assert_arity(%w[1 2]) { "".sub!(//) }
assert_arity(%w[0 1..2]) { "".sub!{} }
assert_arity(%w[0 1+]) { exec }
assert_arity(%w[0 1+]) { Struct.new }
end
end