This commit addresses two critical issues in the interpreter's match
expression handling:
1. Stack corruption in match expressions: The scrutinee's stack-allocated
header was being corrupted when pattern match bindings allocated new
stack space, reusing the same memory. Fixed by using pushCopy to
allocate a fresh stack location for the scrutinee, protecting it from
corruption during pattern matching.
2. Missing tuple element cleanup: StackValue.decref had no case for
.tuple layout tags, causing it to skip cleanup entirely. This meant
refcounted elements inside tuples (lists, strings, boxes, etc.) were
never being decref'd. Added proper tuple decref logic that iterates
through tuple elements and decrefs each one, similar to record handling.
The "match list pattern destructures" test now passes, though there are
still memory leaks from list allocations that need further investigation.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
The test was crashing with an integer overflow during memory deallocation.
Root cause:
1. The scrutinee (the list [1, 2, 3]) was evaluated and its RocList header
was stored on the interpreter's stack
2. During pattern matching ([a, b, c]), new stack allocations for bindings
reused the same memory region
3. The RocList header was corrupted by subsequent stack allocations
4. When the deferred scrutinee.decref tried to free the list, it read
garbage from the corrupted header
5. This led to attempting to deallocate memory with size 0, causing integer
overflow in old_memory.len - 1
Solution:
Changed the match expression handler to make a copy of the scrutinee before
pattern matching. The pushCopy call allocates a new stack location for the
RocList header and increments the refcount of the underlying data. This
protects the header from being corrupted by subsequent stack allocations
during pattern matching.
Result:
- Before: Test crashed with integer overflow (signal 6)
- After: Test passes successfully (with a minor memory leak)
- Overall: 262/287 tests passing (91.3% pass rate)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>