Failure contracts: a function type says whether it can fail (Throws, Throwing, failure-contract) #115
Loading…
Reference in a new issue
No description provided.
Delete branch "failure-contracts"
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?
Based on
fail-closed-integration, which ismainplus the six open PRs (#109 to #114) merged together. Review the diff as it stands; once those six are in, I'll rebase this ontomainand retarget it.SAFE-TS finds out which
PropagateErrorkinds a function can throw by following calls. The previous PR taught it to follow a method through a field or a declared type to a class. What it still could not follow is a call whose target is only known at run time:The same held for an override reached through its base class, and for a callback kept in a field or a collection and called later.
The rule
A function type says whether it can fail, as a trait signature does in Rust. A slot that declares nothing cannot propagate; one that can says what:
failure-contract, an error): a class thatimplementsan interface, an object literal typed as one, a visible class handed over as one, an override against the method it overrides, an implementation of an abstract method, a function put into a typed member, field or collection. The message names the two ways out: catch it there, or declare it on the slot.Throwsreturn type is a claim, and its body is checked against it.Throws<R, E>andThrowing<Args, R, E>are in std's result module and the prelude. To TypeScriptThrows<R, E>isR: the checker reads it, as it doesMut<T>today. Whether a function value fails is not in its TypeScript type, so the conformance check is the checker's in any design; a nominal wrapper would have changed every call site and ruled out implementing an interface with ordinary methods.For
async,Promise<Throws<T, E>>declares what the promise rejects with.What an adopter will notice
On an adopter's project: 78 new
failure-contractfindings (69 from its entries), and nothing else changes. All 78 are implementations that throw aPropagateErrortheir slot does not declare; 61 are in one package whose interface doc comment already says "methods throw". 41 slots need a declaration.The implementer read 23 sites and found no false positives. It then declared 35 of the slots in a private copy: every caller the checker follows already catches the failure, so no unhandled failure surfaced. What the project gains is that the contracts are now written down and checked.
One behaviour change: a failing callback kept in a collection or field whose function type is written is now
failure-contract, where it wasuncaught-propagation, because the fix differs (declare it on the slot).The whole-program pass on that project's largest app goes from 864 ms to 923 ms.
How it was checked
test:std,typecheck, the docs examples (870 compile),test:vite, the docs site build and the soundness corpus pass.asyncimplementer rejecting outside its contract, a function whose own claim its body exceeds, a declared rejection nobody awaits.Result(a value, not a propagation), callers that catch,Throws<T, never>, callbacks that are called right away.Where the principle could not be carried through
Listed with examples on the rule page. The main ones:
Map.get, a computed key or aRecord.new Set<Handler>()), not in an annotation.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
sionzee referenced this pull request2026-11-21 10:22:27 +01:00
on...member, the same for a name and its arrow