Skip to content

Commit 9fc197d

Browse files
committed
Update Enumerable#inject to raise a LocalJumpError if no block or symbol are given
1 parent 4c6e74e commit 9fc197d

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
@@ -44,6 +44,7 @@ Compatibility:
4444
* Update `String#split` to raise `TypeError` when false is given (#2606, @bjfish).
4545
* Update `String#lstrip!` to remove leading null characters (#2607, @bjfish).
4646
* Update `File.utime` to return the number of file names in the arguments (#2616, @bjfish).
47+
* Update `Enumerable#inject` to raise a `LocalJumpError` if no block or symbol are given (#2626, @bjfish).
4748

4849
Performance:
4950

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
@@ -1452,6 +1452,12 @@ protected Object injectSymbolNoInitial(
14521452
loopProfile);
14531453
}
14541454

1455+
@Specialization
1456+
protected Object injectNoSymbolNonEmptyArrayNoInitial(
1457+
RubyArray array, NotProvided initialOrSymbol, NotProvided symbol, Nil block) {
1458+
throw new RaiseException(getContext(), coreExceptions().argumentError("no block or symbol given", this));
1459+
}
1460+
14551461
public Object injectSymbolHelper(VirtualFrame frame, RubyArray array, String symbol,
14561462
ArrayStoreLibrary stores, Object store, Object initial, int start,
14571463
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)