- Add pop_rc_scope_except() to skip decref'ing returned variables
- Block expressions now properly preserve returned RC variables
- Function returns skip cleanup for variables being returned
- Track function return types for call expression type inference
- Function calls returning RC types now register for cleanup
- Fix main() entry point to call main_lux() when present
Test result: [RC] No leaks: 17 allocs, 17 frees
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
ADT values with pointer fields (like recursive Tree types) now properly
manage memory:
- Assign unique type tags (starting at 100) to each ADT type
- Track which ADTs have pointer fields that need cleanup
- Generate lux_drop_adt() function with per-ADT drop logic
- Allocate ADT pointer fields with lux_rc_alloc instead of malloc
- Track ADT variables with pointer fields in scope
- Emit field cleanup code at scope exit (switch on tag, decref fields)
Test results:
- ADT test: [RC] No leaks: 6 allocs, 6 frees
- List test: [RC] No leaks: 31 allocs, 31 frees
- Closure test: [RC] No leaks: 8 allocs, 8 frees
- All 263 tests pass
Remaining: early returns, complex conditionals.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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>
- C_BACKEND.md: Update memory management from "Leaks" to "Scope-based RC",
update comparison tables with Koka/Rust/Zig/Go
- LANGUAGE_COMPARISON.md: Add status column to gap tables, add RC row
- OVERVIEW.md: Add C backend RC to completed features, update limitations
- REFERENCE_COUNTING.md: Add "Path to Koka/Rust Parity" section with:
- What we have vs what Koka/Rust have
- Remaining work for full memory safety (~230 lines)
- Performance optimizations for Koka parity (~600 lines)
- Cycle detection strategy
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add scope tracking for reference-counted variables in the C backend:
- Add RcVariable struct and rc_scopes stack to CBackend
- Track RC variables when assigned in let bindings
- Emit lux_decref() calls when scopes exit (functions, blocks)
- Add memory tracking counters (alloc/free) for leak detection
- Fix List.filter to incref elements before copying (prevents double-free)
- Handle return values by incref/decref to keep them alive through cleanup
The RC system now properly frees memory at scope exit. Verified with
test showing "[RC] No leaks: 28 allocs, 28 frees".
Remaining work: early returns, complex conditionals, closures, ADTs.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implements Phase 1-3 of the RC system for automatic memory management:
- Add LuxRcHeader with refcount and type tag for all heap objects
- Add lux_rc_alloc, lux_incref, lux_decref, and lux_drop functions
- Update list allocation to use RC (lux_list_new uses lux_rc_alloc)
- List operations (concat, reverse, take, drop) now incref shared elements
- Update boxing functions (box_int, box_bool, box_float) to use RC
- String operations (concat, int_to_string, readLine) return RC strings
- File and HTTP operations return RC-managed strings
The infrastructure is ready for automatic decref insertion at scope exit
(Phase 4) and closure RC (Phase 5) in future work.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>