Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arity for {|a,| } was -2 and should have been 1 #8129

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyHash.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.