Module state handed out on some path: a static method, one branch of a ?:, a local helper, one of two returns #144
Loading…
Reference in a new issue
No description provided.
Delete branch "module-state-paths"
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?
#143 taught
no-mutable-globalto follow module state through a function that hands it out, and listed three forms it still missed. This closes them, and a fourth found while reviewing. Each of these passedtscstrict,safe-ts check --idiomatic --deny-warningsand oxlint onmain:What changes
Registry.items.push(x)).return) is read where it is called.Evidence
mainreports 32 of them; the ten new ones are the forms above in several spellings, aswitchwith module state on one case, and atry. 12 fine forms are silent: reads, a function whose everyreturnis a new object, a copy made by a static method.deepen(mut(getHidden().inner)). Merging findings per call let the pass tomutitself take the call's slot. Fixed, and pinned in the fixture in three spellings.main, entries and directories.mainfor a whole check of that project's largest entry (18.70 to 18.61 billion, one thread, callgrind, mean of two runs each; runs of one binary differ by up to 0.6%). The memo of hand-outs takes back part of what #143 added; it does not take back all of it.What turns red on upgrade
Code that changes module state through one of the forms above. On the code measured, nothing.
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]>