Skip to content

fix: detect captured variable mutation inside closures#326

Merged
cs01 merged 1 commit intomainfrom
fix-closure-inner-mutation-check
Mar 13, 2026
Merged

fix: detect captured variable mutation inside closures#326
cs01 merged 1 commit intomainfrom
fix-closure-inner-mutation-check

Conversation

@cs01
Copy link
Copy Markdown
Owner

@cs01 cs01 commented Mar 13, 2026

Summary

  • arr.forEach((x) => { sum = sum + x; }) where sum is an outer variable was silently producing wrong output (sum stayed 0) because ChadScript captures by value
  • The closure-mutation-checker only detected mutations AFTER capture (x = 2 after const f = () => x), not mutations INSIDE the closure body
  • Fix: pass captured variable names into the arrow body walk so assignments to captured vars inside the closure are flagged as compile errors
  • Users now get a clear error message instead of silently wrong behavior

Test plan

  • New fixture closures/closure-inner-mutation.ts — forEach with captured var mutation triggers compile error
  • All 621 tests pass
  • Self-hosting verification passes (compiler itself doesn't use this pattern)

🤖 Generated with Claude Code

@cs01 cs01 merged commit 8cfd9a7 into main Mar 13, 2026
12 checks passed
@cs01 cs01 deleted the fix-closure-inner-mutation-check branch March 19, 2026 05:23
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.

1 participant