pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/commit/83d33f8763f085d877e857c8b724d29b63e0d32a

" /> improved error message for "wrong number of arguments" · jruby/jruby@83d33f8 · GitHub
Skip to content

Commit 83d33f8

Browse files
committed
improved error message for "wrong number of arguments"
1 parent b004861 commit 83d33f8

10 files changed

Lines changed: 59 additions & 35 deletions

File tree

core/src/main/java/org/jruby/Ruby.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
import static org.jruby.util.RubyStringBuilder.str;
213213
import static org.jruby.util.RubyStringBuilder.ids;
214214
import static org.jruby.util.RubyStringBuilder.types;
215+
import static org.jruby.runtime.Arity.UNLIMITED_ARGUMENTS;
215216

216217
/**
217218
* The Ruby object represents the top-level of a JRuby "instance" in a given VM.
@@ -3611,11 +3612,31 @@ public RaiseException newArgumentError(String message) {
36113612
}
36123613

36133614
public RaiseException newArgumentError(int got, int expected) {
3614-
return newRaiseException(getArgumentError(), "wrong number of arguments (" + got + " for " + expected + ")");
3615+
return newArgumentError(got, expected, expected);
3616+
}
3617+
3618+
public RaiseException newArgumentError(int got, int min, int max) {
3619+
if (min == max) {
3620+
return newRaiseException(getArgumentError(), "wrong number of arguments (given " + got + ", expected " + min + ")");
3621+
} else if (max == UNLIMITED_ARGUMENTS) {
3622+
return newRaiseException(getArgumentError(), "wrong number of arguments (given " + got + ", expected " + min + "+)");
3623+
} else {
3624+
return newRaiseException(getArgumentError(), "wrong number of arguments (given " + got + ", expected " + min + ".." + max + ")");
3625+
}
36153626
}
36163627

36173628
public RaiseException newArgumentError(String name, int got, int expected) {
3618-
return newRaiseException(getArgumentError(), str(this, "wrong number of arguments calling `", ids(this, name), ("` (" + got + " for " + expected + ")")));
3629+
return newArgumentError(name, got, expected, expected);
3630+
}
3631+
3632+
public RaiseException newArgumentError(String name, int got, int min, int max) {
3633+
if (min == max) {
3634+
return newRaiseException(getArgumentError(), str(this, "wrong number of arguments calling `", ids(this, name), ("` (given " + got + ", expected " + min + ")")));
3635+
} else if (max == UNLIMITED_ARGUMENTS) {
3636+
return newRaiseException(getArgumentError(), str(this, "wrong number of arguments calling `", ids(this, name), ("` (given " + got + ", expected " + min + "+)")));
3637+
} else {
3638+
return newRaiseException(getArgumentError(), str(this, "wrong number of arguments calling `", ids(this, name), ("` (given " + got + ", expected " + min + ".." + max + ")")));
3639+
}
36193640
}
36203641

36213642
public RaiseException newErrnoEBADFError() {

core/src/main/java/org/jruby/RubyFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ protected IRubyObject openFile(ThreadContext context, IRubyObject args[]) {
14271427
if (!args[3].isNil()) {
14281428
options = TypeConverter.convertToTypeWithCheck(context, args[3], context.runtime.getHash(), sites(context).to_hash_checked);
14291429
if (options.isNil()) {
1430-
throw runtime.newArgumentError("wrong number of arguments (4 for 1..3)");
1430+
throw runtime.newArgumentError(4, 1, 3);
14311431
}
14321432
}
14331433
vperm(pm, args[2]);

core/src/main/java/org/jruby/RubyStruct.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
371371
IRubyObject keywordInit = RubyStruct.getInternalVariable(classOf(), KEYWORD_INIT_VAR);
372372

373373
if (keywordInit.isTrue()) {
374-
if (args.length != 1) throw context.runtime.newArgumentError("wrong number of arguments (given " + args.length + ", expected 0)");
374+
if (args.length != 1) throw context.runtime.newArgumentError(args.length, 0);
375375

376376
return initialize(context, args[0]);
377377
} else {
@@ -412,7 +412,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject arg0) {
412412
if (keywordInit.isTrue()) {
413413
IRubyObject maybeKwargs = ArgsUtil.getOptionsArg(runtime, arg0);
414414

415-
if (maybeKwargs.isNil()) throw context.runtime.newArgumentError("wrong number of arguments (given 1, expected 0)");
415+
if (maybeKwargs.isNil()) throw context.runtime.newArgumentError(1, 0);
416416

417417
setupStructValuesFromHash(context, (RubyHash) maybeKwargs);
418418

@@ -427,7 +427,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject arg0) {
427427
public IRubyObject initialize(ThreadContext context, IRubyObject arg0, IRubyObject arg1) {
428428
IRubyObject keywordInit = RubyStruct.getInternalVariable(classOf(), KEYWORD_INIT_VAR);
429429
if (keywordInit.isTrue()) {
430-
throw context.runtime.newArgumentError("wrong number of arguments (given 2, expected 0)");
430+
throw context.runtime.newArgumentError(2, 0);
431431
}
432432

433433
return initializeInternal(context, 2, arg0, arg1, context.nil);
@@ -437,7 +437,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject arg0, IRubyObje
437437
public IRubyObject initialize(ThreadContext context, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
438438
IRubyObject keywordInit = RubyStruct.getInternalVariable(classOf(), KEYWORD_INIT_VAR);
439439
if (keywordInit.isTrue()) {
440-
throw context.runtime.newArgumentError("wrong number of arguments (given 3, expected 0)");
440+
throw context.runtime.newArgumentError(3, 0);
441441
}
442442

443443
return initializeInternal(context, 3, arg0, arg1, arg2);

core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import static org.jruby.runtime.Block.Type.LAMBDA;
8787
import static org.jruby.util.RubyStringBuilder.str;
8888
import static org.jruby.util.RubyStringBuilder.ids;
89+
import static org.jruby.runtime.Arity.UNLIMITED_ARGUMENTS;
8990

9091
public class IRRuntimeHelpers {
9192
private static final Logger LOG = LoggerFactory.getLogger(IRRuntimeHelpers.class);
@@ -535,7 +536,7 @@ public static void checkArity(ThreadContext context, StaticScope scope, Object[]
535536
if (keywordArgs != null) argsLength -= 1;
536537

537538
if ((block == null || block.type.checkArity) && (argsLength < required || (!rest && argsLength > (required + opt)))) {
538-
Arity.raiseArgumentError(context.runtime, argsLength, required, required + opt);
539+
Arity.raiseArgumentError(context.runtime, argsLength, required, rest ? UNLIMITED_ARGUMENTS : (required + opt));
539540
}
540541
}
541542

core/src/main/java/org/jruby/runtime/Arity.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class Arity implements Serializable {
5555
public final static Arity ONE_REQUIRED = newArity(-2);
5656
public final static Arity TWO_REQUIRED = newArity(-3);
5757
public final static Arity THREE_REQUIRED = newArity(-4);
58+
public final static int UNLIMITED_ARGUMENTS = -1;
5859

5960
private Arity(int value) {
6061
this.value = value;
@@ -163,11 +164,11 @@ public void checkArity(Ruby runtime, IRubyObject[] args) {
163164
public void checkArity(Ruby runtime, int length) {
164165
if (isFixed()) {
165166
if (length != required()) {
166-
throw runtime.newArgumentError("wrong number of arguments (" + length + " for " + required() + ")");
167+
throw runtime.newArgumentError(length, required());
167168
}
168169
} else {
169170
if (length < required()) {
170-
throw runtime.newArgumentError("wrong number of arguments (" + length + " for " + required() + ")");
171+
throw runtime.newArgumentError(length, required());
171172
}
172173
}
173174
}
@@ -250,43 +251,42 @@ public static void raiseArgumentError(Ruby runtime, IRubyObject[] args, int min,
250251

251252
// FIXME: JRuby 2/next should change this name since it only sometimes raises an error
252253
public static void raiseArgumentError(Ruby runtime, int length, int min, int max) {
253-
if (length < min) throw runtime.newArgumentError(length, min);
254-
if (max > -1 && length > max) throw runtime.newArgumentError(length, max);
254+
if (length < min || (max > UNLIMITED_ARGUMENTS && length > max))
255+
throw runtime.newArgumentError(length, min, max);
255256
}
256257

257258
// FIXME: JRuby 2/next should change this name since it only sometimes raises an error
258259
public static void raiseArgumentError(ThreadContext context, int length, int min, int max) {
259-
if (length < min) throw context.runtime.newArgumentError(length, min);
260-
if (max > -1 && length > max) throw context.runtime.newArgumentError(length, max);
260+
raiseArgumentError(context.runtime, length, min, max);
261261
}
262262

263263
// FIXME: JRuby 2/next should change this name since it only sometimes raises an error
264264
public static void raiseArgumentError(Ruby runtime, int length, int min, int max, boolean hasKwargs) {
265-
if (length < min) throw runtime.newArgumentError(length, min);
266-
if (max > -1 && length > max) {
265+
if (length < min) throw runtime.newArgumentError(length, min, max);
266+
if (max > UNLIMITED_ARGUMENTS && length > max) {
267267
if (hasKwargs && length == max + 1) {
268268
// we have an extra arg, but kwargs active; let it fall through to assignment
269269
return;
270270
}
271-
throw runtime.newArgumentError(length, max);
271+
throw runtime.newArgumentError(length, min, max);
272272
}
273273
}
274274

275275
// FIXME: JRuby 2/next should change this name since it only sometimes raises an error
276276
public static void raiseArgumentError(Ruby runtime, String name, int length, int min, int max) {
277-
if (length < min) throw runtime.newArgumentError(name, length, min);
278-
if (max > -1 && length > max) throw runtime.newArgumentError(name, length, max);
277+
if (length < min || (max > UNLIMITED_ARGUMENTS && length > max))
278+
throw runtime.newArgumentError(name, length, min, max);
279279
}
280280

281281
// FIXME: JRuby 2/next should change this name since it only sometimes raises an error
282282
public static void raiseArgumentError(Ruby runtime, String name, int length, int min, int max, boolean hasKwargs) {
283-
if (length < min) throw runtime.newArgumentError(name, length, min);
284-
if (max > -1 && length > max) {
283+
if (length < min) throw runtime.newArgumentError(name, length, min, max);
284+
if (max > UNLIMITED_ARGUMENTS && length > max) {
285285
if (hasKwargs && length == max + 1) {
286286
// we have an extra arg, but kwargs active; let it fall through to assignment
287287
return;
288288
}
289-
throw runtime.newArgumentError(name, length, max);
289+
throw runtime.newArgumentError(name, length, min, max);
290290
}
291291
}
292292

core/src/main/java/org/jruby/runtime/Helpers.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,26 +612,26 @@ public static void handleArgumentSizes(ThreadContext context, Ruby runtime, int
612612
if (rest < 0) {
613613
// no opt, no rest, exact match
614614
if (given != required) {
615-
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + required + ")");
615+
throw runtime.newArgumentError(given, required);
616616
}
617617
} else {
618618
// only rest, must be at least required
619619
if (given < required) {
620-
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + required + ")");
620+
throw runtime.newArgumentError(given, required);
621621
}
622622
}
623623
} else {
624624
if (rest < 0) {
625625
// opt but no rest, must be at least required and no more than required + opt
626626
if (given < required) {
627-
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + required + ")");
627+
throw runtime.newArgumentError(given, required);
628628
} else if (given > (required + opt)) {
629-
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + (required + opt) + ")");
629+
throw runtime.newArgumentError(given, required + opt);
630630
}
631631
} else {
632632
// opt and rest, must be at least required
633633
if (given < required) {
634-
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + required + ")");
634+
throw runtime.newArgumentError(given, required);
635635
}
636636
}
637637
}

core/src/main/java/org/jruby/runtime/Signature.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.jruby.runtime.builtin.IRubyObject;
1515
import org.jruby.util.TypeConverter;
1616

17+
import static org.jruby.runtime.Arity.UNLIMITED_ARGUMENTS;
18+
1719
/**
1820
* A representation of a Ruby method signature (argument layout, min/max, keyword layout, rest args).
1921
*/
@@ -269,18 +271,18 @@ public String toString() {
269271

270272
public void checkArity(Ruby runtime, IRubyObject[] args) {
271273
if (args.length < required()) {
272-
throw runtime.newArgumentError("wrong number of arguments (" + args.length + " for " + required() + ")");
274+
throw runtime.newArgumentError(args.length, required(), hasRest() ? UNLIMITED_ARGUMENTS : (required() + opt));
273275
}
274276
if (rest == Rest.NONE || rest == Rest.ANON) {
275277
// no rest, so we have a maximum
276278
if (args.length > required() + opt()) {
277279
if (hasKwargs() && !TypeConverter.checkHashType(runtime, args[args.length - 1]).isNil()) {
278280
// we have kwargs and a potential kwargs hash, check with length - 1
279281
if (args.length - 1 > required() + opt()) {
280-
throw runtime.newArgumentError("wrong number of arguments (" + args.length + " for " + (required() + opt) + ")");
282+
throw runtime.newArgumentError(args.length, required(), hasRest() ? UNLIMITED_ARGUMENTS : (required() + opt));
281283
}
282284
} else {
283-
throw runtime.newArgumentError("wrong number of arguments (" + args.length + " for " + (required() + opt) + ")");
285+
throw runtime.newArgumentError(args.length, required(), hasRest() ? UNLIMITED_ARGUMENTS : (required() + opt));
284286
}
285287
}
286288
}

core/src/main/ruby/jruby/kernel/enumerable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Enumerable
22
def slice_before(filter = (no_filter = true; nil), &block)
3-
raise ArgumentError.new("wrong number of arguments (0 for 1)") if (no_filter && !block) || (!no_filter && block)
3+
raise ArgumentError.new("wrong number of arguments (given 0, expected 1)") if (no_filter && !block) || (!no_filter && block)
44

55
state = nil
66

@@ -33,7 +33,7 @@ def slice_before(filter = (no_filter = true; nil), &block)
3333
end
3434

3535
def slice_after(filter = (no_filter = true; nil), &block)
36-
raise ArgumentError.new("wrong number of arguments (0 for 1)") if no_filter && !block
36+
raise ArgumentError.new("wrong number of arguments (given 0, expected 1)") if no_filter && !block
3737
raise ArgumentError.new("cannot pass both filter argument and block") if !no_filter && block
3838

3939
state = nil

core/src/test/java/org/jruby/javasupport/TestJava.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ public void testJavaConstructorExceptionHandling() throws Exception {
112112
assert false;
113113
}
114114
catch (RaiseException ex) {
115-
assertEquals("(ArgumentError) wrong number of arguments (0 for 1)", ex.getMessage());
115+
assertEquals("(ArgumentError) wrong number of arguments (given 0, expected 1)", ex.getMessage());
116116
assertNull(ex.getCause());
117117
assertNotNull(ex.getException());
118-
assertEquals("wrong number of arguments (0 for 1)", ex.getException().getMessage().toString());
118+
assertEquals("wrong number of arguments (given 0, expected 1)", ex.getException().getMessage().toString());
119119
}
120120

121121
try {

test/jruby/test_higher_javasupport.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ def test_raised_errors_on_array_proxy
18441844
fail 'expected to raise'
18451845
rescue ArgumentError => e
18461846
msg = e.message
1847-
assert msg.start_with?("wrong number of arguments calling `length` (1 for 0)"), msg
1847+
assert msg.start_with?("wrong number of arguments calling `length` (given 1, expected 0)"), msg
18481848
end
18491849

18501850
begin # array proxy class

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy