failure-contract reads a Throws written through a type alias in every slot #160

Merged
sionzee merged 2 commits from evobug/failure-alias-reject into main 2026-11-24 14:07:35 +01:00
Owner

What was wrong

failure-contract read a Throws only when it was written in place. Written once as a type alias and named in a slot, it read as "declares no failure". An adopter's routes are a reduced example:

type Reply<T> = Promise<Throws<T, Gone>>;
interface Route { readonly run: (id: number) => Reply<number> }
export const routes: readonly Route[] = [{ run: async (id) => { if (id < 0) throw new Gone("g"); return id; } }];
// main: "`run` can propagate `Gone`, but it goes into `Route.run`, whose type declares no failure"

docs/rules/failure-contract.md already said types are read "through aliases, generic aliases and Readonly", so this was a bug. A function declared Reply<number> was silent too, but only because the alias wasn't read at all. Its body wasn't held to the alias, so async function f(): Reply<number> { throw new Other() } was a missed defect on main. An abstract method typed with the alias gave false findings on every implementation.

What changed

All failure-contract reads of a declared function type go through one place: the contract of the function type (analyze/contracts.rs contract_of, resolved in Run::contract).

  • Locally, a return type that names an alias (a module-level one, an imported one, or ns.X) records an AliasRef: the name plus its type arguments as Throws's E would read them. Each file records its module-level aliases that stand for a Throws, or for another alias, in throws_aliases.
  • In the whole-program pass, Run::contract looks the alias up (memoised per contract). It follows aliases of aliases across files, substitutes type arguments (Fails<number, Gone> with type Fails<T, E> = Throws<T, E>) and defaults (type Maybe<T, E = Busy>), and finds names passed on by export *. Each kind is resolved in the file that writes it.
  • Functions, abstract methods and fields whose return type names a possible alias are kept as own claims locally. Run::own_contract drops them in the whole-program pass when the alias declares nothing, so inference stays as before for type Id = number, Promise<User>, and the like.
  • named_decl (ns.X) now falls back to the target file's own declaration when its value exports don't list the name, as binding_at already did for named imports. Types aren't in exports. This also makes interfaces reached as ns.Store resolve.

So members, method signatures, callback parameters, fields, own return types and abstract methods all share the alias reading. satisfies isn't checked for written-out Throws either, and it stays that way.

Still reported

The new fixture tests/fixtures/contracts/aliased.ts (aliases in replies.ts) keeps every nearby defect:

  • a kind the alias doesn't declare, for each path: field, local alias, sync generic E, alias of alias, default parameter, ns.Reply, method signature, own return type (newly caught), abstract method;
  • an alias that isn't a Throws (type Plain<T> = Promise<T>) still "declares no failure";
  • a call through the alias propagates what it declares.

The help writes the expanded declaration, e.g. Promise<Throws<number, Gone | Other>>.

Not in this PR: reject(failure) with failure: Closed

The second item of the feedback (no-throw on reject(failure) with failure typed as a PropagateError subclass) is the documented design. no-throw.md, under "What the rule can't see", says a value from a variable or a parameter "is always reported", and throw failure behaves the same. Accepting the declared type would change what the rule promises: PropagateError is structural, so tsc accepts waitClosed(new Error("x")) and waitClosed(new Gone("x")) for failure: Closed. The options are reported to the maintainer separately.

Tests

  • cargo nextest run --workspace --no-fail-fast: all pass. work-ceilings.json moves by +6 memo questions and +1 name resolved (the alias lookups), plus +24 allocations.
  • npm run test:rewrites (full), misses.txt blessed:
    • failure-contract far namespace-import 2 and new namespace-import 1 are gone, fixed by the ns.X fallback;
    • failure-contract text namespace-import 1→3, re-export 3→5 and re-export-star 1→3 rise by the new fixture's sites where Throws itself is spelled ns.Throws or re-exported. That is the same pre-existing cause already listed for types.ts and stored.ts: std's Throws is recognised only when imported by name from std. An alias imported through export * resolves (that would have been 4 more);
    • new missing-mut hoist-const 226→229 and text aliased-mut call 14→16 were already so on main (2cd63b1e), not from this change.
  • npm run test:soundness: the escaping classes are unchanged.
  • npm run test:docs: passes, including the new ts good example.

Adopter tree (before export, safe-ts check . --idiomatic)

main 2cd63b1e and this branch give byte-identical output: 3557 findings, 81 failure-contract, 47 no-throw. That tree writes no Throws anywhere, so every Route.run (37 findings) and the other slots really declare no failure. 0 findings change, as expected. The alias fix matters once they declare Reply<T>, as in their later branch.

🤖 Generated with Claude Code

