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)

View File

@@ -15,6 +15,8 @@ The RC system is now functional for lists and boxed values.
- Polymorphic drop function (`lux_drop`)
- Lists, boxed values, strings use RC allocation
- List operations incref shared elements
- **Closures and environments** - RC-managed with automatic cleanup
- **Inline lambda cleanup** - temporary closures freed after use
- **Scope tracking** - compiler tracks RC variable lifetimes
- **Automatic decref at scope exit** - variables are freed when out of scope
- **Memory tracking** - debug mode reports allocs/frees at program exit
@@ -27,7 +29,6 @@ The RC system is now functional for lists and boxed values.
### What's NOT Yet Implemented
- Early return handling (decref before return in nested scopes)
- Conditional branch handling (complex if/else patterns)
- Closure RC (environments still leak)
- ADT RC
## The Problem
@@ -403,10 +404,10 @@ Rust's ownership system is fundamentally different:
#### Phase A: Complete Coverage (Prevent All Leaks)
1. **Closure RC** - Environments should be RC-managed
- Allocate env with `lux_rc_alloc`
- Drop env when closure is dropped
- ~50 lines in `emit_lambda`
1. ~~**Closure RC**~~ ✅ DONE - Environments are now RC-managed
- Closures allocated with `lux_rc_alloc(sizeof(LuxClosure), LUX_TAG_CLOSURE)`
- Environments allocated with `lux_rc_alloc(sizeof(LuxEnv_N), LUX_TAG_ENV)`
- Inline lambdas freed after use in List operations
2. **ADT RC** - Algebraic data types with heap fields
- Track which variants contain RC fields
@@ -444,17 +445,17 @@ Rust's ownership system is fundamentally different:
### Estimated Effort
| Phase | Description | Lines | Priority |
|-------|-------------|-------|----------|
| A1 | Closure RC | ~50 | P0 - Closures leak |
| A2 | ADT RC | ~100 | P1 - ADTs leak |
| A3 | Early returns | ~30 | P1 - Edge cases |
| A4 | Conditionals | ~50 | P2 - Uncommon |
| B1 | Last-use opt | ~200 | P3 - Performance |
| B2 | Reuse (FBIP) | ~300 | P3 - Performance |
| B3 | Drop special | ~100 | P3 - Performance |
| Phase | Description | Lines | Priority | Status |
|-------|-------------|-------|----------|--------|
| A1 | Closure RC | ~50 | P0 | ✅ Done |
| A2 | ADT RC | ~100 | P1 - ADTs leak | Pending |
| A3 | Early returns | ~30 | P1 - Edge cases | Pending |
| A4 | Conditionals | ~50 | P2 - Uncommon | Pending |
| B1 | Last-use opt | ~200 | P3 - Performance | Pending |
| B2 | Reuse (FBIP) | ~300 | P3 - Performance | Pending |
| B3 | Drop special | ~100 | P3 - Performance | Pending |
**Phase A total: ~230 lines** - Gets us to "no leaks"
**Phase A remaining: ~180 lines** - Gets us to "no leaks"
**Phase B total: ~600 lines** - Gets us to Koka-level performance
### Cycle Detection