Skip to content

JS/TS: scope let/const bindings to their own block, not the function (issue 2822) - #3688

Closed
ayushcodes10 wants to merge 3 commits into
Graphify-Labs:v8from
ayushcodes10:fix-2822-block-scoped-const-let
Closed

ayushcodes10 wants to merge 3 commits into
Graphify-Labs:v8from
ayushcodes10:fix-2822-block-scoped-const-let

Conversation

@ayushcodes10

Copy link
Copy Markdown
Contributor

What

_js_local_bound_names collected let/const declarator targets, and a for/for-of/for-in loop's own binding, into one function-wide shadow set. Both are block-scoped in JS (unlike var, which is genuinely function-scoped through hoisting, regardless of nesting depth). A reference to a same-named module callable made outside the block that actually binds the name had its genuine indirect_call edge suppressed:

  • a const/let declared inside a nested if/try/bare-{} block
  • a for...of/for...in loop binding, referenced after the loop ends
  • a closure that outlives the loop and still references the loop binding

This is issue #2822's own repro, reproduced directly against current v8 before any change (all 3 cases produced no edge; the control case — a reference still inside the loop, #2606 — correctly produced none either).

Fix

The function-wide set (_js_local_bound_names) now only collects var — distinguished by checking whether a variable_declarator's parent is variable_declaration (var) rather than lexical_declaration (let/const), and a for-in/for-of loop's own binding only when it uses var.

Every let/const and for/for-of/for-in binding is instead folded into extra_locals for exactly its own block or loop subtree — the same mechanism already used for catch bindings (#2517) — via a new _js_direct_lexical_names helper plus two new cases in walk_calls (for statement_block/for_statement, and for_in_statement). A reference genuinely inside that block or loop still correctly shadows, while one outside it resolves. A C-style for (let i = ...) loop's own initializer gets the same treatment, since it's scoped to the whole loop the same way a for-of binding is.

Tests

New file tests/test_indirect_call_block_scoped_shadow.py, 8 tests:

  • the issue's 3 reported shapes (block-scoped const, for-of after the loop, closure outliving the loop)
  • the control case that must not regress (JavaScript resolver links block-scoped object-shorthand variables to unrelated functions #2606: a reference still inside its own loop)
  • var's continued function-wide reach in the same shapes (block and for-of)
  • a C-style for (let ...) loop, asserting on source_location since both calls read as identical source text and a plain edge-presence check can't distinguish "only the outside call resolved" from "both wrongly resolved"
  • a genuine reference elsewhere still resolving (no blanket suppression)

Verified each of the 4 new-behavior tests fails against the pre-fix code and passes after. Full suite: 5694 passed, 68 skipped, 0 failures.

Fixes #2822

ayushcodes10 and others added 3 commits September 20, 2026 01:48
_js_local_bound_names collected let/const declarator targets, and a
for of/for in loop's own binding, into one function wide shadow set,
but both are block scoped in JS, unlike var, which is function scoped
through hoisting regardless of nesting depth. A reference to a
same named module callable made outside the block that actually binds
the name had its genuine indirect_call edge suppressed, whether the
binding sat inside an if, try, or bare brace block, came from a for
of/for in loop referenced after the loop ends, or was read from a
closure that outlives the loop.

The function wide set now only collects var, distinguished by
checking whether a declarator's parent is a variable_declaration
rather than a lexical_declaration, and a for in loop's own binding
only when it uses var. Every let/const and for/for of/for in binding
is instead folded into extra_locals for exactly its own block or loop
subtree, the same mechanism already used for catch bindings, so a
reference genuinely inside that block or loop still correctly shadows
while one outside it resolves. A C style for loop's own let/const
initializer gets the same treatment, since it is scoped to the whole
loop the same way a for of binding is.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017qfdzgbA5KedGEjD1AayNh
Covers the issue's three reported shapes (const inside an if block,
a for of binding referenced after the loop, and a closure that
outlives the loop), the control case that must not regress (a
reference still inside its own loop), var's continued function wide
reach in the same shapes, and a genuine reference elsewhere still
resolving. The C style for loop case asserts on source_location since
both calls read as identical source text and a plain edge presence
check cannot tell "only the outside call resolved" from "both wrongly
resolved."

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017qfdzgbA5KedGEjD1AayNh
@ayushcodes10

Copy link
Copy Markdown
Contributor Author

Note on risk: the issue itself says the reporter prototyped this same general approach (routing block-scoped bindings through `extra_locals`, mirroring the existing `catch_clause` fold) and found it "exposed other inaccuracies in the shadow collector" and "was not safe" — without specifying which inaccuracies. That's a serious, specific caution from someone who clearly did deep field-testing (see the issue's own 920-file A/B methodology for the for-of arm), so I want to be upfront about it rather than let it go unmentioned.

What I did to try to gain confidence despite that: reproduced the issue's exact 3 cases plus its own control case against current `v8` before writing anything; verified `var`'s continued function-wide (hoisted) reach isn't broken in the same shapes (block-nested and for-of); verified a C-style `for (let i = ...)` loop still shadows correctly inside its own loop while a reference outside it now resolves, distinguishing the two via `source_location` since a naive edge-presence check can't tell them apart; ran the full existing shadow/indirect_call test suite (`catch`, single-arrow-param, external-import, nested-closure, for-of, #2606) plus the complete test suite (5694 tests) with zero regressions; and confirmed each new test genuinely fails against the pre-fix code.

That said, I don't know what specific "other inaccuracies" the reporter's own prototype surfaced, so I can't be certain this fix doesn't share whatever gap theirs had. Flagging this explicitly so review weighs it accordingly — happy to dig further into any specific concern, or to have this held for closer scrutiny before merge given that history.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).