## What was wrong `failure-contract` read a `Throws` only when it was written in place. Written once as a type alias and named in a slot, it read as "declares no failure". An adopter's routes are a reduced example: ```ts type Reply<T> = Promise<Throws<T, Gone>>; interface Route { readonly run: (id: number) => Reply<number> } export const routes: readonly Route[] = [{ run: async (id) => { if (id < 0) throw new Gone("g"); return id; } }]; // main: "`run` can propagate `Gone`, but it goes into `Route.run`, whose type declares no failure" ``` `docs/rules/failure-contract.md` already said types are read "through aliases, generic aliases and `Readonly`", so this was a bug. A function *declared* `Reply<number>` was silent too, but only because the alias wasn't read at all. Its body wasn't held to the alias, so `async function f(): Reply<number> { throw new Other() }` was a **missed defect** on main. An abstract method typed with the alias gave false findings on every implementation. ## What changed All failure-contract reads of a declared function type go through one place: the contract of the function type (`analyze/contracts.rs` `contract_of`, resolved in `Run::contract`). - Locally, a return type that names an alias (a module-level one, an imported one, or `ns.X`) records an `AliasRef`: the name plus its type arguments as `Throws`'s `E` would read them. Each file records its module-level aliases that stand for a `Throws`, or for another alias, in `throws_aliases`. - In the whole-program pass, `Run::contract` looks the alias up (memoised per contract). It follows aliases of aliases across files, substitutes type arguments (`Fails<number, Gone>` with `type Fails<T, E> = Throws<T, E>`) and defaults (`type Maybe<T, E = Busy>`), and finds names passed on by `export *`. Each kind is resolved in the file that writes it. - Functions, abstract methods and fields whose return type names a possible alias are kept as `own` claims locally. `Run::own_contract` drops them in the whole-program pass when the alias declares nothing, so inference stays as before for `type Id = number`, `Promise<User>`, and the like. - `named_decl` (`ns.X`) now falls back to the target file's own declaration when its value exports don't list the name, as `binding_at` already did for named imports. Types aren't in `exports`. This also makes interfaces reached as `ns.Store` resolve. So members, method signatures, callback parameters, fields, own return types and abstract methods all share the alias reading. `satisfies` isn't checked for written-out `Throws` either, and it stays that way. ## Still reported The new fixture `tests/fixtures/contracts/aliased.ts` (aliases in `replies.ts`) keeps every nearby defect: - a kind the alias doesn't declare, for each path: field, local alias, sync generic `E`, alias of alias, default parameter, `ns.Reply`, method signature, own return type (newly caught), abstract method; - an alias that isn't a `Throws` (`type Plain<T> = Promise<T>`) still "declares no failure"; - a call through the alias propagates what it declares. The help writes the expanded declaration, e.g. `Promise<Throws<number, Gone | Other>>`. ## Not in this PR: `reject(failure)` with `failure: Closed` The second item of the feedback (`no-throw` on `reject(failure)` with `failure` typed as a `PropagateError` subclass) is the documented design. no-throw.md, under "What the rule can't see", says a value from a variable or a parameter "is always reported", and `throw failure` behaves the same. Accepting the declared type would change what the rule promises: `PropagateError` is structural, so `tsc` accepts `waitClosed(new Error("x"))` and `waitClosed(new Gone("x"))` for `failure: Closed`. The options are reported to the maintainer separately. ## Tests - `cargo nextest run --workspace --no-fail-fast`: all pass. `work-ceilings.json` moves by +6 memo questions and +1 name resolved (the alias lookups), plus +24 allocations. - `npm run test:rewrites` (full), `misses.txt` blessed: - failure-contract `far namespace-import 2` and `new namespace-import 1` are gone, fixed by the `ns.X` fallback; - failure-contract `text namespace-import 1→3`, `re-export 3→5` and `re-export-star 1→3` rise by the new fixture's sites where `Throws` itself is spelled `ns.Throws` or re-exported. That is the same pre-existing cause already listed for `types.ts` and `stored.ts`: std's `Throws` is recognised only when imported by name from std. An alias imported through `export *` resolves (that would have been 4 more); - `new missing-mut hoist-const 226→229` and `text aliased-mut call 14→16` were already so on main (2cd63b1e), not from this change. - `npm run test:soundness`: the escaping classes are unchanged. - `npm run test:docs`: passes, including the new `ts good` example. ## Adopter tree (before export, `safe-ts check . --idiomatic`) main 2cd63b1e and this branch give byte-identical output: 3557 findings, 81 `failure-contract`, 47 `no-throw`. That tree writes no `Throws` anywhere, so every `Route.run` (37 findings) and the other slots really declare no failure. 0 findings change, as expected. The alias fix matters once they declare `Reply<T>`, as in their later branch. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
failure-contract reads a Throws written through a type alias in every slot
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m33s
Tests / test (pull_request) Successful in 33m1s
8d43a78532
`type Reply<T> = Promise<Throws<T, Gone>>` named in a member's type read as
"declares no failure", and a function, abstract method or field declared with it
wasn't held to it (a kind it doesn't declare got out unreported). A return type
naming an alias now records it; the whole-program pass resolves it in
`Run::contract` (here or imported, `ns.X`, `export *`, alias of alias, type
arguments and defaults), so every slot shares one reading. `named_decl` falls
back to the target's own declaration for `ns.X`, as named imports already did.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge main into evobug/failure-alias-reject
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
2ccc605c56
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch evobug/failure-alias-reject 2026-11-24 14:07:35 +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!160
No description provided.