diff options
author | Jeremy Evans <code@jeremyevans.net> | 2023-03-23 13:44:04 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2023-04-24 07:37:20 -0700 |
commit | f8e7048348d022814736d0a7e49f2f2494db6a2f (patch) | |
tree | eecca5b3e7fa827a81d71fe9cdfc6adc41b9afc9 | |
parent | 73fc81199de5e567e38f7ea9067260eb6866cbf9 (diff) | |
download | ruby-f8e7048348d022814736d0a7e49f2f2494db6a2f.tar.gz |
Allow anonymous memberless Struct
Previously, named memberless Structs were allowed, but anonymous
memberless Structs were not.
Fixes [Bug #19416]
-rw-r--r-- | spec/ruby/core/struct/new_spec.rb | 14 | ||||
-rw-r--r-- | struct.c | 11 | ||||
-rw-r--r-- | test/ruby/test_arity.rb | 1 |
3 files changed, 18 insertions, 8 deletions
diff --git a/spec/ruby/core/struct/new_spec.rb b/spec/ruby/core/struct/new_spec.rb index 7b4a4f7980..4aeaa066e1 100644 --- a/spec/ruby/core/struct/new_spec.rb +++ b/spec/ruby/core/struct/new_spec.rb @@ -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 @@ -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; } diff --git a/test/ruby/test_arity.rb b/test/ruby/test_arity.rb index d26338e0aa..bd26d5f0f5 100644 --- a/test/ruby/test_arity.rb +++ b/test/ruby/test_arity.rb @@ -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 |