failure-contract reads a Throws written through a type alias in every slot #160
Loading…
Reference in a new issue
No description provided.
Delete branch "evobug/failure-alias-reject"
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?
What was wrong
failure-contractread aThrowsonly 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:docs/rules/failure-contract.mdalready said types are read "through aliases, generic aliases andReadonly", so this was a bug. A function declaredReply<number>was silent too, but only because the alias wasn't read at all. Its body wasn't held to the alias, soasync 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.rscontract_of, resolved inRun::contract).ns.X) records anAliasRef: the name plus its type arguments asThrows'sEwould read them. Each file records its module-level aliases that stand for aThrows, or for another alias, inthrows_aliases.Run::contractlooks the alias up (memoised per contract). It follows aliases of aliases across files, substitutes type arguments (Fails<number, Gone>withtype Fails<T, E> = Throws<T, E>) and defaults (type Maybe<T, E = Busy>), and finds names passed on byexport *. Each kind is resolved in the file that writes it.ownclaims locally.Run::own_contractdrops them in the whole-program pass when the alias declares nothing, so inference stays as before fortype 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, asbinding_atalready did for named imports. Types aren't inexports. This also makes interfaces reached asns.Storeresolve.So members, method signatures, callback parameters, fields, own return types and abstract methods all share the alias reading.
satisfiesisn't checked for written-outThrowseither, and it stays that way.Still reported
The new fixture
tests/fixtures/contracts/aliased.ts(aliases inreplies.ts) keeps every nearby defect:E, alias of alias, default parameter,ns.Reply, method signature, own return type (newly caught), abstract method;Throws(type Plain<T> = Promise<T>) still "declares no failure";The help writes the expanded declaration, e.g.
Promise<Throws<number, Gone | Other>>.Not in this PR:
reject(failure)withfailure: ClosedThe second item of the feedback (
no-throwonreject(failure)withfailuretyped as aPropagateErrorsubclass) 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", andthrow failurebehaves the same. Accepting the declared type would change what the rule promises:PropagateErroris structural, sotscacceptswaitClosed(new Error("x"))andwaitClosed(new Gone("x"))forfailure: Closed. The options are reported to the maintainer separately.Tests
cargo nextest run --workspace --no-fail-fast: all pass.work-ceilings.jsonmoves by +6 memo questions and +1 name resolved (the alias lookups), plus +24 allocations.npm run test:rewrites(full),misses.txtblessed:far namespace-import 2andnew namespace-import 1are gone, fixed by thens.Xfallback;text namespace-import 1→3,re-export 3→5andre-export-star 1→3rise by the new fixture's sites whereThrowsitself is spelledns.Throwsor re-exported. That is the same pre-existing cause already listed fortypes.tsandstored.ts: std'sThrowsis recognised only when imported by name from std. An alias imported throughexport *resolves (that would have been 4 more);new missing-mut hoist-const 226→229andtext aliased-mut call 14→16were 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 newts goodexample.Adopter tree (before export,
safe-ts check . --idiomatic)main
2cd63b1eand this branch give byte-identical output: 3557 findings, 81failure-contract, 47no-throw. That tree writes noThrowsanywhere, so everyRoute.run(37 findings) and the other slots really declare no failure. 0 findings change, as expected. The alias fix matters once they declareReply<T>, as in their later branch.🤖 Generated with Claude Code