negative-member-guard, code-range-compare (@ordered), inline-member-set; exhaustive-dispatch checked against an adopter's fixes #99
Loading…
Reference in a new issue
No description provided.
Delete branch "closed-set-guards"
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?
Stacked on #91 (
closed-set-dispatch), and reuses its closed-set machinery (dispatch.rs,json::value::closed/codes).Problem
An adopter's agent audited ~50 old bug fixes in their game. Several came from closed sets (
Phase,Job,Op:codesOf(...)tables of numbers) where a member added later took the wrong branch:e1ed54f8: the farmercarry. A?:chain and anifchain onu.phasehad noTENDbranch, and this reached production.4c358533: a fake test server'sswitch (p.op)withdefault: return undefinedlacked three opcodes.968dcf38:u.phase === Phase.INSIDE && u.job !== Job.FLEEguarded work. AWORSHIPjob added later took that branch.aa532bbe: the "jobs a unit keeps" set was copied by hand four times, one copy being a range of codes (afterJob >= Job.MOVE && afterJob <= Job.IGNITE). A world became unloadable.Run against the parents of these fixes, #91's
exhaustive-dispatchcaught none of them.Solution
1.
exhaustive-dispatchchecked against the adopter's history. I ran it on read-only copies of each fix and its parent (git archive), using the realnode_modules. It missed every case, for three reasons. Each is now fixed:codesOfis declared asnames: T & UniqueCodeNames<T>. Type-parameter inference read only a parameter typed exactlyT, so nocodesOfset resolved andPhaseandJobwere unknown. NowTis also inferred through an intersection where it is the only bare type parameter, which is what TypeScript does (json.rs,callee_decl).CommandPacketisPacketOf<CommandOp>=O extends OpCode ? Simplify<{ op: O } & FieldsOf<Specs[O]['fields']>> : never. The evaluator can't buildFieldsOfover the packet table, sop.opwas unknown, and #91 fell back to everyOpconstant (server-to-client opcodes included).lazy_fieldnow reads one field through generic aliases with their arguments bound (closures across files), theSimplifyidentity map, and a distributive conditional whose otherwise-branch isnever(checked: every member of the binding matches). It no longer builds the other fields.ifbranchu.phase === Phase.CLEAN && …maderescheduleAbility's chain "no dispatch". A branch narrowed by&&now names its member when the comparison comes first and another branch is a plain comparison. When every branch is narrowed, the chain is a filter and stays unreported.e1ed54f8invariants.tscarryMax(?:) andsim.tsrescheduleAbility(ifchain). Both listPhase.TENDamong the missing membersTEND/DIG: the fix added those two branches and left 9–11 phases to: 0/ no branch. Silencing it would mean naming every phase (a table orunreachable), which the adopter didn't do4c358533exhaustive-switch, sincep.opis now typed): "doesn't handle Op.CmdPlaceBuilding, Op.CmdSetResearch and Op.CmdSetStudy", exactly the three the fix addeddefault: unreachable(...)", because the fix keptdefault: return undefined968dcf38setRuleschain mixes subjects (if (s),waitsToUnload(u)). This is item 2's case:negative-member-guardfiresnegative-member-guardstill fires: the fix's&& u.job !== Job.WORSHIPis a second exclusion of the same kindBefore these changes,
exhaustive-dispatch+exhaustive-switchgave 33 reports on the adopter'spackages/sim(~35k lines) ate1ed54f8^. Now they give 80 (63 + 17). The extra reports come from sets that now resolve and from&&branches. Of the 24 that&&branches added at first, 10 remain after the "comparison first, one plain branch" restriction. All have the shape #91 already reports: two or more named members, the rest left to nothing.2.
negative-member-guard. Reportsx !== E.A(or!=) on a closed set in aniftest whose branch does work, when the test also compares another value with a member (u.phase === Phase.INSIDE && u.job !== Job.FLEE), or excludes two members or more. These are not reported: an early exit (return;,return <literal>,return <constant>,continue,break,throw), and one exclusion alone. I measured three triggers on the adopter'spackages/simat968dcf38^:x !== memberguarding workMost of the 34 are "everything but X" read as meant (
if (u.phase !== Phase.IDLE) leavePhase(...),if (code !== SimError.UNREACHABLE) return code). The 9 include the real bug (sim.ts:15434) and a second copy of the same guard atsim.ts:8162(u.phase === Phase.INSIDE && u.due !== NONE && u.job !== Job.WORSHIP && u.job !== Job.FLEE). In this repo's docs examples, fixtures andexamples/, all three triggers give 0 reports (only the new fixture's own cases).3.
code-range-compare. Reports<,<=,>,>=with a constant of a numeric code table. A rangex >= A && x <= Bis reported once, and the message names the members it takes in today. A table is exempt when it is declared ordered with/** @ordered */in the doc comment of itsconst. This follows #95's@sorted/@unique: same placement, read with the existinginline::tagged, and resolved across files via a newTypeLookup::code_table. It is documented on the rule page and in the Enums guide. On the adopter's sim it gives 47 reports ataa532bbe^. 41 are on stage tables (LifeStage37,PanicStage3,FieldStage/GriefTie) that@orderedexempts. The rest areJob(the real save-reader range),Weather,SimError,PileStateandBuildingKind. The real one is gone at the fix.4.
inline-member-set. Groups each inline set by the value's last name (afterJobofu.afterJoband of a localafterJob) and by which closed set it is (the code table's declaration, or the union's members). A set is an||of ≥2 members of one value, a range of codes, or(…) && more. A place is reported when another function's set shares a member with it. The message names the other places and what differs: "with Job.MOVE…, without Job.MINE". A value tested in more than five functions is skipped: it is a dispatch field (u.job,u.phase) whose tests ask different questions. Measured on the adopter's sim ataa532bbe^:The 18 include the real pair:
invariants.ts:1496{DEPOSIT, MINE}vssave.ts:1685(range + names, withoutMINE). They also include a likely second drift that the fix'sFLEE_ONLY_AFTER_JOBSconfirms:codec.ts:198{MATE, HAUL, ATTACK, DOUSE}vsinvariants.ts:1019{MATE, HAUL, ATTACK}. Other hits are copies: two identical 7-memberBuildingKindlists inresearch.ts, andplaceStoragein one copy of the placement commands but not the other. At the fix: 15, and the real pair is gone.The rule needs the whole program, so
check_entriescomputes it once (Run::member_sets) and hands each file its share. Levels and ignore comments then apply as usual.check_file(editor, LSP, oxlint plugin) sees only one file and its imports, and the adopter's two files don't import each other. So it is a CLI-only rule:PROGRAM_RULESinlib.rs, and the editor/CLI agreement test skips it. The rule page and the CHANGELOG say this. It is listed in the plugin'sRULESandrecommended.json(the preset test requires every rule), but the plugin never reports it.5.
must-use-resultonmatch. std'smatchreturnstype Result<R, H> = unknown extends R ? MatchResult<H> : R, an alias of its own that the name-based check took for std'sResult. I chose not to rename the alias inenum.ts, which #91's sibling branchclosed-set-dispatch-matchis editing. Instead, the fix is self-contained in the checker: a file that declares its ownResulttype whose shape isn't std'sT | Err<E>doesn't make its functions return aResult. This also covers a library's ownResult. The fixturematch_statementshows a discardedmatchreportingmust-use-resultbefore and not after, while std'sResultstill does. I couldn't find the docs example that worked around it. I searcheddocs/,skills/, READMEs and the other open branches forvoid match,const _ = match,console.log(match(and their history. The closest isdocs/guide/bun.md'sconsole.log(match(job, …)), but it was written that way from its first commit. I left it as it is. If you know the example, it's a one-line revert.6.
census.mjs:RULE_SEEentries forexhaustive-dispatch,string-dispatchand the three new rules, all pointing topatterns-core.md#a-closed-set-of-states. I changed only those lines.Docs: new rule pages with checked ✅/❌ examples. The two-file
inline-member-setbad example is a plaintsblock, because the docs harness checks one file. Also updated:exhaustive-dispatch.md(&&branches, what is read now),must-use-result.md, the rule tables inREADME.mdanddocs/README.md, the sidebar,docs/guide/enums.md("Name the members, not the rest"), the skill'spatterns-core.md"A closed set of states", and the CHANGELOG.Tests
cargo nextest run --workspace: 280 passed. TheemitCLI test needs the native addon, so I rannpm run buildfirst.closed_set_guards(reduced from the four fixes: acodesOfwithnames: T & Unique<T>, aPacketOf/Simplifypacket type, the three new rules with their non-reported forms,@ordered, and a two-fileinline-member-set) andmatch_statement.npm run test:std: 367 passed.node scripts/docs-typecheck.mjs: 626 examples compile. Docs/skill examples:tests/docs.rspasses.cargo clippy -p safe_ts_core: no new warnings in the changed code.git archiveof each fix and its parent). The adopter repo wasn't touched.Left out
negative-member-guarddoesn't look at?:,while, exclusions inside||/!(...), or[A, B].includes(x).inline-member-setdoesn't count a singlex === Aas a set. The adopter's twoDEPOSIT-only copies aren't found that way; the two multi-member copies are.code-range-compareskips string tables and compares with number literals.@orderedon something other than a code table isn't reported. #95 reports a misplaced@sorted, but I kept this tag minimal.Created with Claude Opus 5.5 in Claude Code.
88e987fc388a2a1e49c5