[Repo Assist] [Python] Fix TCO context adding unnecessary default params to nested lambdas#4426
Merged
dbrattli merged 4 commits intoMar 21, 2026
Conversation
#3877) When a function is tail-call optimised, all nested functions/lambdas created within its body previously inherited all TCO variables as default parameters (e.g. `_arg: Any=_arg`), even when the nested function doesn't reference those variables at all. Root cause: `transformFunction` computes `tcArgs` from the outer `TailCallOpportunity` in context and appends them unconditionally to every nested function. The capture is needed for closures that *do* reference a mutated TCO variable (Python captures by reference, so defaults bind the current value). But it was applied indiscriminately. Fix: before appending a TCO arg as a default, check whether the function body actually references that variable using `isIdentUsed`. Variables not referenced in the body are excluded from the captured defaults. This eliminates spurious parameters like `_arg: Any=_arg` on lambdas like `fun () -> g()` that don't use the outer TCO variable, and keeps the capture for lambdas like `fun () -> i` that do. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
7 tasks
…re-capture-e64e452bdb8069e0
Contributor
Author
Python Type Checking Results (Pyright)
Excluded files with errors (4 files)These files have known type errors and are excluded from CI. Remove from
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
transformFunctioninFable2Python.Transforms.fsunconditionally appended all TCO args as default parameters to every nested lambda, regardless of whether the lambda body references those variablesisIdentUsed name bodybefore adding them as defaultsTest plan
test Passing a function reference in a TCO context does not capture unneeded outer argsadded totests/Python/TestTailCall.fs./build.sh test python --skip-fable-libraryto verify🤖 Generated with [Claude Code]((claude.com/redacted)