Skip to content

issue-2088 plan: a parenthesized for-of/for-in loop-head target (for ((x) of arr)) is invisible to patternBindsName #2630

Open

Description

@carlos-alm

What

While verifying findResolvingScopeNode's new for_in_statement disjunct (PR #2612, plan for issue #2088) against the real tree-sitter-javascript grammar, found that a PARENTHESIZED for-of/for-in loop-head target is invisible to the shadow check the disjunct relies on, patternBindsName.

Verified against tree-sitter-javascript@0.25.0's node-types.json: for_in_statement.left's declared type list includes parenthesized_expression alongside identifier/array_pattern/object_pattern/member_expression/subscript_expression/undefined — i.e. for ((x) of arr) is grammar-valid, with left a parenthesized_expression wrapping the identifier x. patternBindsName (src/extractors/javascript.ts:4861-4909) has no case for parenthesized_expression and falls through to its default: return false (javascript.ts:4906-4907), so patternBindsName(left, name) returns false even when x === name — the loop-head binding is invisible to any caller that resolves it through this primitive.

Why this matters, and why it's the safe direction

Two call sites are affected identically, since both resolve the SAME left field through the SAME primitive:

Both are the same shadow-miss shape as the arrow-bare-parameter gap filed separately as its own #2088 plan follow-up — pathological syntax (parenthesizing a bare for-of/for-in target has no practical use apparent here, but the grammar permits it), and, for blockContainsIdentifierExcluding specifically, the safe direction: a missed shadow there causes over-inclusion in a reference walk whose contract only ever gets MORE conservative (more likely to escape) from spurious extra matches, never less sound.

Suggested fix shape (not binding — decide at execute/fix time)

Unwrap a leading parenthesized_expression (recursively, in case of nested parens) before applying the existing pattern-shape cases in patternBindsName, or add a dedicated case that delegates to its own single child. Add a fixture for for ((x) of arr) proving the unwrapped target is recognised, for both affected call sites.

Where

