Skip to content

Commit

Permalink
[GR-18163] Raise TypeError instead of ArgumentError when Array#fill's…
Browse files Browse the repository at this point in the history
… 3d argument length is not Number

PullRequest: truffleruby/3380
  • Loading branch information
andrykonchin committed Jun 20, 2022
2 parents d1f8516 + 01b6e52 commit 4693f7b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Bug fixes:

Compatibility:

* Fix `Array#fill` to raise `TypeError` instead of `ArgumentError` when the length argument is not numeric (#2652, @andrykonchin).

Performance:

Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/core/array/fill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@
-> { [].fill('a', obj) }.should raise_error(TypeError)
end

it "raises a TypeError when the length is not numeric" do
-> { [1, 2, 3].fill("x", 1, "foo") }.should raise_error(TypeError, /no implicit conversion of String into Integer/)
-> { [1, 2, 3].fill("x", 1, :"foo") }.should raise_error(TypeError, /no implicit conversion of Symbol into Integer/)
-> { [1, 2, 3].fill("x", 1, Object.new) }.should raise_error(TypeError, /no implicit conversion of Object into Integer/)
end

not_supported_on :opal do
it "raises an ArgumentError or RangeError for too-large sizes" do
error_types = [RangeError, ArgumentError]
Expand Down
7 changes: 1 addition & 6 deletions src/main/ruby/truffleruby/core/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,7 @@ def fetch(idx, default=undefined)
left = 0 if left < 0

if !Primitive.undefined?(length) and length
begin
right = Primitive.rb_num2int length
rescue TypeError
raise ArgumentError, 'second argument must be an Integer'
end

right = Primitive.rb_num2int length
return self if right == 0
right += left
else
Expand Down

0 comments on commit 4693f7b

Please sign in to comment.