Skip to content

Applying a function does not rename the enclosing function's parameters inside a nested block - #656

Closed
enumeratio wants to merge 1 commit into
cortex-js:mainfrom
enumeratio:apply-capture-rename
Closed

enumeratio wants to merge 1 commit into
cortex-js:mainfrom
enumeratio:apply-capture-rename

Conversation

@enumeratio

Copy link
Copy Markdown
Contributor

When the inner function v -> body was applied to a value that mentions a parameter acc of the enclosing function, the capture check counted the acc entry that a nested Block holds (because its body reads acc) as a binder of that block. It renamed acc to acc_1 there, and nothing substituted the renamed occurrence, so acc_1 (and k_1) leaked into the result: a Fold whose step applies such a typed let gave [5, At(acc_1, 2), At(acc_1_1, 3)], not [5, 6, 7].

parameterSubBinds now does not count a scope entry that holds the binding of a parameter of an enclosing function literal as a binder. A Block still binds its own locals, and a nested literal still binds its own parameters.

Kept: renaming of a real binder whose name is free in the value (y -> (x -> y^x) applied to x still gives x_1 -> x^x_1), and a user symbol named acc_1 is left alone.

Tests: test/compute-engine/apply-capture-rename.test.ts covers the repro, the bare Apply variant, a Filter in place of the Map, two levels of nesting, with and without Typed, through evaluate() and .N(), and a declared acc_1. The existing apply, fold, reduce, substitution and closure tests pass unchanged.

Fixes #655

arnog added a commit that referenced this pull request Nov 22, 2026
…ther argument mentions the same name (#632, #655; PRs #633 and #656 by enumeratio)

The scope of a nested Block holds the binding of each enclosing parameter
its body reads. parameterSubBinds took that entry for a binder of the
Block, so the capture check renamed the parameter there (a_1, acc_1) and
nothing substituted the renamed occurrence. An entry that holds the binding
of a function-literal parameter is no longer a binder of the Block; the
literal binds it at its own Function node.

PRs #633 and #656 make the same fix. This lands the source of #633 and the
tests of both.

Co-authored-by: enumeratio <314378874+enumeratio@users.noreply.github.com>
@arnog arnog closed this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Applying a function that holds a nested function leaks renamed parameters (acc_1) of the enclosing function

2 participants