Activity

  1. added
    follow-upDeferred work from PR reviews that needs tracking
    on Aug 24, 2026
  2. carlos-alm commented on Aug 24, 2026

    @carlos-alm
    ContributorAuthor

    Correction to this issue's own rationale, found while implementing round 13 of the #2088 plan (PR #2612).

    This issue's "Why this matters" section calls a parenthesized for-of/for-in loop-head target "the same shadow-miss shape as the arrow-bare-parameter gap" (filed as #2629). It is not, and the difference matters for anyone who picks this up.

    for (const (x) of arr) — parenthesizing a target inside a DECLARING for-of/for-in head — is a SyntaxError, and tree-sitter-javascript's own grammar confirms this structurally, not just empirically. Checked against grammar.json in tree-sitter-javascript@0.25.0: the hidden _for_header rule's three kind-bearing branches (var; let/const; using/await using) each require left to be identifier | alias('of', identifier) | _destructuring_pattern — parenthesized_expression is not one of the alternatives in any of them. parenthesized_expression is reachable only through the OTHER member of the same top-level CHOICE — the one with no kind field at all. So a parenthesized loop-head target is, by construction, never a declaration and never introduces a shadow — it is always an ASSIGNMENT to a pre-existing binding declared somewhere else. The arrow-bare-parameter gap (#2629) is the opposite kind of thing: a bare run => {…} parameter genuinely DECLARES a new, shadowing binding that introducesShadowedBinding simply fails to see. Those are different failure classes, not the same one twice.

    This doesn't change what's broken here — patternBindsName's missing parenthesized_expression case is real, and the suggested fix (unwrap the parens before applying the existing pattern-shape cases) is still correct and still safe: under-detecting either a shadow (condition 4's resolution question) or a write (a case added this round, see below) only ever pushes the analysis toward its already-accepted conservative direction, never toward unsoundness. But the write-up's framing would send whoever picks this up looking for a missed DECLARATION, when what's actually missing is recognition of an ASSIGNMENT target.

    One more consumer worth noting for whoever fixes this: round 13 of the #2088 plan (PR #2612) adds a subtreeContainsReassignmentOf check that also calls patternBindsName on a for_in_statement's left field, for the same reason findResolvingScopeNode's existing for-in disjunct does. Fixing patternBindsName here will close a matching gap for that new caller too (a parenthesized for ((run) of arr) reassignment target), not just for the two consumers already listed in "Where."

    Leaving this open — the underlying patternBindsName gap is still real and still worth fixing, just for the reason above rather than the one originally stated.

  3. carlos-alm commented on Aug 24, 2026

    @carlos-alm
    ContributorAuthor

    Correction to my own immediately-preceding comment on this issue -- it was wrong, and the error is soundness-relevant, not a wording nit, so I want to flag it plainly rather than let it stand alongside the first correction.

    That comment claimed: "under-detecting either a shadow (condition 4's resolution question) or a write (a case added this round, see below) only ever pushes the analysis toward its already-accepted conservative direction, never toward unsoundness." That is inverted, for both the shadow case and the write case. I over-generalized a safety property this issue's own original body correctly scoped to ONE consumer only, and wrongly extended it to the other two.

    Why the original issue body's scoping was right. blockContainsIdentifierExcluding's reference walk (condition 3, #2260) is a monotonic AND over every candidate reference position it finds. Missing a shadow there only ever ADDS an extra candidate node to check -- which can flip the walk's verdict from "tracked" to "not tracked" (conservative: escapes moves toward true) but can never flip it the other way, since adding a conjunct to an AND cannot turn a false result true. The original issue body said this explicitly and correctly: "for blockContainsIdentifierExcluding specifically, the safe direction."

    Why findResolvingScopeNode's shadow check and subtreeContainsReassignmentOf's write check are NOT this shape. Both are single fail-safe branches, not conjuncts in a walk. Missing a shadow in findResolvingScopeNode does not add anything to a check -- it REMOVES the fail-safe branch that would otherwise have fired, letting execution fall through to findTopLevelFunctionNodeByName's module-level search, which then confidently resolves to whatever declaration it finds there. If that declaration happens to be this-free, the function returns "safe" -- wrongly, since the shadow it missed might genuinely be this-using. That is a confidently WRONG "safe" vote, i.e. fail-OPEN -- exactly the round-10/round-11 failure mode this whole resolution chain exists to close, not the conservative direction. The identical mechanism applies to subtreeContainsReassignmentOf's write check: missing a write does not add a candidate to an AND, it lets a stale, already-resolved initial value be trusted as though no later reassignment existed -- again fail-open, not conservative.

    Net effect. My prior comment's safety claim holds ONLY for blockContainsIdentifierExcluding, exactly as the original issue body already said. It does NOT extend to findResolvingScopeNode's shadow check or to subtreeContainsReassignmentOf's write check, both of which remain genuine, if narrow and pathological, soundness gaps for as long as patternBindsName has no parenthesized_expression case.

    Re-scoping. This issue now names FOUR affected call sites, not three. Round 14 (PR #2612) additionally routes subtreeContainsReassignmentOf's assignment-expression branch through patternBindsName (closing a separate, unrelated detection gap -- see that commit), so a parenthesized assignment target ((run) = fn) is now invisible to that arm too, alongside the pre-existing gaps in findResolvingScopeNode's for-in disjunct, blockContainsIdentifierExcluding's for-in branch, and subtreeContainsReassignmentOf's OWN for-in branch. Of these four, only blockContainsIdentifierExcluding's is conservative-only; the other three are latent, narrow soundness gaps, not merely recall costs.

    Leaving this open -- the suggested fix (unwrap a leading parenthesized_expression in patternBindsName before applying its existing pattern-shape cases) is unchanged from the original.

  4. carlos-alm commented on Aug 24, 2026

    @carlos-alm
    ContributorAuthor

    Status update, round 16 (PR #2612).

    This issue's own second comment above already corrected the "safe direction" framing for three of its four named consumers (fail-OPEN, not conservative) and re-scoped this issue to four affected call sites. This comment is a closure update on top of that correction, not a re-litigation of it.

    Round 16 closes three of the four:

    • findResolvingScopeNode's for_in_statement disjunct (condition 4's resolution question)
    • subtreeContainsReassignmentOf's own for_in_statement branch (condition 4's write-scan)
    • subtreeContainsReassignmentOf's assignment_expression/augmented_assignment_expression branch (the fourth consumer the second comment above named)

    Fixed via a small local unwrapParens(node) helper, called at each of these three call sites immediately before delegating to patternBindsName — not by adding a parenthesized_expression case to patternBindsName itself. patternBindsName stays exactly as narrow as its other verified consumers (blockContainsIdentifierExcluding's own for-in branch, killsBinding's assignment branch, declarationDeclaresName) already depend on it being.

    This issue's remaining scope narrows to its one conservative-direction consumer: blockContainsIdentifierExcluding's own for_in_statement branch (condition 3, #2260's reference-walk boundary). The second comment above already proves that one is safe to leave as-is — a missed shadow there only ever adds a spurious candidate to a conjunction (pushing toward escapes = true, never the other way) — so it's fine for it to remain open as an accepted, over-escape-direction limitation. The other three, being fail-open, could not be left that way.

    Mirrored in the Rust engine (crates/codegraph-core/src/extractors/javascript.rs): a new unwrap_parens helper at the equivalent three call sites, verified against the real, already-shipped pattern_binds_name's own _ => false default arm (javascript.rs:5036), which carries the identical gap.

    See docs/plans/issue-2088.md, WU-2's findResolvingScopeNode and subtreeContainsReassignmentOf (round-16 essays), and WU-10 escape-fallback cases (al)-(an).

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

    follow-upDeferred work from PR reviews that needs tracking

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions