From 9fc197d79d9cef732a244224aabdcd5c47f0e2ca Mon Sep 17 00:00:00 2001 From: Brandon Fish Date: Tue, 15 Mar 2022 09:22:16 -0500 Subject: [PATCH] Update Enumerable#inject to raise a LocalJumpError if no block or symbol are given --- CHANGELOG.md | 1 + spec/ruby/core/enumerable/shared/inject.rb | 7 +++++++ src/main/java/org/truffleruby/core/array/ArrayNodes.java | 6 ++++++ src/main/ruby/truffleruby/core/enumerable.rb | 1 + 4 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d66978f79a1f..b8f9115adc04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Compatibility: * Update `String#split` to raise `TypeError` when false is given (#2606, @bjfish). * Update `String#lstrip!` to remove leading null characters (#2607, @bjfish). * Update `File.utime` to return the number of file names in the arguments (#2616, @bjfish). +* Update `Enumerable#inject` to raise a `LocalJumpError` if no block or symbol are given (#2626, @bjfish). Performance: diff --git a/spec/ruby/core/enumerable/shared/inject.rb b/spec/ruby/core/enumerable/shared/inject.rb index 12e0665dda5e..e59b82b02e8c 100644 --- a/spec/ruby/core/enumerable/shared/inject.rb +++ b/spec/ruby/core/enumerable/shared/inject.rb @@ -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 diff --git a/src/main/java/org/truffleruby/core/array/ArrayNodes.java b/src/main/java/org/truffleruby/core/array/ArrayNodes.java index 7871d8443075..8b41c62700b2 100644 --- a/src/main/java/org/truffleruby/core/array/ArrayNodes.java +++ b/src/main/java/org/truffleruby/core/array/ArrayNodes.java @@ -1452,6 +1452,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) { diff --git a/src/main/ruby/truffleruby/core/enumerable.rb b/src/main/ruby/truffleruby/core/enumerable.rb index fa95f6c03180..ac6d3afd8de1 100644 --- a/src/main/ruby/truffleruby/core/enumerable.rb +++ b/src/main/ruby/truffleruby/core/enumerable.rb @@ -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