Skip to content

perl args: legacy prototypes (sub foo($$;\@)) miscounted as exactly 1 arg regardless of true arity #1607

Closed

Description

@squid-protocol

Summary

Found while investigating perl's args_exact_match accuracy gap (769/937 = 82.1% baseline): traditional Perl subs commonly declare an old-style PROTOTYPE instead of (or as well as) a modern named signature. A prototype is a sequence of bare sigils ($=scalar, @=array, %=hash, ;=optional-args marker, \=by-ref) with NO commas between them. The args regex's first alternative matches a prototype's parens exactly the same as it matches a real signature's, and _count_top_level_args's "comma-free non-empty parens = exactly 1 argument" heuristic (added by #1199 for real signatures, correct there) silently misapplies to prototypes: a prototype like ($$;@) structurally never contains a comma regardless of how many parameters it declares, so every prototype function is locked at got=1 forever.

Roughly 150 of the 168 total perl args_exact_match mismatches in the current baseline fit this exact shape (got=1 no matter what real is), concentrated almost entirely in exiftool/ExifTool.pm and exiftool/exiftool -- old-style Perl heavy on prototypes.

Confirmed repro

Measured against language-crucible/data/perl, corpus commit 12fc7af, via tests/tools/tree_sitter_accuracy_audit.py --lang perl --verbose (each line is a distinct function in exiftool/ExifTool.pm or exiftool/exiftool):

Get8u($$)              real=0 got=1   (sub Get8u($$) { return DoUnpackStd('C', @_); })
SwapBytes($$)          real=2 got=1
FindValue($$$)         real=3 got=1
Decode(...)            real=6 got=1
HDump($$$$;$$$)        real=7 got=1

The real values above are correct (measured by tree-sitter's own _count_perl_real_args, added by #1519's fix, PR #1520, which falls through to body-idiom walking when no named signature exists). The got values are all locked at 1 because the regex matches the prototype and the count-heuristic misapplies.

Tree-sitter's ground truth already handles this correctly: it only trusts a modern named signature tree-sitter node. For anything else (prototypes included), it falls through to walking the body's my (...) = @_ / shift idioms -- which is why the real=0/2/3/6/7 values above are correct even though the sub has a prototype, not a named signature.

Root cause

File: gitgalaxy/standards/language_standards.py, perl's args rule (first alternation branch: \b(?:sub|method)(\s+[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*)?\s*(\([^)]*\))).

Combined with gitgalaxy/core/detector.py's _calculate_block_metrics counting logic (around the #1199 self-contained-(...) branch: stripped.startswith("(") and stripped.endswith(")") -> self._count_top_level_args(stripped)), which applies a "comma-free non-empty parens = exactly 1 argument" heuristic that's correct for real signatures but wrong for prototypes.

Fix strategy

Recognize when the parenthesized text after sub name is a bare-sigil PROTOTYPE (roughly ^[$@%\;+*&\\]*$, no identifier characters at all) rather than a real named signature. When detected, skip counting it as a self-contained arg list and fall through to the SAME body-idiom scan (_args_findall_sum_groups, groups 3/4/5) that #1519 already built for signature-less traditional subs.

Treat a prototype-only sub exactly like a signature-less one, since a prototype only gives sigil TYPES (and even summing sigils isn't equivalent to real arity once ; marks optional args), while the body-idiom scan already recovers the correct count for this exact shape (proven by the real=0/2/3/6/7 values above all coming from that same body-walk on the ground-truth side).

References

#1199 (added the comma-free heuristic), #1519 (fixed the similar body-idiom-only case), #1520 (PR for #1519), #1227 (parent tree-sitter-accuracy epic), #1261 (related args-counting accuracy work).

Same shape of bug as #1519 (a wrong count, not a missed detection) -- cross-reference when closing.

Activity

  1. added
    bug
    testing
    core-engine
    threat: false-negative
    on Aug 14, 2026
  2. squid-protocol commented on Aug 14, 2026

    @squid-protocol
    OwnerAuthor

    Fixed and verified in #1610 (merged, all CI green): legacy prototypes now detected via the new _args_prototype_groups rules-dict flag and fall through to the same body-idiom scan #1519 built for signature-less subs, instead of being locked at a false count of 1. Also fixed the related $shift-variable-name false-positive this exposed. Confirmed via tree_sitter_accuracy_audit.py --lang perl: args_exact_match 769/937 (82.1%) → 923/938 (98.4%).

  3. added this to the v2.4.8 milestone on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcore-enginetestingthreat: false-negative

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions