exhaustive-switch / exhaustive-dispatch: name only the members that can reach the dispatch #154

Merged
sionzee merged 5 commits from narrowed-dispatch into main 2026-11-24 11:39:17 +01:00
Owner

Both rules listed members that TypeScript had already narrowed away. An adopter hit two forms:

  • a switch after if (tool === "bridge") return ...; asked for case "bridge", which is TS2678;
  • a ?: chain inside an eight-label case of a switch on a command's type was reported for the other 24 members.

What changed

  • New crates/safe_ts_core/src/narrowing.rs, built on flow.rs. In the file pass, each function holding a dispatch whose value is tested elsewhere in it gets its graph reduced to change events, marks and tests (the tests keep the values as written). The whole-program pass resolves those values (Job.MOVE → 1) and solves one fact per member plus one for "a member added later", with Meet::Every.
  • What it follows: earlier exits (return, throw, continue, break, std's unreachable / panic), ||, a const holding a test, enclosing if / && / ?: branches, a preceding switch whose cases return or whose default is unreachable, and the case of an enclosing switch with fall-through. Inside an enclosing case, "and so would a new member" is dropped, and a dispatch that handles every label isn't reported.
  • What makes a test stop counting: an assignment to the value or a prefix of its path (also by destructuring, ++, delete, or a re-declaration in a loop); a write of that member through another name; a loan (mut(...)); a method called on a prefix or on a receiver that isn't a place; await / yield; any call, for a value something else can reach (this, module-level, an outer binding) or one a closure here changes or hands on; a getter read (new TypeLookup::runs_accessor). Calls given the object without a loan don't count, per the loan rules (the rule only runs in checked files).
  • dispatch.rs filters "doesn't handle" by what can reach the site and says how many members can (some of the 3 values that can reach it (of the 5 of its type)).
  • Docs: docs/rules/exhaustive-switch.md and exhaustive-dispatch.md get "Members the code before it took". CHANGELOG has an entry under Unreleased → Fixed.

Tests

  • New fixture tests/fixtures/narrowed_dispatch/. Its 24 // lists: / // later annotations are checked by messages.rs. It covers string unions, Enum / is(), codesOf codes (typed and number), x.kind, and each invalidation.
  • A meaning.rs program applies the Add default: unreachable(…) fix on a narrowed switch; tsc passes before and after.
  • A tsc probe (not committed) adds back the members the old messages asked for: 9 give TS2678 / TS2367 / TS2339, and the tenth is already never in TS.
  • cargo nextest run --workspace: 413/413 pass. npm run test:docs passes. Clippy wasn't run: it isn't installed for the pinned nightly.
  • rewrites/misses.txt: miss exhaustive-dispatch ternary-to-if goes from 2 to 6. It's the same existing gap, hit at the new fixture's sites.

Measurements

  • Soundness corpus: 16 of 110 classes escape, before and after.
  • Adopter tree (check --json --idiomatic): 22 exhaustive-switch / 59 exhaustive-dispatch before and after. No site disappears, because each still has a real missing member. 11 messages shrink, with 89 members removed in total. Each removed member was checked by hand as unreachable at its site:
    • in an enclosing case: 671 (24 removed), 687 (27, also through a const alias), 700 (29);
    • earlier returns: errors 632, orders 911, movement 346 and 1213, terrain-works 291 (BRIDGE);
    • a preceding switch whose cases return: world-ops 401 and 522;
    • a preceding switch with default: unreachable: civic 487 (only the "new member" clause is dropped).
  • Wall time on the same profile, under load average ~146: main 3.48–3.99 s, branch 3.65–3.79 s, within noise. User time: 3.53–3.55 s on the branch vs 3.60–3.69 s on main.

Not done

  • unreachable-code (decided) still reads the declared type.
  • There is no case-adding quick fix. The rule's only fix is default: unreachable(...), and on a narrowed switch it compiles.

🤖 Generated with Claude Code

Both rules listed members that TypeScript had already narrowed away. An adopter hit two forms: - a `switch` after `if (tool === "bridge") return ...;` asked for `case "bridge"`, which is TS2678; - a `?:` chain inside an eight-label `case` of a `switch` on a command's type was reported for the other 24 members. ## What changed - New `crates/safe_ts_core/src/narrowing.rs`, built on `flow.rs`. In the file pass, each function holding a dispatch whose value is tested elsewhere in it gets its graph reduced to change events, marks and tests (the tests keep the values as written). The whole-program pass resolves those values (`Job.MOVE` → `1`) and solves one fact per member plus one for "a member added later", with `Meet::Every`. - What it follows: earlier exits (`return`, `throw`, `continue`, `break`, std's `unreachable` / `panic`), `||`, a `const` holding a test, enclosing `if` / `&&` / `?:` branches, a preceding `switch` whose cases return or whose `default` is `unreachable`, and the `case` of an enclosing `switch` with fall-through. Inside an enclosing `case`, "and so would a new member" is dropped, and a dispatch that handles every label isn't reported. - What makes a test stop counting: an assignment to the value or a prefix of its path (also by destructuring, `++`, `delete`, or a re-declaration in a loop); a write of that member through another name; a loan (`mut(...)`); a method called on a prefix or on a receiver that isn't a place; `await` / `yield`; any call, for a value something else can reach (`this`, module-level, an outer binding) or one a closure here changes or hands on; a getter read (new `TypeLookup::runs_accessor`). Calls given the object without a loan don't count, per the loan rules (the rule only runs in checked files). - `dispatch.rs` filters "doesn't handle" by what can reach the site and says how many members can (`some of the 3 values that can reach it (of the 5 of its type)`). - Docs: `docs/rules/exhaustive-switch.md` and `exhaustive-dispatch.md` get "Members the code before it took". CHANGELOG has an entry under Unreleased → Fixed. ## Tests - New fixture `tests/fixtures/narrowed_dispatch/`. Its 24 `// lists:` / `// later` annotations are checked by `messages.rs`. It covers string unions, `Enum` / `is()`, `codesOf` codes (typed and `number`), `x.kind`, and each invalidation. - A `meaning.rs` program applies the *Add default: unreachable(…)* fix on a narrowed switch; tsc passes before and after. - A tsc probe (not committed) adds back the members the old messages asked for: 9 give TS2678 / TS2367 / TS2339, and the tenth is already `never` in TS. - `cargo nextest run --workspace`: 413/413 pass. `npm run test:docs` passes. Clippy wasn't run: it isn't installed for the pinned nightly. - `rewrites/misses.txt`: `miss exhaustive-dispatch ternary-to-if` goes from 2 to 6. It's the same existing gap, hit at the new fixture's sites. ## Measurements - Soundness corpus: 16 of 110 classes escape, before and after. - Adopter tree (`check --json --idiomatic`): 22 exhaustive-switch / 59 exhaustive-dispatch before and after. No site disappears, because each still has a real missing member. 11 messages shrink, with 89 members removed in total. Each removed member was checked by hand as unreachable at its site: - in an enclosing `case`: 671 (24 removed), 687 (27, also through a `const` alias), 700 (29); - earlier returns: errors 632, orders 911, movement 346 and 1213, terrain-works 291 (BRIDGE); - a preceding `switch` whose cases return: world-ops 401 and 522; - a preceding `switch` with `default: unreachable`: civic 487 (only the "new member" clause is dropped). - Wall time on the same profile, under load average ~146: main 3.48–3.99 s, branch 3.65–3.79 s, within noise. User time: 3.53–3.55 s on the branch vs 3.60–3.69 s on main. ## Not done - `unreachable-code` (`decided`) still reads the declared type. - There is no case-adding quick fix. The rule's only fix is `default: unreachable(...)`, and on a narrowed switch it compiles. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
exhaustive-switch and exhaustive-dispatch name only the members that can reach the dispatch
Some checks failed
Benchmarks / bench (pull_request) Successful in 4m39s
Tests / test (pull_request) Failing after 19m33s
fafc7c0bda
Both rules listed members TypeScript had already narrowed away: a switch after
`if (tool === "bridge") return ...;` asked for a `case "bridge"` (TS2678), and a
`?:` chain inside an eight-label `case` was reported for the other 24 members.

New `narrowing.rs` on the shared control-flow layer: the file pass reduces the
flow graph of each function with a dispatch whose value is tested elsewhere
(changes, marks, tests as written); the whole-program pass solves it per member
(Meet::Every), with the compared values resolved. Followed: earlier exits
(return/throw/continue/break, std's unreachable/panic), `||`, const-held tests,
enclosing if/&&/?: branches, a preceding switch whose cases return, and the case
of an enclosing switch (no "a new member" there). A test stops counting on an
assignment to the value or a prefix, a write of that member through another
name, a loan, a method of the object, an await/yield, any call for a value
reachable from elsewhere or changed by a closure, or a getter read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Narrowing: a loan or method call in any spelling undoes the test
Some checks failed
Benchmarks / bench (pull_request) Successful in 5m7s
Tests / test (pull_request) Failing after 11m37s
77a2bdcfbc
std's mut imported under another name, a loan inside the arguments (touchAll({ h: mut(h) })),
std.mut, a const copy of mut, .call/.apply, and a method called through .call or a const copy
of its object now undo the narrowing of a path, as mut(h) and h.turn() written plainly do.
Found by the full rewrites ratchet: the renamed import and the nested loan kept the narrowing
on the tree as checked, so a member the code may have set since was left out of the list.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Narrowing: read std's mut from the import itself
Some checks failed
Benchmarks / bench (pull_request) Successful in 4m53s
Tests / test (pull_request) Has been cancelled
4b38126716
imports.imported keeps one local name per imported one, picked in no fixed order, so with
`import { mut, mut as lend }` the loan through `lend` was seen in some runs only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Narrowing: mut through a re-export, ns["mut"], and .call.call undo the test too
Some checks failed
Benchmarks / bench (pull_request) Successful in 4m40s
Tests / test (pull_request) Failing after 19m5s
43cfc05cbd
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge main
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
bb79644cd9
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch narrowed-dispatch 2026-11-24 11:39:17 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!154
No description provided.