Skip to content

Commit ae7e102

Browse files
committed
[GR-18163] Update Enumerable#inject to raise a LocalJumpError if no block or symbol are given
PullRequest: truffleruby/3234
2 parents b8cf3c6 + 9fc197d commit ae7e102

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Compatibility:
6868
* Update `Dir.foreach` to accept an `encoding` parameter (#2627, @bjfish).
6969
* Update `IO.readlines` to ignore negative limit parameters (#2625 , @bjfish).
7070
* Update `Math.sqrt` to raise a `Math::DomainError` for negative numbers (#2621, @bjfish).
71+
* Update `Enumerable#inject` to raise a `LocalJumpError` if no block or symbol are given (#2626, @bjfish).
7172

7273
Performance:
7374

spec/ruby/core/enumerable/shared/inject.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@
6666
it "returns nil when fails(legacy rubycon)" do
6767
EnumerableSpecs::EachDefiner.new().send(@method) {|acc,x| 999 }.should == nil
6868
end
69+
70+
ruby_bug '#18635', ''...'3.2' do
71+
it "raises an ArgumentError when no parameters or block is given" do
72+
-> { [1,2].send(@method) }.should raise_error(ArgumentError)
73+
-> { {one: 1, two: 2}.send(@method) }.should raise_error(ArgumentError)
74+
end
75+
end
6976
end

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,12 @@ protected Object injectSymbolNoInitial(
14541454
loopProfile);
14551455
}
14561456

1457+
@Specialization
1458+
protected Object injectNoSymbolNonEmptyArrayNoInitial(
1459+
RubyArray array, NotProvided initialOrSymbol, NotProvided symbol, Nil block) {
1460+
throw new RaiseException(getContext(), coreExceptions().argumentError("no block or symbol given", this));
1461+
}
1462+
14571463
public Object injectSymbolHelper(VirtualFrame frame, RubyArray array, String symbol,
14581464
ArrayStoreLibrary stores, Object store, Object initial, int start,
14591465
IntValueProfile arraySizeProfile, LoopConditionProfile loopProfile) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def inject(initial=undefined, sym=undefined, &block)
492492

493493
if !block_given? or !Primitive.undefined?(sym)
494494
if Primitive.undefined?(sym)
495+
raise ArgumentError, 'no block or symbol given' if Primitive.undefined?(initial)
495496
sym = initial
496497
initial = undefined
497498
end

0 commit comments

Comments
 (0)