Skip to content

fix(typescript): resolve calls through immutable local const aliases - #5622

Closed
Komorebi-GZM wants to merge 6 commits into
Graphify-Labs:v8from
Komorebi-GZM:fix/ts-const-alias-call-edges
Closed

Komorebi-GZM wants to merge 6 commits into
Graphify-Labs:v8from
Komorebi-GZM:fix/ts-const-alias-call-edges

Conversation

@Komorebi-GZM

Copy link
Copy Markdown
Contributor

Fixes #5613.

Root cause

Inside an indexed TS scoped callable, a call through a local const alias (const persist = save; persist()) enters the TS local-binding arm of the call-edge resolver. _js_receiver_binding_scope correctly finds the binding scope for persist, but ts_callable_scopes.bindings only registers function-valued consts and function declarations, so the lookup returns None. The raw_calls fallback requires ts_local_binding is None, so the call is dropped entirely — no edge, no raw_call.

Fix

  • New _ts_immutable_alias_target(scope, name, source): one-hop follow of const name = <bare identifier> inside the binding scope. Deterministic AST-only lookup, no environment or ordering dependence.
  • At the call site, when the direct bindings lookup misses, follow the alias and re-resolve the aliased name exactly like the bare-call path: nearest lexical scope via lexical_nids_by_scope/scope_parents, then the file-wide label map, then raw_calls (which resolves imports cross-file). Non-const (reassignable) bindings and self-referential aliases stay unresolved, preserving the existing fail-closed rule.

Provenance is preserved: the emitted edge keeps the scoped callable as caller and the real alias call's location (L6 in the issue's repro) as source_location.

Testing

New tests/test_ts_const_alias_calls.py (red before the fix, green after — run in that order):

  • the issue's exact repro: prepare() → save() at L6 and control() → save() at L8;
  • same-file alias target resolving through the lexical walk;
  • mutable let alias stays fail-closed (no fabricated edge to either candidate).

Full targeted suites (test_ts_factory_call_ownership, test_extract, indirect-call shadow suites, TS receiver/member/new-expression suites, etc.): 519 passed, 4 skipped, 1 xfailed. A broad -k "ts or ... call ..." sweep shows only failures that reproduce identically on unmodified v8 (baseline: 84 → with fix: 82, the delta being the 3 new passing tests), so no regressions.

…raphify-Labs#5613)

Inside an indexed TS scoped callable, a call through a const alias
(`const persist = save; persist()`) hit the local-binding arm, failed
the bindings lookup (the alias is not a function-valued const), and was
dropped entirely: raw_calls requires ts_local_binding to be None.

Follow the alias one hop when it is an immutable const initialized to a
bare identifier, re-resolving the aliased name through the nearest
lexical scope, then the file-wide label map, then raw_calls. The emitted
edge keeps the caller as the scoped callable and the real alias call
site as its location. Reassigned (`let`) or self-referential bindings
stay unresolved, matching the existing fail-closed rule.

Regressions prove the old failure first: the issue's repro asserted
prepare() -> save() (L6) is absent before the fix, alongside a same-file
alias case and a mutable-let fail-closed guard.
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Komorebi-GZM. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@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 review View in Graphify

Review before merging · Merge confidence 4/5

1 bug · 3 possible issues to check · High impact

The extractor now tries to resolve calls through a one-hop const alias in TypeScript and JavaScript.

P2  A local variable can make an alias call point to an outer function · engine.py:13180

3 possible issues we couldn't confirm

They don't change the score.

Code graph · 4822 functions depend on this change
  • 3 functions became more coupled. extract() (1432 callers), _js_enclosing_bound_names() (4 callers), walk_calls() (1 callers).
  • 58 of 659 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed f4cc2c5 · 6 earlier findings not re-checked

Comment on lines +13149 to +13152
# stays unresolved.
aliased = _ts_immutable_alias_target(ts_local_binding, callee_name, source)
if aliased is not None:
tgt_nid = ts_callable_scopes.bindings.get(ts_local_binding, {}).get(aliased)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  An alias of a parameter can resolve to a same-named global function

A call through a const alias can get a call edge to a global function even when the alias refers to a parameter instead.

Resolve the alias initializer against lexical bindings in _ts_immutable_alias_target.

Evidence · checked against the code

