State reached past the borrow rules: a loan pending when a function throws, a function property replaced through a loan, module state reached through a part or a function #143
Loading…
Reference in a new issue
No description provided.
Delete branch "state-reached"
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?
Three ways to change state that the borrow rules did not follow. Each of these passed
tscstrict,safe-ts check --idiomatic --deny-warningsand oxlint:What changes
aliased-mutasks about a way out by an exception. A loan whose promise is not yet awaited was followed to everyreturn; now also to athrow, a call that can throw and anawaitthat can reject before the loan's own. Which calls can throw is decided by the whole-program pass, the same way #140 does it forunreleased-resource. It was the one analysis on the control-flow layer without exception exits.covariant-aliasreports a function property lent as one that needs more. A value lent withmut(...)as a type in which a writable property of function type takes a narrower parameter, or returns a wider result, than the same property of the value's own type. The state guide listed this as a known hole ofMut: a type cannot tell a property holding a function from a method. The checker can, because it reads the syntax. Areadonlyproperty and a method written as a method are not reported.no-mutable-globalfollows what reaches module state. Destructuring, a list or an object made in place that holds the state, a shallow copy (its parts are still the module's), a loop or a callback over such a list, and a function, an arrow, a getter that hands the state or a part of it back, in the same file or another. It uses the reach and hand-out machinery the borrow rules use for a parameter.Evidence
awaitthat rejects first, aThrowsslot, a throwing callback run right away, a built-in that throws, acatchthat returns). 5 fine forms are silent (a call that cannot fail, the loan awaited in afinally, the throw before the loan).tsc.no-mutable-globalfindings, both in one benchmark script that writes an element of a module-level list; real by the rule's definition, low stakes. On a copy migrated tomut(...), the loan and function-property changes add nothing (the implementer's measurement).mut-function-field-replacedis closed and comes off the list. Two classes added and closed (async-loan-exception,module-state-reached). 16 of 110 escape.What turns red on upgrade
no-mutable-globalreports changes of module state it did not follow before. Pass the state down, or keep it in something created once per use, as the rule's page says.aliased-mutmay report a loan left pending by a throw. Await the loan in afinally, or before the call that can throw.covariant-aliasreports a function property lent as one that needs more. Make the propertyreadonly, or lend the value as the type it is.What stays unseen
?:(on ? state : undefined), and state passed on through a function kept in a local.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
What was open (review of 7b2d3f74's parent): with `import { q as ask }` and `import * as R` of the same module in one file, `renamed_imports` made the first `q`, so the namespace member `R.q` was no longer read as a name (a binding called `q` existed) and the leak past `R.q(parse(n))` went silent, where the starting commit reported it. Findings also quoted the call as `R(...)`. What closes it: a namespace member of a SAFE-TS module that the file also imports by that name is read as that binding (no import added), and a copy of it (`const take = R.q`) names that binding too; messages quote the callee as written, dots included. The harness rewrites one site at a time and can't write the two imports together: fixtures for it in `unreleased_resource` and `q_undeclared_error`. Two by-design lines for `R?.q(...)` and `const RCopy1 = R; RCopy1.q(...)`, which the transform doesn't rewrite (checked with `safe-ts emit`); `miss no-mutable-global callee-const` closes as a side effect. Co-Authored-By: Claude Opus 5.5 <[email protected]>Two ways to change data that is not the function's to change passed every gate. A shallow copy of a borrowed parameter (`{ ...order }`, `Object.assign({}, order)`, a rest, a list with a spread among other elements, a copy a helper hands back) is a new object whose parts are still the caller's. `no-param-mutation` followed only a list made of one spread. It now follows the others: a change of a part through the copy is reported, and so is lending the part. A whole copy lent to a `Mut` parameter is reported where the callee could write something the copy still shares: a field the copy took, that something can be written through as far as its type is written, that the callee's parameter type has, and that the callee names (a function, a static method, or a method no override can replace). TypeScript lets `readonly` be dropped by any annotation, so a frozen object can be handed around as writable and a write to it throws when it runs. New rule `readonly-dropped` (error, with a quick fix): a value whose written type has a `readonly` property given a place that can write it, with types opened across files. Soundness corpus: `shallow-copy-shares-object` and `spread-list-shares-elements` added and closed. Contains the ways-out piece it was merged with: the rewrite list and the work ceilings are those of the merged tree. Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>Open: `{ onEvent: (a: Animal) => string }` lent as `Mut<{ onEvent: (d: Dog) => string }>` passed every gate: TypeScript checks a function property one way, and Mut can't tell such a property from a method (methods must be compared one way so subclasses lend). The callee installed a handler needing a dog, the owner called it with any animal. docs/guide/state.md listed it as a hole of Mut; the corpus class mut-function-field-replaced escaped. Closed: the checker reads the syntax, which tells them apart. cache::Shape::Function now carries the written signature (cache::Sig: each parameter's and the result's shape and span); each file lists the values it lends (`mut(x)`, a Mut value passed on, a const holding either) with their written type (readonly::Lend, from covariance.rs's value types), and each function's Mut parameters keep the shape they lend (loans::Signature::lent_shapes). The whole-program pass, where missing-mut already resolves the call, compares a writable function property of the callee's type with the value's (readonly.rs, through aliases, interfaces, intersections and Mut<...>, across files): a narrower parameter or a wider result is covariant-alias. A readonly property, a method written as a method and a class's method aren't replaced through the loan; what either side can't be read as is silent. A rule of covariant-alias's: the hazard (a write through a differently typed writable view lands where readers expect the other type) and the fix (readonly on the callee's side, or the same type) are its own. Corpus: mut-function-field-replaced closed by this rule alone. On an adopter, as is and migrated: nothing new. Work guard: +2 memo questions, +0.4% allocations (the lends each file lists): ceilings regenerated. Co-Authored-By: Claude Opus 5.5 <[email protected]>Open: no-mutable-global followed a member of module state and a part kept in a local only. A change through destructuring (`const { inner } = CONFIG; inner.depth = 9`), a loop or a callback over a part, a list or an object made in the function holding the state, a shallow copy's shared parts, or what a function hands back of it (`current().retries = 7`, the way most code reaches a singleton) passed every gate, while the borrow rules answered the same questions for a parameter. Closed with that machinery, not a second one: module state (a module's let / var / const, or an import of another module's binding) is a root of no-param-mutation's reach() from a function (Borrowed::module), so bind_reach, element callbacks, for...of, copies and calls follow it as they follow a borrowed parameter, and the sites go through the same ParamSite decisions with the rule no-mutable-global. A function's hand-out summary (borrow::HandsOut via part_in, decided across files by handed_out) counts module state as a parameter (mutation::STATE), methods of top-level classes too (Summary::state_methods); a call of a project function, a namespace's function or a method of module state is asked whether it hands state back. Where the two could not be joined: a `let` given another value later and a container made in the function lent whole are not followed (both reported false findings on an adopter), nor a copy of a namespace's function (the ratchet lists it). Soundness corpus: module-state-reached (destructured, a getter, a list made of it), escaping before, rejected now. On an adopter, as is and migrated: 2 new findings, both real by the rule's definition (a benchmark's module-level table written through an element). Rewrite ratchet: the destructure miss of no-mutable-global comes off; a callee-const miss is listed. Work guard: +0.1% allocations, ceilings regenerated. Co-Authored-By: Claude Opus 5.5 <[email protected]>