Skip to content

Commit 6876cd3

Browse files
committed
Use Truffle::Type.try_convert for Integer.try_convert
* Fix Truffle::Type.try_convert message to match CRuby. * Spec the message for all .try_convert TypeErrors.
1 parent c08d7b3 commit 6876cd3

File tree

8 files changed

+13
-21
lines changed

8 files changed

+13
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Compatibility:
8383
* Modify `Struct#{inspect,to_s}` to match MRI when the struct is nested inside of an anonymous class or module (@st0012, @nirvdrum).
8484
* `Fiber.current` and `Fiber#transfer` are available without `require 'fiber'` like in CRuby 3.1 (#2733, @eregon).
8585
* Add `freeze` keyword argument to `Marshal.load` (#2733, @andrykonchin).
86+
* Add `Integer.try_convert` (#2733, @moste00, @eregon).
8687

8788
Performance:
8889

spec/ruby/core/array/try_convert_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
it "sends #to_ary to the argument and raises TypeError if it's not a kind of Array" do
4040
obj = mock("to_ary")
4141
obj.should_receive(:to_ary).and_return(Object.new)
42-
-> { Array.try_convert obj }.should raise_error(TypeError)
42+
-> { Array.try_convert obj }.should raise_error(TypeError, "can't convert MockObject to Array (MockObject#to_ary gives Object)")
4343
end
4444

4545
it "does not rescue exceptions raised by #to_ary" do

spec/ruby/core/hash/try_convert_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
it "sends #to_hash to the argument and raises TypeError if it's not a kind of Hash" do
4040
obj = mock("to_hash")
4141
obj.should_receive(:to_hash).and_return(Object.new)
42-
-> { Hash.try_convert obj }.should raise_error(TypeError)
42+
-> { Hash.try_convert obj }.should raise_error(TypeError, "can't convert MockObject to Hash (MockObject#to_hash gives Object)")
4343
end
4444

4545
it "does not rescue exceptions raised by #to_hash" do

spec/ruby/core/io/try_convert_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
it "raises a TypeError if the object does not return an IO from #to_io" do
3939
obj = mock("io")
4040
obj.should_receive(:to_io).and_return("io")
41-
-> { IO.try_convert(obj) }.should raise_error(TypeError)
41+
-> { IO.try_convert(obj) }.should raise_error(TypeError, "can't convert MockObject to IO (MockObject#to_io gives String)")
4242
end
4343

4444
it "propagates an exception raised by #to_io" do

spec/ruby/core/regexp/try_convert_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818
rex.should_receive(:to_regexp).and_return(/(p(a)t[e]rn)/)
1919
Regexp.try_convert(rex).should == /(p(a)t[e]rn)/
2020
end
21+
22+
it "raises a TypeError if the object does not return an Regexp from #to_regexp" do
23+
obj = mock("regexp")
24+
obj.should_receive(:to_regexp).and_return("string")
25+
-> { Regexp.try_convert(obj) }.should raise_error(TypeError, "can't convert MockObject to Regexp (MockObject#to_regexp gives String)")
26+
end
2127
end

spec/ruby/core/string/try_convert_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
it "sends #to_str to the argument and raises TypeError if it's not a kind of String" do
4040
obj = mock("to_str")
4141
obj.should_receive(:to_str).and_return(Object.new)
42-
-> { String.try_convert obj }.should raise_error(TypeError)
42+
-> { String.try_convert obj }.should raise_error(TypeError, "can't convert MockObject to String (MockObject#to_str gives Object)")
4343
end
4444

4545
it "does not rescue exceptions raised by #to_str" do

src/main/ruby/truffleruby/core/integer.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,7 @@ def zero?
333333
end
334334

335335
def self.try_convert(obj)
336-
unless obj.respond_to?(:to_int)
337-
return nil
338-
end
339-
340-
result = obj.to_int
341-
if Primitive.nil?(result) or Primitive.object_kind_of?(result, Integer)
342-
return result
343-
end
344-
345-
Truffle::Type.conversion_mismatch_into(obj, Integer, :to_int, result)
336+
Truffle::Type.try_convert(obj, Integer, :to_int)
346337
end
347338

348339
def self.sqrt(n)

src/main/ruby/truffleruby/core/type.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,6 @@ def self.conversion_mismatch(obj, cls, meth, res)
187187
raise TypeError, "can't convert #{oc} to #{cls} (#{oc}##{meth} gives #{Primitive.object_class(res)})"
188188
end
189189

190-
# Same as conversion mismatch, with a tiny difference in error message ("into" instead of "to")
191-
def self.conversion_mismatch_into(obj, cls, meth, res)
192-
oc = Primitive.object_class(obj)
193-
raise TypeError, "can't convert #{oc} into #{cls} (#{oc}##{meth} gives #{Primitive.object_class(res)})"
194-
end
195-
196190
def self.fits_into_int?(val)
197191
Integer === val && Primitive.integer_fits_into_int(val)
198192
end
@@ -373,7 +367,7 @@ def self.execute_try_convert(obj, cls, meth)
373367
if Primitive.nil?(ret) || Primitive.object_kind_of?(ret, cls)
374368
ret
375369
else
376-
raise TypeError, "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{Primitive.object_class(ret)})"
370+
conversion_mismatch(obj, cls, meth, ret)
377371
end
378372
end
379373

0 commit comments

Comments
 (0)