Expected: The call edge should not identify the same-named file-level function when the parameter shadows it.
Actually: If the callable-scope binding lookup has no entry for the aliased name, the fallback can select the file-level label and attribute the call to that function.

  • Independent source review: Inspected the nested-callable scanner, which records nested callable declarations in its binding and lexical-name maps, and the alias branch. Neither establishes that the alias initializer's identifier resolves to the selected file-level function rather than a parameter. The existing tests cover ordinary aliases and local-callable aliases, but do not test a parameter shadowing a same-named file-level function.
  • I did not run a reproduction against the extractor.
  • Also reported as: Calls through a local alias can link to an unrelated outer function with the same name
Prompt to fix with AI
In graphify/extractors/engine.py, _ts_immutable_alias_target (lines 13149-13152):
Problem: An alias of a parameter can resolve to a same-named global function
Expected: The call edge should not identify the same-named file-level function when the parameter shadows it.
Actually: If the callable-scope binding lookup has no entry for the aliased name, the fallback can select the file-level label and attribute the call to that function.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

… local binding

Following an immutable const alias fell through to the file-wide label
map even when the aliased name was bound by an intermediate scope -- e.g.
a parameter shadowing a same-named file-level function -- so the edge
could point at the global it does not name. Gate the label fallback on
the call site's enclosing bound names: a shadowed initializer is not a
known callable, so the call fails closed.

Also strengthen the reassigned-let regression: assert no edge at all
from the reassigned call plus a positive const-alias control in the same
fixture, and add a parameter-shadowing regression (red before the fix).

Co-Authored-By: Claude <[email protected]>
@Komorebi-GZM

Copy link
Copy Markdown
Contributor Author

Fixed the review findings in de22a26:\n\n- P2 (shadowed alias): the const-alias path now only falls back to the file-wide label map when the aliased name is not bound by any enclosing scope (parameter/local); a shadowed initializer fails closed with no edge.\n- Test A: the reassigned-let test now asserts no edge at all from the reassigned call, plus a positive const-alias control in the same fixture so it cannot pass vacuously.\n- Issue B: added a parameter-shadowing-same-named-global regression (fails on the old code, passes now).\n\nTarget suite plus TS/indirect-call/shadow suites and a sampled set of unrelated suites all pass.

Comment thread graphify/extractors/engine.py Outdated
Comment on lines +13164 to +13167
tgt_nid = lexical_nids_by_scope[curr_scope][aliased]
break
curr_scope = scope_parents.get(curr_scope)
if not tgt_nid:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  An outer alias can resolve to an inner shadowing function

A call through an alias captured from an outer scope can point to a same-named function declared inside the calling function.

Start fallback lookup for captured aliases from the alias’s binding scope, not caller_nid.

Evidence · checked against the code

Expected: The call edge identifies the callable named by the alias initializer, or remains unresolved if that target cannot be established.
Actually: The lexical lookup starts at the caller and can select its same-named nested function before reaching the alias's target scope.

  • Independent source review: The parameter-shadow test checks an alias initialized from a parameter and verifies it does not fall through to a file-level function. That does not block the claimed case: a nested function declaration is recorded in lexical_nids_by_scope for the caller's scope, which the alias fallback consults first. No read test covers this nested-function case.
  • Also reported as: A local const alias can call the wrong same-named function when the call is nested in an inner function
Prompt to fix with AI
In graphify/extractors/engine.py, _extract_generic (lines 13164-13167):
Problem: An outer alias can resolve to an inner shadowing function
Expected: The call edge identifies the callable named by the alias initializer, or remains unresolved if that target cannot be established.
Actually: The lexical lookup starts at the caller and can select its same-named nested function before reaching the alias's target scope.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

…g scope

Following an immutable const alias, the lexical fallback walk started at
the caller's scope. When the call sits in an inner function that declares
a same-named nested function, the walk named that nested function instead
of the alias initializer's real target (Graphify-Labs#5622 review, round 2). Start the
walk at the scope where the alias is declared, so the inner caller's
lexical table never participates.

Adds a closure-shadowing regression (red before, green after); the
parameter-shadowing regression from the previous round stays green.
@Komorebi-GZM

Copy link
Copy Markdown
Contributor Author

Addressed the round-2 review finding: the lexical fallback after a const alias now starts at the alias's binding scope instead of the caller's scope, so an inner caller with a same-named nested function can no longer shadow the initializer's real target. Added a closure-shadowing regression test (red before the fix, green after); the parameter-shadowing regression from the previous round still passes.

