feat: implement ADT RC - pointer fields in algebraic data types

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>
This commit is contained in:
2026-02-14 13:24:21 -05:00
parent 397c633d51
commit 5098104aaf
4 changed files with 204 additions and 12 deletions

View File

@@ -295,8 +295,8 @@ Inspired by Perceus (Koka), our RC system:
-**Scope tracking** - compiler tracks RC variable lifetimes
-**Automatic decref at scope exit** - verified leak-free
-**Closure RC** - closures and environments are RC-managed
-**ADT RC** - pointer fields in ADTs are RC-managed
- ⏳ Early return handling (decref before nested returns)
- ⏳ ADT RC (algebraic data types)
- ⏳ Last-use optimization / reuse (FBIP)
See [docs/REFERENCE_COUNTING.md](REFERENCE_COUNTING.md) for details.