Many built-in functions only convert their arguments and then call the String
method that does the work: LEFT calls RexxString::left, ABS calls
RexxString::abs, and so on. The method checks the rest and, when an argument
is wrong, raises its own error. That error speaks of a method the program never
called, counts the arguments as the method does (one less than the function,
since the function's first argument is the method's target), and sometimes names
no argument at all:
say left("abc", -1) -- 93.923 Invalid length argument specified; found "-1".
say substr("abc", 0) -- 93.924 Invalid position argument specified; found "0".
say copies("abc", -1) -- 93.906 Method argument 1 must be zero or a positive whole number; found "-1".
say strip("abc", "X") -- 93.915 Method option must be one of "BLT"; found "X".
say abs("A") -- 93.943 ABS method target must be a number; found "A".
say max(5, "A") -- 93.904 Method argument 1 must be a number; found "A".
say max(1, , 2) -- 93.903 Missing argument in method; argument 0 is required.
This is the common cause of [#2006], [#2007], [#2015] and [#2016], and of the
COPIES and TRUNC part of [#2010]. I wrote a small program that calls 56 built-in
functions with a bad value ("A", -1, 0, 1.5, "+", "", omitted) in
each argument position: 131 of its 1,011 calls end in a 93.xxx, in 40 different
functions.
With the attached patch:
say left("abc", -1) -- 40.13 LEFT argument 2 must be zero or positive; found "-1".
say substr("abc", 0) -- 40.14 SUBSTR argument 2 must be positive; found "0".
say copies("abc", -1) -- 40.13 COPIES argument 2 must be zero or positive; found "-1".
say strip("abc", "X") -- 40.904 STRIP argument 2 must be one of BLT; found "X".
say abs("A") -- 40.11 ABS argument 1 must be a number; found "A".
say max(5, "A") -- 40.11 MAX argument 2 must be a number; found "A".
say max(1, , 2) -- 40.5 Missing argument in invocation of MAX; argument 2 is required.
The methods themselves are not changed: "abc"~left(-1) still raises 93.923.
How it works: a running built-in function now marks itself on its activity
(BuiltinFunctionScope, created by fix_args/check_args, which every
built-in function starts with), together with the activation frame it runs in.
The argument checks the String methods use (lengthArgument,
positionArgument, optionArgument, padArgument, numberArgument,
requiredPositive, requiredNonNegative, missingArgument, the "target
must be a number" check, and MAX/MIN's own checks) raise the function's 40.xxx
error, with its name and argument position, but only while the function's frame
is still the top one. Anything reached through a message send runs in a frame
of its own, so a method called by name, user code, or a STRING method that the
function calls on its argument keeps its method errors. The seven functions
whose method target is their second argument (POS, LASTPOS, INSERT, OVERLAY,
WORDPOS, CHANGESTR, COUNTSTR) say so, so that the positions come out right.
Two small bugs are fixed along the way:
5~max(, 2) and 5.5~max(, 2): "argument 1").Not in this patch, because they work differently (separate patches will
follow): the stream functions ([#2011], [#2014], the CHARIN part of [#2010]), BEEP
([#2009]), and the data errors of D2C, D2X, X2D and FORMAT (93.927 to 93.942).
Tests: 216 expectSyntax calls in 30 test groups expected the method codes,
and now expect the function's: 93.906 and 93.923 become 40.13, 93.924 becomes
40.14, 93.943 becomes 40.11, and 93.915 becomes 40.904. A new
base/bif/BuiltinArgumentErrors.testGroup checks the code, the function name
and the position (through ADDITIONAL). It also checks that the methods keep
their errors, and that code reached through a message does too. 6 of its 7
tests fail without the patch.
Full test suite on Linux (24,361 tests) and on Windows (24,612 tests, MSVC):
no changes other than those.
Bugs: #2006
Bugs: #2007
Bugs: #2009
Bugs: #2010
Bugs: #2011
Bugs: #2014
Bugs: #2015
Bugs: #2016
Anonymous
Diff:
Thank you for fixing this! It has bugged (no pun intended) me for a long time and I had determined the basic cause but didn't see how to fix it. Well done!
Committed in [r13363] (code) and [r13364] (tests: the 216 expectSyntax calls
updated, and the new base/bif/BuiltinArgumentErrors.testGroup).
Built-in functions now report their own errors (40.xxx, with the function's
name and argument position) when the method that does the work finds a bad
argument; the methods themselves keep their 93.xxx errors. This fixes
[#2006], [#2007], [#2015] and [#2016], and the CHANGESTR, COPIES and TRUNC part of
[#2010]. The stream functions ([#2011], [#2014], the CHARIN part of [#2010]) and
BEEP ([#2009]) work differently and will get patches of their own.
Related
Bugs:
#2006Bugs:
#2007Bugs:
#2009Bugs:
#2010Bugs:
#2011Bugs:
#2014Bugs:
#2015Bugs:
#2016Commit: [r13363]
Commit: [r13364]