From 4fe8f1cc5430d9932db65ffa55fe9a373537555a Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Wed, 18 Dec 2024 16:03:23 +0200 Subject: [PATCH] Fix NoMethodError#message when receiver is an instance of an anonymous class --- spec/ruby/core/exception/no_method_error_spec.rb | 11 +++++++++++ spec/tags/core/exception/no_method_error_tags.txt | 2 -- .../truffleruby/core/truffle/exception_operations.rb | 11 ++++------- 3 files changed, 15 insertions(+), 9 deletions(-) delete mode 100644 spec/tags/core/exception/no_method_error_tags.txt diff --git a/spec/ruby/core/exception/no_method_error_spec.rb b/spec/ruby/core/exception/no_method_error_spec.rb index 78fafcaa28f3..9fbaa901295a 100644 --- a/spec/ruby/core/exception/no_method_error_spec.rb +++ b/spec/ruby/core/exception/no_method_error_spec.rb @@ -166,6 +166,17 @@ def inspect end end + it "uses class' string representation when receiver is a singleton class" do + obj = Object.new + singleton_class = obj.singleton_class + + begin + singleton_class.foo + rescue NoMethodError => error + error.message.should =~ /\Aundefined method [`']foo' for class #>\Z/ + end + end + it "uses #name when receiver is a module" do begin NoMethodErrorSpecs::AModule.foo diff --git a/spec/tags/core/exception/no_method_error_tags.txt b/spec/tags/core/exception/no_method_error_tags.txt deleted file mode 100644 index 0a1d27b5d5d4..000000000000 --- a/spec/tags/core/exception/no_method_error_tags.txt +++ /dev/null @@ -1,2 +0,0 @@ -fails:NoMethodError#message uses class string representation when receiver is an instance of anonymous class -fails:NoMethodError#message does not call #inspect when calling Exception#message diff --git a/src/main/ruby/truffleruby/core/truffle/exception_operations.rb b/src/main/ruby/truffleruby/core/truffle/exception_operations.rb index 8a4513f1769d..41c78b492702 100644 --- a/src/main/ruby/truffleruby/core/truffle/exception_operations.rb +++ b/src/main/ruby/truffleruby/core/truffle/exception_operations.rb @@ -79,18 +79,15 @@ def self.receiver_string(receiver) when NilClass, TrueClass, FalseClass Truffle::Type.rb_inspect(receiver) when Class - unless receiver.singleton_class? - "class #{receiver}" - end - # otherwise fall through to rb_any_to_s + "class #{receiver}" when Module "module #{receiver}" else klass = Primitive.metaclass(receiver) - if klass.name - "an instance of #{klass.name}" + unless klass.singleton_class? + "an instance of #{klass}" end - # else fall through to rb_any_to_s + # otherwise fall through to rb_any_to_s end rescue Exception # rubocop:disable Lint/RescueException nil