On this page

0051. Exit codes, CliError and unit results

Context

An I/O main must propagate errors without boilerplate, and C and the interpreter must share exact error output, exit codes, fuel positions and destination passing.

Decision

Seed CliError as NotFound(str), Denied(str), IsDirectory(str), Io(str) and Failed(str). Main returns exactly result<unit, CliError>. Lowering appends synthetic __start, calls main(unit), returns on ok, and on err prints error: plus the variant payload and LF to stderr, then exits 1. It uses ordinary IR Call/Print/Switch/Exit, including ADR-0043's destination path. Synthetic functions consume no function fuel or call depth. Pure main has no wrapper.

All result<unit, E> functions implicitly return ok at fallthrough; ok() is the explicit success constructor. For other payloads ok() is E0808. There is no source unit literal. Ordinary ? propagation is unchanged.

Host supplies interpreter stdout/stderr, buffered stdin and raw arguments; run does not acquire process streams. Drivers flush stdout before returning Exit(u8). Stderr does not implicitly flush stdout; traps and process exit do. Stderr lines use a 4 KiB stack buffer for a single write when they fit.

M6.3 implements e.message(), args/raw_args (excluding argv[0]), env get/get_bytes and diverging cli.exit(i64). Exit accepts 0..255, flushes stdout, then exits; other values trap exit code out of range. UTF-8 args validate or trap; raw args do not. Environment text that is not UTF-8 gives none. run and interp forward raw arguments after --; compiler options never consume that suffix.

Args materialize fresh vectors on every field read, with normal arena placement; elements view the immutable startup argument bytes. Env values are static views (cap == 0), because v0 cannot change the environment. Empty keys and keys with NUL or = yield none; empty values yield some(""). Keys may be slices, so the C lookup compares environment names by length without allocating a terminated key.

Alternatives considered

Consequences

Stage A supplies the entry point and unit results; M6.3 supplies args/env/exit and message methods on both backends. Exit is an IR terminator with no retaining flows. Exit 101 is still a legal program exit, so tests must not infer traps from status alone. Global print survives only until M6.1 Stage C; Stage B leaves fn main() pure when no printing function is reachable. Lanes can use fallthrough immediately. Source copy/promotion reports omit synthetic startup's error dispatch. The nested-call limit is 253 (was 254) inside a platform method receiver, because cli.stdout.println( costs one more recursion level.