Skip to content

issue-2088 plan: round-10 shadow fail-safe in condition 4's identifier resolution narrows recall for this-free shadowing handlers #2625

Open

Description

@carlos-alm

What

docs/plans/issue-2088.md's escape analysis (condition 4 of computeObjectLiteralSiteEscapes, WU-2) resolves an identifier-valued pair or shorthand object-literal property to the same-file function it names via resolveIdentifierValueThisReference → findTopLevelFunctionNodeByName, which only ever searches the module root's direct children.

Round 10 (PR #2612) fixed a soundness bug (Greptile-flagged as "Shadowed handler resolves incorrectly"): when a function- or block-scoped object literal uses an identifier/shorthand property whose name is also declared at module level, findTopLevelFunctionNodeByName resolved to that unrelated module-level declaration with full confidence — even though the name is actually shadowed, at the object literal's own lexical position, by a closer, non-module declaration. A this-using shadowing handler could consequently be classified this-free, making T1 exclusive with no evidence for an invoked sibling property and reporting it dead.

The fix makes resolveIdentifierValueThisReference check findDeclaringScopeNode(objectNode, name) first and fail safe (treat the property as potentially this-using) whenever the name is shadowed by any scope strictly between the object literal and the module root — without attempting to resolve the identifier into that shadowing scope's own declaration and check whether that declaration itself uses this.

This closes the soundness hole, but it is strictly more conservative than necessary: a shadowing declaration that is itself genuinely this-free would, under a fuller fix, still allow T1 to fire; under this fix it does not — the site always escapes and falls back to T2 once any shadowing is detected, regardless of what the shadowing declaration's body actually contains.

Why this wasn't fixed with full resolution directly

findTopLevelFunctionNodeByName's search shape (module-level function_declaration or variable_declarator children only) cannot be reused as-is against an arbitrary block/function scope — a more general traversal would be needed, adding a second AST-search shape to a soundness-critical resolution chain this same plan has already found bugs in across rounds 7, 8, and 9 — and doing so identically in both the TypeScript and Rust engines. Fail-safe was chosen instead: it closes the confidently-wrong-answer hazard immediately, with no new traversal shape to get wrong a sixth time, consistent with how every other unresolved-identifier case in this function is already handled (an import, a non-function binding, or a declaration nested past the module-level-only search all already fail safe rather than attempt deeper resolution).

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

Generalise findTopLevelFunctionNodeByName's declaration-shape matching (a function_declaration, or a variable_declarator whose value is an arrow_function/function_expression/function) into a helper parameterised on which node's direct children to search, then call it against findDeclaringScopeNode(objectNode, name)'s result (when non-null) instead of unconditionally failing safe on it. Add a WU-10 escape-fallback case proving a shadowing declaration that is itself this-free correlates once implemented, without weakening the round-10 fail-safe default for a shadowing declaration that is not provably safe.

Where

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