Repository navigation
JS/TS: scope let/const bindings to their own block, not the function (issue 2822) - #3688
ayushcodes10 wants to merge 3 commits into
Conversation
_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
Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_017qfdzgbA5KedGEjD1AayNh
|
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. |
There was a problem hiding this comment.
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-safetytests/test_affected_member_seed.py— full-run-safetytests/test_agents_platform.py— full-run-safetytests/test_analyze.py— full-run-safetytests/test_anthropic_custom_endpoint.py— full-run-safetytests/test_antigravity_install.py— full-run-safetytests/test_apm_fallback_version.py— full-run-safetytests/test_architecture_doc.py— full-run-safetytests/test_astro_extraction.py— impact, full-run-safetytests/test_astro_import_ids.py— full-run-safetytests/test_atomic_canvas_export.py— full-run-safetytests/test_atomic_version_stamp.py— full-run-safetytests/test_atomic_writes.py— full-run-safetytests/test_backend_env_isolation.py— full-run-safetytests/test_backend_extras.py— full-run-safetytests/test_benchmark.py— full-run-safetytests/test_benchmark_raw_graph.py— full-run-safetytests/test_build.py— impact, full-run-safetytests/test_build_merge_dedup_scope.py— full-run-safetytests/test_build_merge_hyperedges_and_prune.py— full-run-safetytests/test_build_merge_shrink_guard.py— full-run-safetytests/test_builtin_global_type_refs.py— full-run-safetytests/test_cache.py— full-run-safetytests/test_callflow_html.py— full-run-safetytests/test_cargo_introspect.py— full-run-safetytests/test_carried_hyperedge_remap.py— full-run-safetytests/test_case_sensitive_resolution.py— full-run-safetytests/test_charmap_encoding.py— full-run-safetytests/test_chunking.py— full-run-safetytests/test_cjs_module_extension.py— impact, full-run-safetytests/test_claude_cli_backend.py— full-run-safetytests/test_claude_md.py— full-run-safetytests/test_cli_broken_pipe.py— full-run-safetytests/test_cli_export.py— full-run-safetytests/test_cli_help.py— full-run-safetytests/test_cluster.py— full-run-safetytests/test_codebuddy.py— full-run-safetytests/test_community_hub_labels.py— full-run-safetytests/test_community_labels_skill.py— full-run-safetytests/test_confidence.py— full-run-safetytests/test_corrupt_graph_json.py— full-run-safetytests/test_cpp_nested_and_cli.py— impact, full-run-safetytests/test_cpp_objc_cross_file_calls.py— full-run-safetytests/test_cpp_preprocess.py— full-run-safetytests/test_cross_extension_reexport_self_cycle.py— full-run-safetytests/test_cross_language_call_resolution.py— full-run-safetytests/test_cross_repo_external_call_guards.py— full-run-safetytests/test_cross_repo_member_calls.py— full-run-safetytests/test_cross_repo_shared_types.py— full-run-safetytests/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).
|
Shipped in v0.9.65 (on PyPI). Cherry-picked with authorship preserved. Thanks @ayushcodes10! |
What
_js_local_bound_namescollectedlet/constdeclarator targets, and afor/for-of/for-inloop's own binding, into one function-wide shadow set. Both are block-scoped in JS (unlikevar, 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 genuineindirect_calledge suppressed:const/letdeclared inside a nestedif/try/bare-{}blockfor...of/for...inloop binding, referenced after the loop endsThis is issue #2822's own repro, reproduced directly against current
v8before 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 collectsvar— distinguished by checking whether avariable_declarator's parent isvariable_declaration(var) rather thanlexical_declaration(let/const), and afor-in/for-ofloop's own binding only when it usesvar.Every
let/constandfor/for-of/for-inbinding is instead folded intoextra_localsfor exactly its own block or loop subtree — the same mechanism already used forcatchbindings (#2517) — via a new_js_direct_lexical_nameshelper plus two new cases inwalk_calls(forstatement_block/for_statement, andfor_in_statement). A reference genuinely inside that block or loop still correctly shadows, while one outside it resolves. A C-stylefor (let i = ...)loop's own initializer gets the same treatment, since it's scoped to the whole loop the same way afor-ofbinding is.Tests
New file
tests/test_indirect_call_block_scoped_shadow.py, 8 tests:var's continued function-wide reach in the same shapes (block and for-of)for (let ...)loop, asserting onsource_locationsince 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"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