Comment thread graphify/extractors/engine.py Outdated
Comment on lines +13166 to +13169
# call-site location (#5613).
curr_scope = alias_scope
while curr_scope:
if curr_scope in lexical_nids_by_scope and aliased in lexical_nids_by_scope[curr_scope]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  An aliased call can resolve to the wrong same-named function

When a const alias refers to a function declared in an enclosing scope, the lookup can skip that local function and emit a call edge to a same-named file-level function instead.

Resolve aliases through the enclosing callable's scope in _extract_generic.

Evidence · checked against the code

Expected: The persist() call edge targets the enclosing callable's nested save.
Actually: Alias lookup can fail to traverse from its AST scope to the enclosing callable ID, miss the nested declaration, and fall back to the file-level save.

  • Independent source review: Inspected the existing closure test where persist aliases a file-level save and the inner closure declares another save. That is the reverse arrangement: the expected target is the file-level function, so it does not refute this claim. The inspected tests do not cover an enclosing callable's nested save aliased by a closure when a file-level save also exists.
  • Some lookups asked for files or lines that do not exist; the review was told and carried on.
  • Also reported as: Aliases to callables in an outer scope are left unresolved
Prompt to fix with AI
In graphify/extractors/engine.py, _extract_generic (lines 13166-13169):
Problem: An aliased call can resolve to the wrong same-named function
Expected: The `persist()` call edge targets the enclosing callable's nested `save`.
Actually: Alias lookup can fail to traverse from its AST scope to the enclosing callable ID, miss the nested declaration, and fall back to the file-level `save`.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

…lable's nested declarations

The alias re-resolution climbed scope_parents/lexical_nids_by_scope from
the alias's AST scope node, but those maps are keyed by callable nid, so
the walk never matched and a miss fell through to the file-wide label
map. When the aliased name names a function nested in the enclosing
callable (with a same-named file-level function also present), the call
edge pointed at the wrong function.

Climb the AST scope chain from the alias's own block via
ts_scopes.bindings instead (keyed by AST body block, where nested
declarations are registered), stopping at any parameter/var/let-const
shadow along the way; the label fallback stays gated on the enclosing
bound names.
@Komorebi-GZM

Copy link
Copy Markdown
Contributor Author

Round 3 fix: the const-alias re-resolution climbed scope_parents/lexical_nids_by_scope starting from the alias's AST scope node, but those maps are keyed by callable nid — so the walk never matched, and a miss fell through to the file-wide label map. When the aliased name refers to a function declared nested inside the enclosing callable (with a same-named file-level function also present), the call edge landed on the file-level function.

The fix climbs the AST scope chain outward from the alias's own block via ts_scopes.bindings (keyed by AST body block, where the nested-callable scanner registers declarations), stopping at any parameter/var/let-const shadow on the way; the fail-closed gate before the file-wide label fallback is unchanged. Combined with the two earlier rounds, alias resolution now covers shadowed initializers (round 1), starts from the alias's binding scope rather than the caller's (round 2), and reaches targets nested in enclosing callables (round 3). Adds a red→green regression test; all prior alias tests and the TS/JS suites show no new failures vs. the pre-change baseline.

Comment on lines +13177 to +13180
# round 3); the caller-nid climb would miss it
# and the file-wide label map would name an
# unrelated same-named module function.
shadowed = False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  A local variable can make an alias call point to an outer function

In TypeScript, const persist = save; persist() can be recorded as a call to an enclosing save() even when a local const save shadows it.

Check the alias’s own scope for shadowing declarations before climbing in walk_calls.

Evidence · checked against the code

Expected: The alias call should not resolve to the enclosing save function because the initializer refers to the block-local value.
Actually: Alias resolution skips its own scope when searching for save, so it can select the enclosing callable and emit a false call edge.

  • Independent source review: The existing alias tests cover parameter shadowing and an alias declared outside a caller that has a same-named nested function. They do not cover a non-callable let/const in the alias’s own scope shadowing an enclosing callable; the implementation’s ancestor walk omits that scope.
  • No test was run; the supplied tests do not cover a non-callable lexical binding in the alias’s own scope shadowing an enclosing callable.
  • Also reported as: Alias lookup skips the alias's own scope, so a local non-function save can resolve to an outer function named save
Prompt to fix with AI
In graphify/extractors/engine.py, walk_calls (lines 13177-13180):
Problem: A local variable can make an alias call point to an outer function
Expected: The alias call should not resolve to the enclosing `save` function because the initializer refers to the block-local value.
Actually: Alias resolution skips its own scope when searching for `save`, so it can select the enclosing callable and emit a false call edge.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

