exhaustive-switch / exhaustive-dispatch: a member of a union takes its set from the union's members; Mut<Class> members; panic spellings #166
Loading…
Reference in a new issue
No description provided.
Delete branch "dispatch-union-members"
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?
From an adopter's feedback on 0.5.1: a dispatch on
p.opof a packet union asked for every constant of the code table.1. The set of a member of a union is the union's members
main:
exhaustive-dispatch: "doesn't handle Op.Hello, Op.Welcome and Op.Pong".Op.WelcomeandOp.Pongcan't come (acasefor one is TS2678). The rule already takes the set from the value's type when it can read the type; here it couldn't, so it fell back to the table every tested constant is read from. The type evaluator couldn't read:{ [O in OpCode]: ... }with numeric codes). It took only string literals as keys. Under the newnumber_keyssetting (the value evaluators set it; JSON validators still refuse number keys), an integer key names the field by its text, and the parameter is bound to the number;as constarray (fields: [...SEQ_TICK, ["good", "u8"]]), now a tuple rest;64 * 1024), nownumber.(These three came from an earlier attempt at this piece. I reviewed them and kept them, with one change: string-keyed mapped types bind the key exactly as before.)
Now:
exhaustive-switch: "switch (p.op)doesn't handle Op.Hello". A typed switch isexhaustive-switch's, which can ask fordefault: unreachable(p.op). Anif/?:chain says "tests some of the constants ofOpits type allows" when the type allows fewer constants than the table has.2. A member of a class instance declared
Mut<C>switch (w.mode)withw: Mut<Walker>(a class) wasn't reported; withw: Walker,Readonly<Walker>orMut<Holder>(an object alias) it was.program_membertook a declared type as a class instance only when it was a bare class name. It now looks throughMut/Readonly(alsoMut<Readonly<C>>). This is the evaluator every type-reading rule shares, so the same gap closes forstrict-truthiness(if (w.count)withw: Mut<Counter>wasn't reported on main), and forunreachable-codeand the rest ofjson::value's users. The rules with their own type walk (declared_keys.rs,loans.rs,spread_args.rs, ...) already unwrapMut.3.
panic/unreachablein other spellings never returndispatch.rsis_std_nevernow reads the import declaration (asis_std_lenddoes), notimports.imported(one local per imported name, so a renamed import was missed on the normalised tree). The narrowing (never_returns) follows.call/.applyandconstcopies. Also a member of a std namespace import (result.panic(...), also through aconstcopy) andpanic["call"](...). Apanicimported from another module still isn't taken as never returning: it may be another function that returns, and taking it for std's would narrow the members asked for. That stays on the safe side: more members are listed.Still reported (fixtures)
tests/fixtures/union_dispatch(checked bymessages.rsfor the exact lists):receiveSome:Msg.Book), and so is a member whose packets come in the other direction when the union allows it (AnyMessage,MsgCode: every constant);numberstill gets the table's constants (exhaustive-dispatch);answerAfterLeave);Mut<C>from another file and from this one,Readonly<C>,Mut<Readonly<C>>,Mut<alias>,ifchains, andstrict-truthinessthroughMut<C>.tests/fixtures/narrowed_dispatch/never.ts: renamed,.call,.apply,constcopy narrow;panicfrom./log(which returns) doesn't.Adopter tree (before export with the workspace packages linked)
exhaustive-*The five switches on packet codes (
market.ts:199,world-host.ts:1246,online.ts:982, a test fake server, a bot script) dropped 19 or 45Opconstants each. I checked every dropped constant against the table'sdir: each is a packet of the other direction.online.ts:982handles every server packet and ends indefault: break, so it is now "withoutdefault: unreachable(...)". Other changes come from types the evaluator can now read: 11 newexhaustive-dispatchand 8exhaustive-switchlines, including the five above (new dispatch reports on an event enum and string unions) (spot-checked:grief.ts:125really sends"sibling"to thedefault), 39strict-truthiness(numbers tested for truth), 2inline-member-set, and 4undeclared-return-typegone (their results can now be read).Not done:
world-host.ts:1246still lists theCmd*codes, whichif (isCommandPacket(p)) { ...; continue; }narrowed away (p is CommandPacket). Following a type predicate is new analysis innarrowing.rs, and type predicates are whatno-type-predicatebans. main lists them too (and the server packets as well).Tests
cargo nextest run --workspace(fast profile): 416 of 416 pass.npm run test:rewrites(full mode): passes withmisses.txtupdated:text exhaustive-dispatchlines forpanicspelled.apply,.call, as aconstcopy, and as a renamed import (item 3).ternary-to-if6→14,hoist-const(new exhaustive-switch) 10→13,decl-to-arrow(far, both rules) 2→3.text exhaustive-dispatch re-export/re-export-star1→3:panicimported through a project module that re-exports it isn't taken for std's in the file pass (on purpose, see above). The normalised tree imports it from std.text exhaustive-dispatch namespace-import 1: as parsed, a table keyed by[ns.Msg.Join]isn't read by the evaluator, so the set falls back to the whole table (more listed). The normalised tree reads it.used hatch:panic/hatch:trustapply/call: the rewriter wraps the fixture'spanic.call(...)/result.panic(...)again (panic.call.call(panic, ...)). As parsed, thepanicor namespace given as an argument counts as a hatch handed on (fail closed).npm run test:soundness: passes (16 of 110 classes escape, the ones inescaping.txt).npm run test:docs: passes. The newexhaustive-switchexample is reported on main asexhaustive-dispatchlistingOp.WelcomeandOp.Pong; on this branch it listsOp.Helloonly.🤖 Generated with Claude Code
4cf9bff30b52ca8d0fd6