Formal verification. No changes could be formally verified in this run.


Graphify review — findings

Fixes JS/TS indirect_call edges being wrongly suppressed by block-scoped bindings: _js_local_bound_names now collects only function-scoped var (and var loop bindings) into the function-wide shadow set, so let/const in a nested if/try/bare block and for/for-of/for-in loop bindings no longer hide same-named module callables referenced outside their block. walk_calls in _extract_generic instead folds each lexical binding into extra_locals for exactly its own statement_block, C-style for_statement, or for_in_statement subtree — via the new _js_direct_lexical_names — reusing the mechanism already handling catch bindings, so references genuinely inside the block still shadow correctly.

No blocking issues surfaced. 4 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 908 functions depend on the 459 functions this change touches.

Health — this change adds coupling hotspots:

  • new: _extract_generic() — 18 callers, 29 callees
  • new: extract_js() — 87 callers, 4 callees
  • new: extract_xaml() — 19 callers, 17 callees
  • new: extract_objc() — 27 callers, 9 callees
  • new: extract_julia() — 17 callers, 7 callees
  • new: extract_cpp() — 29 callers, 3 callees
  • new: extract_vue() — 10 callers, 7 callees
  • new: walk() — 1 callers, 62 callees
  • …and 9 more — each is listed as a finding

Verification — 908 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 848 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

287 of 287 test file(s) selected (100%) via static blast radius.

Escalated to a full run for safety — the selection is not trustworthy on its own (see below). CI should run the whole suite.

  • tests/test_affected_cli.py — full-run-safety
  • tests/test_affected_member_seed.py — full-run-safety
  • tests/test_agents_platform.py — full-run-safety
  • tests/test_analyze.py — full-run-safety
  • tests/test_anthropic_custom_endpoint.py — full-run-safety
  • tests/test_antigravity_install.py — full-run-safety
  • tests/test_apm_fallback_version.py — full-run-safety
  • tests/test_architecture_doc.py — full-run-safety
  • tests/test_astro_extraction.py — impact, full-run-safety
  • tests/test_astro_import_ids.py — full-run-safety
  • tests/test_atomic_canvas_export.py — full-run-safety
  • tests/test_atomic_version_stamp.py — full-run-safety
  • tests/test_atomic_writes.py — full-run-safety
  • tests/test_backend_env_isolation.py — full-run-safety
  • tests/test_backend_extras.py — full-run-safety
  • tests/test_benchmark.py — full-run-safety
  • tests/test_benchmark_raw_graph.py — full-run-safety
  • tests/test_build.py — impact, full-run-safety
  • tests/test_build_merge_dedup_scope.py — full-run-safety
  • tests/test_build_merge_hyperedges_and_prune.py — full-run-safety
  • tests/test_build_merge_shrink_guard.py — full-run-safety
  • tests/test_builtin_global_type_refs.py — full-run-safety
  • tests/test_cache.py — full-run-safety
  • tests/test_callflow_html.py — full-run-safety
  • tests/test_cargo_introspect.py — full-run-safety
  • tests/test_carried_hyperedge_remap.py — full-run-safety
  • tests/test_case_sensitive_resolution.py — full-run-safety
  • tests/test_charmap_encoding.py — full-run-safety
  • tests/test_chunking.py — full-run-safety
  • tests/test_cjs_module_extension.py — impact, full-run-safety
  • tests/test_claude_cli_backend.py — full-run-safety
  • tests/test_claude_md.py — full-run-safety
  • tests/test_cli_broken_pipe.py — full-run-safety
  • tests/test_cli_export.py — full-run-safety
  • tests/test_cli_help.py — full-run-safety
  • tests/test_cluster.py — full-run-safety
  • tests/test_codebuddy.py — full-run-safety
  • tests/test_community_hub_labels.py — full-run-safety
  • tests/test_community_labels_skill.py — full-run-safety
  • tests/test_confidence.py — full-run-safety
  • tests/test_corrupt_graph_json.py — full-run-safety
  • tests/test_cpp_nested_and_cli.py — impact, full-run-safety
  • tests/test_cpp_objc_cross_file_calls.py — full-run-safety
  • tests/test_cpp_preprocess.py — full-run-safety
  • tests/test_cross_extension_reexport_self_cycle.py — full-run-safety
  • tests/test_cross_language_call_resolution.py — full-run-safety
  • tests/test_cross_repo_external_call_guards.py — full-run-safety
  • tests/test_cross_repo_member_calls.py — full-run-safety
  • tests/test_cross_repo_shared_types.py — full-run-safety
  • tests/test_csharp_call_site_generic_args.py — full-run-safety
  • … and 237 more

non-code file(s) changed (CHANGELOG.md) → running the full suite for safety (a code graph can't see config/fixture/data deps)

changed code file(s) with no mapped test (CHANGELOG.md) — a coverage gap or a missing link — running the full suite rather than only the selected tests

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `path` is annotated `Path` — outside the synthesizable primitive/collection set

Could not verify: Could not verify \_js\_local\_bound\_names.

The verifier did not have enough to check \_js\_local\_bound\_names, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 200 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

· 17 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.65 (on PyPI). Cherry-picked with authorship preserved. Thanks @ayushcodes10!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JS/TS: block-scoped let/const bindings suppress genuine indirect_call edges outside their scope

2 participants