Skip to content

Commit 902984f

Browse files
committed
Fix warnings for Enumerable#inject
* Fixes #2859
1 parent 0c315d2 commit 902984f

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
}.should complain(/#{__FILE__}:#{__LINE__-1}: warning: given block not used/, verbose: true)
2929
end
3030

31+
it "does not warn when given a Symbol with $VERBOSE true" do
32+
-> {
33+
[1, 2].send(@method, 0, :+)
34+
[1, 2].send(@method, :+)
35+
EnumerableSpecs::Numerous.new(1, 2).send(@method, 0, :+)
36+
EnumerableSpecs::Numerous.new(1, 2).send(@method, :+)
37+
}.should_not complain(verbose: true)
38+
end
39+
3140
it "can take a symbol argument" do
3241
EnumerableSpecs::Numerous.new(10, 1, 2, 3).send(@method, :-).should == 4
3342
end

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ private static class State {
13501350
@Child private DispatchNode dispatch = DispatchNode.create(PUBLIC);
13511351
@Child private CallBlockNode yieldNode = CallBlockNode.create();
13521352

1353-
// Uses block
1353+
// Uses block and no Symbol
13541354

13551355
@Specialization(guards = { "isEmptyArray(array)", "wasProvided(initialOrSymbol)" })
13561356
protected Object injectEmptyArray(RubyArray array, Object initialOrSymbol, NotProvided symbol, RubyProc block) {
@@ -1380,7 +1380,7 @@ protected Object injectNoInitial(
13801380
return injectBlockHelper(array, block, stores.read(store, 0), 1, iteratorNode);
13811381
}
13821382

1383-
public Object injectBlockHelper(RubyArray array,
1383+
private Object injectBlockHelper(RubyArray array,
13841384
RubyProc block, Object initial, int start, ArrayEachIteratorNode iteratorNode) {
13851385
Object accumulator = initial;
13861386
State iterationState = new State(accumulator, block);
@@ -1397,7 +1397,7 @@ public void accept(RubyArray array, Object stateObject, Object element, int inde
13971397
state.accumulator = accumulator;
13981398
}
13991399

1400-
// Uses Symbol
1400+
// Uses Symbol and no block
14011401

14021402
@Specialization(guards = { "isEmptyArray(array)" })
14031403
protected Object injectSymbolEmptyArrayNoInitial(
@@ -1409,8 +1409,7 @@ protected Object injectSymbolEmptyArrayNoInitial(
14091409
guards = {
14101410
"isEmptyArray(array)",
14111411
"wasProvided(initialOrSymbol)" })
1412-
protected Object injectSymbolEmptyArray(
1413-
RubyArray array, Object initialOrSymbol, RubySymbol symbol, Object maybeBlock) {
1412+
protected Object injectSymbolEmptyArray(RubyArray array, Object initialOrSymbol, RubySymbol symbol, Nil block) {
14141413
return initialOrSymbol;
14151414
}
14161415

@@ -1442,17 +1441,12 @@ protected Object injectSymbolNoInitial(
14421441
"wasProvided(initialOrSymbol)" },
14431442
limit = "storageStrategyLimit()")
14441443
protected Object injectSymbolWithInitial(
1445-
VirtualFrame frame, RubyArray array, Object initialOrSymbol, RubySymbol symbol, Object block,
1444+
VirtualFrame frame, RubyArray array, Object initialOrSymbol, RubySymbol symbol, Nil block,
14461445
@Bind("array.getStore()") Object store,
14471446
@CachedLibrary("store") ArrayStoreLibrary stores,
14481447
@Cached IntValueProfile arraySizeProfile,
14491448
@Cached LoopConditionProfile loopProfile,
1450-
@Cached ToJavaStringNode toJavaString,
1451-
@Cached("new()") WarningNode warningNode) {
1452-
if (warningNode.shouldWarn()) {
1453-
final SourceSection sourceSection = getContext().getCallStack().getTopMostUserSourceSection();
1454-
warningNode.warningMessage(sourceSection, "given block not used");
1455-
}
1449+
@Cached ToJavaStringNode toJavaString) {
14561450
return injectSymbolHelper(
14571451
frame,
14581452
array,
@@ -1465,15 +1459,7 @@ protected Object injectSymbolWithInitial(
14651459
loopProfile);
14661460
}
14671461

1468-
// No Symbol or Block
1469-
1470-
@Specialization
1471-
protected Object injectNoSymbolNonEmptyArrayNoInitial(
1472-
RubyArray array, NotProvided initialOrSymbol, NotProvided symbol, Nil block) {
1473-
throw new RaiseException(getContext(), coreExceptions().argumentError("no block or symbol given", this));
1474-
}
1475-
1476-
public Object injectSymbolHelper(VirtualFrame frame, RubyArray array, String symbol,
1462+
private Object injectSymbolHelper(VirtualFrame frame, RubyArray array, String symbol,
14771463
ArrayStoreLibrary stores, Object store, Object initial, int start,
14781464
IntValueProfile arraySizeProfile, LoopConditionProfile loopProfile) {
14791465
Object accumulator = initial;

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,22 +510,23 @@ def sort_by
510510
# inject(initial_operand, symbol) -> object
511511
# inject {|memo, operand| ... } -> object
512512
# inject(initial_operand) {|memo, operand| ... } -> object
513-
def inject(initial=undefined, sym=undefined, &block)
514-
if Array === self
515-
return Primitive.array_inject(self, initial, sym, block)
516-
end
517-
513+
def inject(initial = undefined, sym = undefined, &block)
518514
# inject()
519-
if !block_given? && Primitive.undefined?(initial)
515+
if !block && Primitive.undefined?(initial)
520516
raise ArgumentError, 'no block or symbol given'
521517
end
522518

523519
# inject(initial_operand, symbol) { ... }
524-
if block_given? && !Primitive.undefined?(sym)
520+
if block && !Primitive.undefined?(sym)
525521
Primitive.warn_given_block_not_used
522+
block = nil
523+
end
524+
525+
if Primitive.object_kind_of?(self, ::Array)
526+
return Primitive.array_inject(self, initial, sym, block)
526527
end
527528

528-
if Primitive.undefined?(sym) && block_given?
529+
if Primitive.undefined?(sym) && block
529530
# Do the block version:
530531
# inject {|memo, operand| ... } -> object
531532
# inject(initial_operand) {|memo, operand| ... } -> object

0 commit comments

Comments
 (0)