Repository navigation
Description
Method#arity and #parameters carry no information for a native method: the
arity is −1 for almost all of them and parameters is always [[:rest]].
Measured on main @be2598d against MRI 4.0.5, across the instance methods of
String, Array, Hash, Integer, Symbol, Float and Range:
| arity | MRI 4.0.5 | rbgo |
|---|---|---|
| −1 | 153 | 467 |
| −2 | 3 | 0 |
| 0 | 193 | 6 |
| 1 | 132 | 3 |
| 2 | 8 | 0 |
So MRI reports −1 for 31% of them and rbgo for 98%. Spot checks:
String.instance_method(:length).arity # MRI 0 rbgo -1
String.instance_method(:each_char).arity # MRI 0 rbgo -1
Integer.instance_method(:times).arity # MRI 0 rbgo -1
String.instance_method(:sub).arity # MRI -1 rbgo -1 (agrees, by accident)
String.instance_method(:length).parameters # MRI [] rbgo [[:rest]]Why this is one defect and not hundreds
internal/vm/method.go's methodArity has no channel through which a native
method's declared arity could arrive — the native table does not record it — so the
value is a constant dressed as a measurement. Method#inspect and #parameters
read the same place, which is why they are wrong in lockstep.
That also means fixing one method is worse than fixing none: give length
arity 0 while parameters still answers [[:rest]] and the two become mutually
inconsistent, which no MRI method is. The fix has to be a field on the native
registration, applied across the table — or nothing.
The shape, which is now the fourth instance in two days
A value that carries no information while looking like a value. The others:
Time.nowreturned whole seconds, so every Ruby-level elapsed-time measurement
read0.0— I read one as "this change costs nothing" (Time.now truncates to a whole second, so every Ruby-level elapsed-time measurement reads 0.0 #689, fixed in Carry nanoseconds from the wall clock into Time.now (#689) #702).Process.clock_gettime(999)returns a plausible wall-clock Float where MRI raises
Errno::EINVAL(File::Stat timestamps truncate to whole seconds (same carrier shape as #689), four CLOCK_* constants missing, and clock_gettime(999) returns a plausible number where MRI raises #705).- a coverage profile read
100.0%for a function the gate read at86.2%, because
the local toolchain counted 21 statements where CI counted 29 (The coverage gate cannot be reproduced locally (Go 1.26 counts 404 blocks where CI's 1.27 counts 414) and has no rounding headroom left #704).
Each cost more to find than a crash would have, because nothing prompts anyone to
look at an answer that seems reasonable. arity returning −1 is the most benign
member of the family — −1 is a legal answer — and therefore the hardest to notice.
Found while measuring the ARGF surface (#712): one spec asserts
each_codepoint.arity == 0, which is the only example in the corpus that this
defect costs. The corpus can see 1 of it; a program that dispatches on arity sees
all of it.
Activity
Refuse an undeclared core-class native arity, and pin every count to …
Give a native method's arity a channel to arrive through (#714) (#721)