Skip to content

Commit

Permalink
Merge pull request #8129 from enebo/arity_fix
Browse files Browse the repository at this point in the history
arity for {|a,| } was -2 and should have been 1
  • Loading branch information
enebo committed May 1, 2024
2 parents a0507b7 + 16be728 commit 26ad84c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyHash.java
Expand Up @@ -1624,7 +1624,7 @@ public RubyHash each_pairCommon(final ThreadContext context, final Block block)
public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyObject value, int index, Block block) {
if (block.type == Block.Type.LAMBDA) {
block.call(context, context.runtime.newArray(key, value));
} else if (block.getSignature().arityValue() > 1) {
} else if (block.getSignature().isSpreadable()) {
block.yieldSpecific(context, key, value);
} else {
block.yield(context, context.runtime.newArray(key, value));
Expand Down
Expand Up @@ -513,7 +513,9 @@ public static IRubyObject[] convertValueIntoArgArray(ThreadContext context, IRub
new IRubyObject[] { value };
case 0:
case 1:
return new IRubyObject[] { value };
return signature.rest() == org.jruby.runtime.Signature.Rest.ANON ?
IRBlockBody.toAry(context, value) :
new IRubyObject[] { value };
}

return IRBlockBody.toAry(context, value);
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/org/jruby/runtime/IRBlockBody.java
Expand Up @@ -102,14 +102,13 @@ public IRubyObject yieldSpecific(ThreadContext context, Block block, IRubyObject
}

private IRubyObject yieldSpecificMultiArgsCommon(ThreadContext context, Block block, IRubyObject... args) {
int blockArity = signature.arityValue();
if (blockArity == 1) {
if (signature.isOneArgument()) {
args = new IRubyObject[] { RubyArray.newArrayMayCopy(context.runtime, args) };
}

if (canCallDirect()) return yieldDirect(context, block, args, null);

if (blockArity == 0) {
if (signature.arityValue() == 0) {
args = IRubyObject.NULL_ARRAY; // discard args
}
if (block.type == Block.Type.LAMBDA) signature.checkArity(context.runtime, args);
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/jruby/runtime/Signature.java
Expand Up @@ -85,7 +85,7 @@ public boolean restKwargs() {
* Are there an exact (fixed) number of parameters to this signature?
*/
public boolean isFixed() {
return arityValue() >= 0;
return arityValue() >= 0 && rest != Rest.ANON;
}

/**
Expand Down Expand Up @@ -135,8 +135,9 @@ public int calculateArityValue() {
int oneForKeywords = requiredKwargs > 0 ? 1 : 0;
int fixedValue = pre() + post() + oneForKeywords;
boolean hasOptionalKeywords = kwargs - requiredKwargs > 0;
boolean optionalFromRest = rest() != Rest.NONE && rest != Rest.ANON;

if (opt() > 0 || rest() != Rest.NONE || (hasOptionalKeywords || restKwargs()) && oneForKeywords == 0) {
if (opt() > 0 || optionalFromRest || (hasOptionalKeywords || restKwargs()) && oneForKeywords == 0) {
return -1 * (fixedValue + 1);
}

Expand All @@ -153,7 +154,7 @@ public int arityValue() {
* @return true if the signature expects multiple args
*/
public boolean isSpreadable() {
return arityValue < -1 || arityValue > 1 || (opt > 0 && !restKwargs());
return arityValue < -1 || arityValue > 1 || (opt > 0 && !restKwargs()) || rest == Rest.ANON;
}


Expand Down
2 changes: 0 additions & 2 deletions spec/tags/ruby/core/proc/arity_tags.txt

This file was deleted.

0 comments on commit 26ad84c

Please sign in to comment.