Files
AILang/examples/rc_pin_recurse_implicit.ailx
T
Brummel e8c6e99a87 fix: 18d.4 Iter A — gate arm-close pattern-binder dec on scrutinee mode
Iter B (Own-param dec at fn return) gates on param_modes[i] ==
ParamMode::Own and skips Implicit/Borrow because those modes carry
no caller-handed-off-ownership signal. Iter A (arm-close pattern-
binder dec) is the same shape — pattern-binders loaded out of a
scrutinee owe their drop-validity to the scrutinee's ownership —
but Iter A had no such gate and fired on every ptr-typed binder
with consume_count == 0.

Concrete failure (regression test): pin (params t) is Implicit,
caller loop holds t and re-passes it to itself. pin's (TNode v l r)
arm dec'd l and r, fragmenting the tree the caller still references.
Next recursion tripped ailang_rc_dec underflow.

Fix: thread current_param_modes onto the emitter, set it in
emit_fn from the fn type's param_modes (and reset/save it across
lambda thunk emission). In lower_match, derive scrutinee_is_owned
from the scrutinee's mode (Own only; let-binders and temps are
treated as owned) and skip Iter A when not owned.

Carve-out: a let-alias of a non-Own param is not yet detected;
the regression doesn't trigger it and a propagation pass belongs
in its own iter. Recorded in JOURNAL.

Red: alloc_rc_pattern_bind_in_implicit_fn_does_not_dec_borrowed_children.
Pre-fix the rc binary aborted with refcount underflow; post-fix
matches alloc=gc ("0").
2026-05-08 13:56:17 +02:00

56 lines
1.6 KiB
Plaintext

; Iter 18d.4 regression fixture: under --alloc=rc, codegen's
; "pattern-binder dec at arm close" (Iter A) was firing on
; pattern-bound pointer fields whose enclosing fn is Implicit-
; mode. Result: a free of memory the caller still references.
;
; Symptom under --alloc=rc before the fix:
; - Implicit-mode pin/loop: refcount underflow at runtime.
; - Same code under --alloc=gc: clean exit, prints `0`.
;
; Pattern shape: a heap-allocated value `t` shared between a
; callee that pattern-destructures it (pin) and a recursive
; call that re-passes it (loop). Pattern arm binds the children
; (l, r) without consuming them — pre-fix, codegen emitted a
; drop on each, freeing memory still referenced via the outer
; let-binder `t`.
(module rc_pin_recurse_implicit
(data Tree
(ctor TLeaf)
(ctor TNode (con Int) (con Tree) (con Tree)))
(fn build
(type (fn-type (params (con Int)) (ret (con Tree))))
(params d)
(body
(if (app == d 0)
(term-ctor Tree TLeaf)
(term-ctor Tree TNode 1
(app build (app - d 1))
(app build (app - d 1))))))
(fn pin
(type (fn-type (params (con Tree)) (ret (con Int))))
(params t)
(body
(match t
(case (pat-ctor TLeaf) 0)
(case (pat-ctor TNode v l r) 1))))
(fn loop
(type (fn-type (params (con Int) (con Tree)) (ret (con Int))))
(params n t)
(body
(if (app == n 0)
0
(let _v (app pin t)
(app loop (app - n 1) t)))))
(fn main
(type (fn-type (params) (ret (con Unit)) (effects IO)))
(params)
(body
(let t (app build 2)
(do io/print_int (app loop 3 t))))))