Skip to content

Commit

Permalink
[GR-18163] Update Enumerable#inject to raise a LocalJumpError if no b…
Browse files Browse the repository at this point in the history
…lock or symbol are given

PullRequest: truffleruby/3234
  • Loading branch information
bjfish committed Mar 25, 2022
2 parents b8cf3c6 + 9fc197d commit ae7e102
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Compatibility:
* Update `Dir.foreach` to accept an `encoding` parameter (#2627, @bjfish).
* Update `IO.readlines` to ignore negative limit parameters (#2625 , @bjfish).
* Update `Math.sqrt` to raise a `Math::DomainError` for negative numbers (#2621, @bjfish).
* Update `Enumerable#inject` to raise a `LocalJumpError` if no block or symbol are given (#2626, @bjfish).

Performance:

Expand Down
7 changes: 7 additions & 0 deletions spec/ruby/core/enumerable/shared/inject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@
it "returns nil when fails(legacy rubycon)" do
EnumerableSpecs::EachDefiner.new().send(@method) {|acc,x| 999 }.should == nil
end
ruby_bug '#18635', ''...'3.2' do
it "raises an ArgumentError when no parameters or block is given" do
-> { [1,2].send(@method) }.should raise_error(ArgumentError)
-> { {one: 1, two: 2}.send(@method) }.should raise_error(ArgumentError)
end
end
end
6 changes: 6 additions & 0 deletions src/main/java/org/truffleruby/core/array/ArrayNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,12 @@ protected Object injectSymbolNoInitial(
loopProfile);
}

@Specialization
protected Object injectNoSymbolNonEmptyArrayNoInitial(
RubyArray array, NotProvided initialOrSymbol, NotProvided symbol, Nil block) {
throw new RaiseException(getContext(), coreExceptions().argumentError("no block or symbol given", this));
}

public Object injectSymbolHelper(VirtualFrame frame, RubyArray array, String symbol,
ArrayStoreLibrary stores, Object store, Object initial, int start,
IntValueProfile arraySizeProfile, LoopConditionProfile loopProfile) {
Expand Down
1 change: 1 addition & 0 deletions src/main/ruby/truffleruby/core/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ def inject(initial=undefined, sym=undefined, &block)

if !block_given? or !Primitive.undefined?(sym)
if Primitive.undefined?(sym)
raise ArgumentError, 'no block or symbol given' if Primitive.undefined?(initial)
sym = initial
initial = undefined
end
Expand Down

0 comments on commit ae7e102

Please sign in to comment.