Skip to content

Commit

Permalink
Use Truffle::Type.try_convert for Integer.try_convert
Browse files Browse the repository at this point in the history
* Fix Truffle::Type.try_convert message to match CRuby.
* Spec the message for all .try_convert TypeErrors.
  • Loading branch information
eregon committed Mar 7, 2023
1 parent c08d7b3 commit 6876cd3
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Compatibility:
* Modify `Struct#{inspect,to_s}` to match MRI when the struct is nested inside of an anonymous class or module (@st0012, @nirvdrum).
* `Fiber.current` and `Fiber#transfer` are available without `require 'fiber'` like in CRuby 3.1 (#2733, @eregon).
* Add `freeze` keyword argument to `Marshal.load` (#2733, @andrykonchin).
* Add `Integer.try_convert` (#2733, @moste00, @eregon).

Performance:

Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/array/try_convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
it "sends #to_ary to the argument and raises TypeError if it's not a kind of Array" do
obj = mock("to_ary")
obj.should_receive(:to_ary).and_return(Object.new)
-> { Array.try_convert obj }.should raise_error(TypeError)
-> { Array.try_convert obj }.should raise_error(TypeError, "can't convert MockObject to Array (MockObject#to_ary gives Object)")
end

it "does not rescue exceptions raised by #to_ary" do
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/hash/try_convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
it "sends #to_hash to the argument and raises TypeError if it's not a kind of Hash" do
obj = mock("to_hash")
obj.should_receive(:to_hash).and_return(Object.new)
-> { Hash.try_convert obj }.should raise_error(TypeError)
-> { Hash.try_convert obj }.should raise_error(TypeError, "can't convert MockObject to Hash (MockObject#to_hash gives Object)")
end

it "does not rescue exceptions raised by #to_hash" do
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/io/try_convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
it "raises a TypeError if the object does not return an IO from #to_io" do
obj = mock("io")
obj.should_receive(:to_io).and_return("io")
-> { IO.try_convert(obj) }.should raise_error(TypeError)
-> { IO.try_convert(obj) }.should raise_error(TypeError, "can't convert MockObject to IO (MockObject#to_io gives String)")
end

it "propagates an exception raised by #to_io" do
Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/core/regexp/try_convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
rex.should_receive(:to_regexp).and_return(/(p(a)t[e]rn)/)
Regexp.try_convert(rex).should == /(p(a)t[e]rn)/
end

it "raises a TypeError if the object does not return an Regexp from #to_regexp" do
obj = mock("regexp")
obj.should_receive(:to_regexp).and_return("string")
-> { Regexp.try_convert(obj) }.should raise_error(TypeError, "can't convert MockObject to Regexp (MockObject#to_regexp gives String)")
end
end
2 changes: 1 addition & 1 deletion spec/ruby/core/string/try_convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
it "sends #to_str to the argument and raises TypeError if it's not a kind of String" do
obj = mock("to_str")
obj.should_receive(:to_str).and_return(Object.new)
-> { String.try_convert obj }.should raise_error(TypeError)
-> { String.try_convert obj }.should raise_error(TypeError, "can't convert MockObject to String (MockObject#to_str gives Object)")
end

it "does not rescue exceptions raised by #to_str" do
Expand Down
11 changes: 1 addition & 10 deletions src/main/ruby/truffleruby/core/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,7 @@ def zero?
end

def self.try_convert(obj)
unless obj.respond_to?(:to_int)
return nil
end

result = obj.to_int
if Primitive.nil?(result) or Primitive.object_kind_of?(result, Integer)
return result
end

Truffle::Type.conversion_mismatch_into(obj, Integer, :to_int, result)
Truffle::Type.try_convert(obj, Integer, :to_int)
end

def self.sqrt(n)
Expand Down
8 changes: 1 addition & 7 deletions src/main/ruby/truffleruby/core/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,6 @@ def self.conversion_mismatch(obj, cls, meth, res)
raise TypeError, "can't convert #{oc} to #{cls} (#{oc}##{meth} gives #{Primitive.object_class(res)})"
end

# Same as conversion mismatch, with a tiny difference in error message ("into" instead of "to")
def self.conversion_mismatch_into(obj, cls, meth, res)
oc = Primitive.object_class(obj)
raise TypeError, "can't convert #{oc} into #{cls} (#{oc}##{meth} gives #{Primitive.object_class(res)})"
end

def self.fits_into_int?(val)
Integer === val && Primitive.integer_fits_into_int(val)
end
Expand Down Expand Up @@ -373,7 +367,7 @@ def self.execute_try_convert(obj, cls, meth)
if Primitive.nil?(ret) || Primitive.object_kind_of?(ret, cls)
ret
else
raise TypeError, "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{Primitive.object_class(ret)})"
conversion_mismatch(obj, cls, meth, ret)
end
end

Expand Down

0 comments on commit 6876cd3

Please sign in to comment.