28. Errors propagate with ? to a scope's handler; cleanup runs at scope end¶
Date: 2026-11-21
Status: Accepted (Dean, 2026-11-21), on the condition recorded under "Cleanup never shadows the error in flight"
Context¶
Every allocation and every system call can fail, and Dean wants each failure handled where it happens (ADR 0026). He likes ?-style propagation where a scope defines one handler for the closed set of errors that can occur in it. He also wants RAII for anything not freed explicitly, so the language needs cleanup handlers that run at scope end. The detail is in docs/stdlib.md.
Decision¶
result t ecarries a closed error sete, a variant type or a union of them, and error sets are part of function interfaces. Aresultmust be used.e?yields theOkpayload or jumps with the error to the nearest enclosinghandleblock. With no handler, the error is returned, and the function's declared error set must include it.- A
handleblock on a function body,let ... inscope or loop body must cover exactly the closed set of errors its scope's?s can produce. The compiler computes that set. defer eregisters cleanup for scope end. Types with aDropinstance are dropped at the end of their owning scope unless moved or freed. Both run in reverse order on every exit, including?.- Cleanup has no error set, cannot suspend, and takes no lock other than a top-level leaf lock. A resource whose cleanup can fail is
@linearand must be consumed explicitly. A trap runs no cleanup. - Cleanup never shadows the error in flight (Dean's condition on accepting this ADR). Two rules make it hold:
- It is a proof obligation, not only a type rule. Every
deferbody anddropmust have an empty error set, and the verifier raises acleanupgoal at each exit: the error that reaches the handler is the one the?raised, unchanged by the cleanup run on the way. - A release that can fail, such as closing a file opened for writing, is written
defer? e, withe : result unit c. Every cleanup still runs, and the scope's errors become the aggregatecleanup_error<e, c> = | Failed of e | Cleanup of list c | Both of { error : e; cleanup : list c }. Exiting normally with failed cleanups givesCleanup cs, in the order they ran. Exiting withegivesFailed e, orBothwhen cleanups also failed. The original error is never lost, and the scope's handler covers the aggregate like any other closed set. A@linearvalue whose consumption can fail may be live across a?only when adefer?consumes it. - With ADR 0027:
?does not cross a task orlockclosure (a task's error returns fromjoin; a lock body's error is its result, after release). Ascopeexiting early joins or cancels-then-joins its children before running its own cleanup. Cancellation is an explicit token that yieldsErr Cancelled. - A handler arm knows only what holds where its errors can be raised (Dean, 2026-11-22). Each
?that can raise an arm's error reaches that arm from its own point in the scope, so the arm is checked, and proved, once for each such?: it may use a binding, and rely on a callee's postcondition, only if that holds at every?that reaches it. Inlet a = f x? in let b = g a? in ..., an arm for an error onlygraises may useaand whatfensures about it, and an arm for an error onlyfraises may not mentionb. An arm reached by both may use neither. A catch-all arm is reached by every?whose errors no earlier arm takes. handleanddeferbecome reserved words, anddefer?isdeferfollowed by?.
Consequences¶
- Failure paths are visible and exhaustively handled, and cleanup is guaranteed on all of them, which is what lets every allocation be fallible without clutter.
- The verifier walks each exit's cleanup as code at that exit, so drops are proved like everything else.
- IRGen needs scope-exit lowering (a cleanup chain per scope), which is also what coroutine cancellation uses (ADR 0027).