Skip to content

#2606: lexical LET/LAMBDA variable scoping, balanced scope stack - #2607

Merged
JanKallman merged 1 commit into
develop8from
bug/i2606
Nov 16, 2026
Merged

JanKallman merged 1 commit into
develop8from
bug/i2606

Conversation

@swmal

@swmal swmal commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Fix #2606: Lexical scoping for LET/LAMBDA variables

Problem

A named LAMBDA with a LET body could bind a parameter to the value of another LAMBDA call's parameter with the same name. New scopes took their parent from the top of the shared variable storage stack, and that stack was left unbalanced. The bug showed in nested calls (Out(2, In(5))) and when calculating worksheet by worksheet.

Changes

  • Lexical parent at compile time: VariableStorageManager gets BeginCompile/EndCompile/PeekLexical. Scopes created during compilation get their parent from the enclosing LET/LAMBDA or the lambda's own scope, never from leftover runtime scopes.
  • Balanced stack: pushes record the stack depth and restore it afterwards (VariableFunctionExpression.OnDispose, LambdaCalculator.Execute, EndCompile). Blind pops and Clear() calls in CompileExpressions and RpnFormula.Reset are removed.
  • Safety net: top-level entry points in RpnFormulaExecution restore the stack depth on exit.
  • Cleanup: LambdaNameFunction.Execute returns #VALUE! for too many arguments instead of relying on an exception.

Tests

New LambdaVariableScopeTests: issue repros, control cases and stack-balance invariants. The full test suite passes.

@swmal swmal self-assigned this Nov 16, 2026
@swmal swmal added the bug Something isn't working label Nov 16, 2026
@JanKallman
JanKallman merged commit d669833 into develop8 Nov 16, 2026
3 checks passed
@JanKallman
JanKallman deleted the bug/i2606 branch November 16, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LAMBDA with LET reads a same-named parameter from another LAMBDA's call (per-sheet Calculate, and inside one Workbook.Calculate) - 8.7.2

2 participants