Files
AILang/examples/rc_app_let_partial_drop_leak.prose.txt
T
Brummel 8375eb81ed prose: split reuse-as keyword + inline single-use lets (iter 20b polish)
reuse-as: render `(reuse-as src body)` as `reuse src as body` with
adaptive braces — inline for single-line bodies (the 99% Ctor case),
braced for multi-line (Let, Seq, nested Match). Reads as English
subject-verb-object instead of compound keyword + always-block.

let-inlining: `let x = rhs; <body using x once>` collapses to
`<body[x := rhs]>` when (a) rhs is not a `Term::Do` (keep effect
sequencing visible), (b) x is used exactly once (no work duplication),
(c) rhs renders single-line AND ≤ 40 chars (no line smearing). Lossy
projection — round-trip mediator sees the original .ail.json and can
re-introduce a let when the mode model demands it.

Helpers `count_free_var` and `subst_var_with_term` are render-local;
both respect inner shadowing (let, lam, match arm pattern binders,
let rec). Pre-render value at current level + check for newline + len
budget; on inline, write_term_prec gets passed parent_prec so the
substituted term still wraps correctly inside binops.

Snapshot drift: rc_app_let_partial_drop_leak.prose.txt — boilerplate
`let p = build_pair(1)` collapsed into the match scrutinee. Improves
readability. Cons-tower in rc_own_param_drop kept its let thanks to
the length budget (52 chars > 40).

Tests: 59 unit (+ 9 new) + 4 snapshot, all green. Coverage:
inline-single-use, two-uses-keeps, zero-uses-keeps, Do-rhs-keeps,
multiline-rhs-keeps, length-budget-keeps, shadowing-not-counted,
reuse-as inline + braces.
2026-05-08 23:03:10 +02:00

25 lines
535 B
Plaintext

// module rc_app_let_partial_drop_leak
data Wrap = MkWrap(Int)
data Cell = MkCell(Wrap, Wrap)
data Pair = MkPair(Cell, Cell)
fn build_pair(n: Int) -> own Pair {
MkPair(MkCell(MkWrap(n), MkWrap(2)), MkCell(MkWrap(3), MkWrap(4)))
}
// @suppress over-strict-mode: RC codegen test: shape used to feed App-bound let-close partial-drop into use_cell
fn use_cell(c: own Cell) -> Int {
match c {
MkCell(w1, w2) => 1
}
}
fn main() -> Unit with IO {
do io/print_int(match build_pair(1) {
MkPair(a, _) => use_cell(a)
})
}