From 76375a7f3d80ebf22d5f7782a152dcd2bc04264b Mon Sep 17 00:00:00 2001 From: Simon LeVasseur Date: Thu, 14 Mar 2024 19:34:15 +0000 Subject: [PATCH 1/3] Implement no args Struct.new constructor in Ruby 3.3 --- spec/truffleruby.next-specs | 1 + src/main/ruby/truffleruby/core/struct.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/truffleruby.next-specs b/spec/truffleruby.next-specs index bbf78c265896..8d482b728695 100644 --- a/spec/truffleruby.next-specs +++ b/spec/truffleruby.next-specs @@ -9,6 +9,7 @@ # Use spec/ruby/core/nil/nil_spec.rb as a dummy file to avoid being empty (what causes mspec to error) spec/ruby/core/nil/nil_spec.rb +spec/ruby/core/struct/new_spec.rb spec/ruby/core/warning/element_reference_spec.rb spec/ruby/core/warning/element_set_spec.rb diff --git a/src/main/ruby/truffleruby/core/struct.rb b/src/main/ruby/truffleruby/core/struct.rb index 19f8447620cb..fc3f5eabefd9 100644 --- a/src/main/ruby/truffleruby/core/struct.rb +++ b/src/main/ruby/truffleruby/core/struct.rb @@ -33,7 +33,7 @@ class << self alias_method :subclass_new, :new end - def self.new(klass_name, *attrs, keyword_init: nil, &block) + def self.new(klass_name = nil, *attrs, keyword_init: nil, &block) if klass_name if Primitive.is_a?(klass_name, Symbol) # Truffle: added to avoid exception and match MRI attrs.unshift klass_name From 0435c76ee5df5ebce309893ff8e3efc7014624c7 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Fri, 19 Apr 2024 13:24:27 +0300 Subject: [PATCH 2/3] Refactor Struct.new method and simplify handling a class name parameter --- src/main/ruby/truffleruby/core/struct.rb | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main/ruby/truffleruby/core/struct.rb b/src/main/ruby/truffleruby/core/struct.rb index fc3f5eabefd9..6a930a38071a 100644 --- a/src/main/ruby/truffleruby/core/struct.rb +++ b/src/main/ruby/truffleruby/core/struct.rb @@ -33,19 +33,14 @@ class << self alias_method :subclass_new, :new end - def self.new(klass_name = nil, *attrs, keyword_init: nil, &block) - if klass_name - if Primitive.is_a?(klass_name, Symbol) # Truffle: added to avoid exception and match MRI - attrs.unshift klass_name - klass_name = nil - else - begin - klass_name = StringValue klass_name - rescue TypeError - attrs.unshift klass_name - klass_name = nil - end - end + def self.new(*attrs, keyword_init: nil, &block) + klass_name = nil + + first = attrs[0] + if attrs.size >= 1 && !Primitive.is_a?(first, Symbol) + # nil check because Struct.new(nil, :foo) is valid + klass_name = StringValue(first) unless Primitive.nil?(first) + attrs.shift end attrs = attrs.map { |a| Truffle::Type.symbol_or_string_to_symbol(a) } From 9f9e1d6106ff68922c1ffa9a726ff378adbd47cd Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Fri, 19 Apr 2024 14:04:34 +0300 Subject: [PATCH 3/3] Tag a Struct.new failed test case --- spec/tags/core/struct/new_tags.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 spec/tags/core/struct/new_tags.txt diff --git a/spec/tags/core/struct/new_tags.txt b/spec/tags/core/struct/new_tags.txt new file mode 100644 index 000000000000..f13356c24379 --- /dev/null +++ b/spec/tags/core/struct/new_tags.txt @@ -0,0 +1 @@ +fails:Struct.new raises ArgumentError if not provided any arguments