feat: implement closure RC - environments are now memory-managed

Closures and their environments are now properly reference-counted:

- Allocate closures with lux_rc_alloc(sizeof(LuxClosure), LUX_TAG_CLOSURE)
- Allocate environments with lux_rc_alloc(sizeof(LuxEnv_N), LUX_TAG_ENV)
- Enable Lambda in expr_creates_rc_value() to track closure variables
- Add lux_decref() after List higher-order operations (map, filter, fold,
  find, any, all) to clean up inline lambdas

Test results:
- Closure test: [RC] No leaks: 8 allocs, 8 frees
- List RC test: [RC] No leaks: 31 allocs, 31 frees
- All 263 tests pass

Remaining for full memory safety: ADT RC, early returns, conditionals.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 13:12:40 -05:00
parent b2f4beeaa2
commit c68694294b
3 changed files with 51 additions and 24 deletions

View File

@@ -218,9 +218,9 @@ Koka also compiles to C with algebraic effects. Key differences:
| Aspect | Koka | Lux (current) |
|--------|------|---------------|
| Memory | Perceus RC (full) | Scope-based RC (lists/boxed) |
| Memory | Perceus RC (full) | Scope-based RC (lists/closures) |
| Effects | Evidence passing (zero-cost) | Evidence passing (zero-cost) |
| Closures | Environment vectors | Heap-allocated structs (leak) |
| Closures | Environment vectors | Heap-allocated structs (RC) |
| Reuse (FBIP) | Yes | Not yet |
| Maturity | Production-ready | Experimental |
@@ -294,8 +294,8 @@ Inspired by Perceus (Koka), our RC system:
- ✅ Dynamic strings use RC allocation
-**Scope tracking** - compiler tracks RC variable lifetimes
-**Automatic decref at scope exit** - verified leak-free
-**Closure RC** - closures and environments are RC-managed
- ⏳ Early return handling (decref before nested returns)
- ⏳ Closure RC (environments still leak)
- ⏳ ADT RC (algebraic data types)
- ⏳ Last-use optimization / reuse (FBIP)