…ng before the alias re-resolution climb

The const-alias re-resolution started its outward shadow walk at the
alias's parent scope, so a non-callable local (`const save = someValue`)
declared beside the alias itself was never seen. When the aliased name
also resolves to a callable through the enclosing callable's nested
declarations, the climb named that callable even though the initializer
never refers to it. The alias's own scope is now checked with the same
per-scope shadow logic the climb applies (parameter/var via
_js_local_bound_names, let/const via _js_direct_lexical_names), and an
outer binding is skipped when that check shadows the name, failing
closed (Graphify-Labs#5622 round 4).
@Komorebi-GZM

Copy link
Copy Markdown
Contributor Author

Round 4 fix: the const-alias re-resolution's outward shadow walk started at the alias's parent scope, so a non-callable local declared beside the alias itself — e.g. const save = someValue; sitting in the same block as const persist = save;, while the enclosing callable also declares a same-named nested save — was never checked. The climb then resolved through the enclosing callable's nested declaration even though the initializer never refers to that callable, emitting an edge that should have failed closed.

The fix runs the exact same per-scope shadow logic the climb uses (parameters/var via _js_local_bound_names, let/const via _js_direct_lexical_names) against the alias's own scope first — _js_receiver_binding_scope can return either the function-like node or the body block, so both shapes are handled — and skips any outer binding when that check shadows the name, failing closed with no edge.

Red→green proven: test_alias_own_block_local_const_shadow_stays_fail_closed fails on f4cc2c58 (edge emitted to the nested save), passes with the fix; all six prior alias regression tests plus the indirect-call shadow suites pass, and the stash baseline shows only the new test failing pre-fix.


Alias-resolution semantic matrix — four review rounds (#5622)

# Scenario Expected behavior Test
1 Direct const-alias to a callable one hop away (const persist = save; persist()) Edge to the aliased callable, at the real call site's location test_const_alias_call_keeps_owner_and_real_call_site, test_const_alias_to_local_callable_resolves_within_scope
2 Alias whose target is shadowed by a local binding (param/local) in the call's scope chain Fail closed — no edge, no file-wide fallback test_alias_to_param_shadowing_same_named_function_stays_fail_closed
3 Alias declared in an outer scope, called inside an inner closure that declares a same-named nested function Edge to the alias initializer's real target (module-level callable), never the caller's nested function test_alias_call_inside_closure_with_same_named_nested_function
4 Alias in an inner closure whose target is a function nested in the enclosing callable (same-named file-level function also present) Edge to the enclosing callable's nested declaration, not the file-level one test_alias_to_enclosing_callable_nested_declaration
5 Non-callable const declared in the alias's OWN scope shadowing a same-named callable reachable via the enclosing scope (this round) Fail closed — no edge at all test_alias_own_block_local_const_shadow_stays_fail_closed

Mutable let aliases also remain fail-closed (test_mutable_let_alias_stays_fail_closed). Each direction above has a dedicated red→green regression test pinning it; happy to cover any additional scenario reviewers can think of.

The file-wide fallback after a const-alias walk consulted the CALL
node's enclosing bindings, so an outer `const persist = save` called
inside an inner function that declared a same-named parameter or
`let`/`const` failed closed instead of targeting the initializer.
The climb also skipped `catch` / `for-in` bindings, so an alias of a
catch parameter could resolve to an enclosing same-named nested
function.

Fail-closed now follows the alias's own scope chain (including catch
and for-in), matching `_js_enclosing_bound_names`. Adds red→green
regressions for the call-site parameter/const cases and for a catch
binding; prior alias tests stay green.
@safishamsi

Copy link
Copy Markdown
Member

Thanks for this. Closing in favor of #5614 for the const-alias resolution. The issue with the name-rewrite-into-raw_calls approach here is that on the imported target case the rewritten bare name bypasses this PR's own immutability/shadow guards, so it produces false edges for reassigned/redeclared/shadowed aliases — e.g. const alias = save; alias = cb; alias() still links to save, and likewise for redeclaration, a local shadow of the imported name, enum targets, and use-before-declaration. #5614 handles the same imported case and fails closed on all of those. The genuinely new piece here — purely-local function aliases — would be a very welcome follow-up built on #5614's foundation (just not via the bare-name rewrite, since that's where the false edges come from). Appreciate the contribution!

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.

[Bug]: Calls through a local const alias disappear inside indexed TypeScript callables

2 participants