Loans as facts: two Mut loans alive at once through branches, loops, lists, fields, listeners and timers; the owner's write while lent #132
Loading…
Reference in a new issue
No description provided.
Delete branch "loans-as-facts"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Based on
field-study(#131), and contains it: the diff shown is this piece alone.Mut<T>is SAFE-TS's&mut: a parameter declaredMut<T>promises the function that, while it runs, it is the only way to that object. An async function keeps that loan until its promise settles.aliased-mutreported two loans alive at once in three written shapes only (onePromise.all, two statements of one block,.map(async ...)), andstale-across-awaitleaves aMutparameter alone on purpose, because "the loan is this call's". So nothing checked the promise the second rule relies on. Giveneach of these passed
tscstrict,safe-ts check --deny-warningsand oxlint, and loses an update at run time:What this adds
A loan is a fact on the control-flow layer. It starts where
mut(x)is handed to a call and ends where that call's promise is awaited: directly, throughPromise.alland its siblings (over it, or over the list,SetorMapit was put into), through afor...ofthat awaits each, through the local or field it was kept in.aliased-mutnow reports, on some path:x, a part of it, or what holds it, lent again or written by its owner while a loan is alive: a branch that does not await, the loan of the round before in a loop, a promise dropped withvoidor handed to another call, amapcallback (whoever collects the list);on...prop that lends state it does not own; a method that returns while such a loan is alive;Only where the callee really holds the loan across a suspension, which the whole-program pass decides: it writes the loan (or a part, or lends it on) after an
await, a write of its own answer included (doc.title = await fetchTitle(id): the older answer may land on the newer one), or it cannot be named and returns a promise. A callee done with the object before its firstawait, or one that never waits, is no hazard however its calls overlap.Correct code stays silent: one loan at a time (
for...ofwithawait); a chained queue (this.tail = this.tail.then(() => f(mut(this.state)))); an in-flight guard (if (this.saving) return this.saving;) or a flag tested and set before the loan; one object per call; the elements of a list lent one by one; a{ once: true }listener. Quick fix Awaitpfirst where both loans are in one block.stale-across-await's page and this one now say who guarantees what: the first takes aMutparameter to be the call's alone, the second reports every call site it sees that would make that untrue.Evidence
Promise.allthat each write their own answer after waiting). Fixed; the callees it now leaves out are the ones that cannot interleave.safe-ts migrate mut: 105 async functions take aMut, 81 of them write it after a suspension, 267 call sites lend to them, and 32 findings are new, each read:.thenthat touches another field.loan_holders).What turns red on upgrade
aliased-mutis an error, so a project that lends withmut(x)gets the findings above as errors: on that copy, 32 in about 760 files, most of them overlaps that are safe for a reason the code does not show. The way out is to show it: await the first call, test a flag before lending, chain the calls, or lend the part instead of the whole (mut(game.cache)andmut(game.scene)do not overlap).Two findings the old rule gave are gone on purpose: a
Promise.allof calls whose callee is done with the object before it first waits, or never waits.Decided from the numbers
await": the wider one added a cluster where a field is read before theawaitand a packet sent after, with nothing written back.count += 1after theawait) stay in: 1 finding of 54 candidate sites rested on one alone, not worth a second definition.What stays unseen
Promise.all([this.save(), this.save()]), where eachsave()awaits its own loan, overlaps and is not reported.letor an object literal, or awaited through a flag or a helper.out-of-order-responsesstill escapes because its program writes a captured object. Written as a loan, it is reported.Created with Claude Opus 5.5 (implementation and the merge) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.