Skip to content

FlatExpression: track nested-lambda closure parameter usage during flattening#545

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-map-parameter-usage-closures
Draft

FlatExpression: track nested-lambda closure parameter usage during flattening#545
Copilot wants to merge 4 commits intomasterfrom
copilot/fix-map-parameter-usage-closures

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 30, 2026

FlatExpression already indexed structural nodes (lambdas/blocks/gotos/labels/try), but did not record per-lambda captured parameter usage needed for closure-aware nested-lambda handling. This PR adds closure-usage metadata at construction time so nested lambda capture information is available without a separate traversal.

  • New closure-usage metadata in ExprTree

    • Added LambdaClosureParameterUsage (LambdaNodeIndex, ParameterId).
    • Added ExprTree.LambdaClosureParameterUsages to store captured-parameter mappings per lambda node.
    • Uses FE-style short indexes and struct layout packing for compact storage.
  • Capture analysis wired into FromExpression lambda construction

    • During builder processing of each ExpressionType.Lambda, collect parameters referenced by the lambda body that are:
      • not declared as that lambda’s parameters, and
      • not declared as local block/catch variables in that lambda scope.
    • Nested lambdas are intentionally excluded from the parent’s capture walk (each lambda is analyzed independently when visited).
    • Collector uses reference-based hash sets for O(1) membership checks while preserving reference-identity semantics of ParameterExpression.
  • Focused unit coverage

    • Added tests for:
      • mapping outer parameter capture to nested lambda node,
      • excluding nested-lambda locals from closure usage mapping.
[StructLayout(LayoutKind.Sequential, Pack = 2)]
public readonly struct LambdaClosureParameterUsage
{
    public readonly short LambdaNodeIndex;
    public readonly short ParameterId;
}

Copilot AI changed the title [WIP] Fix FlatExpression: map parameter usage for closures in nested lambdas FlatExpression: track nested-lambda closure parameter usage during flattening Apr 30, 2026
Copilot AI requested a review from dadhi April 30, 2026 06:18
@dadhi
Copy link
Copy Markdown
Owner

dadhi commented Apr 30, 2026

@copilot we are using short indexes in FE no need for int, apply stuct layout too.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 30, 2026

@copilot we are using short indexes in FE no need for int, apply stuct layout too.

Updated in commit b678694: switched LambdaClosureParameterUsage indices to short, applied struct layout (Sequential, Pack=2), and adjusted short conversions/usages accordingly.

Copy link
Copy Markdown
Owner

@dadhi dadhi left a comment

Choose a reason for hiding this comment

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

There are warnings and errors to fix in ci:

Warning: /home/runner/work/FastExpressionCompiler/FastExpressionCompiler/test/FastExpressionCompiler.IssueTests/Issue500_IndexOutOfRangeException_with_value_objects_implicit_conversions.cs(27,85): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. [/home/runner/work/FastExpressionCompiler/FastExpressionCompiler/test/FastExpressionCompiler.IssueTests/FastExpressionCompiler.IssueTests.csproj::TargetFramework=net8.0]
Warning: /home/runner/work/FastExpressionCompiler/FastExpressionCompiler/test/FastExpressionCompiler.IssueTests/Issue500_IndexOutOfRangeException_with_value_objects_implicit_conversions.cs(67,80): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. [/home/runner/work/FastExpressionCompiler/FastExpressionCompiler/test/FastExpressionCompiler.IssueTests/FastExpressionCompiler.IssueTests.csproj::TargetFramework=net8.0]
Error: /home/runner/work/FastExpressionCompiler/FastExpressionCompiler/src/FastExpressionCompiler.LightExpression/FlatExpression.cs(58,12): error CS1591: Missing XML comment for publicly visible type or member 'LambdaClosureParameterUsage.LambdaClosureParameterUsage(short, short)' [/home/runner/work/FastExpressionCompiler/FastExpressionCompiler/src/FastExpressionCompiler.LightExpression/FastExpressionCompiler.LightExpression.csproj::TargetFramework=net472]

@dadhi
Copy link
Copy Markdown
Owner

dadhi commented Apr 30, 2026

@codex please setup env and proceed work on it

@chatgpt-codex-connector
Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@dadhi
Copy link
Copy Markdown
Owner

dadhi commented Apr 30, 2026

@codex Env is created

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.

FlatExpression: map parameter usage for closures in nested lambdas

2 participants