ERR/E4.2: failable-main wrapper (report + exit 1 on escaping error)

A pure-failable `main` (`-> !` / `-> !Named`) that lets an error reach the
function boundary now exits 1 and prints `error: unhandled error reached
main: error.<tag>` + the return trace to stderr, instead of returning the
raw tag id truncated as the exit code with no diagnostic. Success exits 0;
a `catch`-absorbed error exits 0 (buffer cleared).

Codegen wrapper so JIT and AOT behave identically (no host-side special-
casing):
- emit_llvm.zig: the `.ret` arm detects a failable main and routes to
  new `emitFailableMainRet` — `icmp ne tag, 0` → success block `ret i32 0`
  / error block GEPs the tag name out of the always-linked tag-name table,
  calls `sx_trace_report_unhandled`, `ret i32 1`. main's bare-u32 returns
  (success `ret(0)` + each raise's `ret(tag)`) all funnel through it.
- sx_trace.c: new `sx_trace_report_unhandled(tag, name, name_len)` prints
  the header + surviving frames to stderr (placeholder frame format mirrors
  trace.sx until DWARF/E3.0). Lives next to the buffer it reads.
- lower.zig validateMainSignature: the pure-failable arm sets
  needs_trace_runtime so the AOT path auto-links sx_trace.c even when the
  body emits no other push/clear.

Value-carrying `-> (T, !)` main stays gate-rejected (multi-slot wrapper is
a separate slice). examples/244-failable-main.sx.
This commit is contained in:
agra
2026-06-01 09:48:32 +03:00
parent bb20339691
commit 210cf91e37
6 changed files with 123 additions and 2 deletions

View File

@@ -389,7 +389,14 @@ pub const Lowering = struct {
// void / integer, and a pure failable `-> !` (a bare u32 error tag).
if (rt == .void or self.isIntEx(rt)) return;
if (self.errorChannelOf(rt)) |chan| {
if (rt == chan) return; // pure `-> !` / `-> !Named`
if (rt == chan) {
// pure `-> !` / `-> !Named`. The emitted entry-point wrapper
// (emit_llvm `emitFailableMainRet`) calls `sx_trace_report_unhandled`
// on an escaping error, so the AOT path must auto-link the trace
// runtime even when the body emits no other push/clear.
self.needs_trace_runtime = true;
return;
}
// `-> (T..., !)` — a multi-slot tuple return; not yet wired.
if (self.diagnostics) |diags| {
diags.addFmt(.err, if (fd.return_type) |rtn| rtn.span else null, "a value-carrying failable `main` (`-> (T, !)`) is not yet supported — its multi-slot return ABI-mismatches the entry-point call; use `-> !` (no value) or a non-failable integer return, or absorb errors with `catch`", .{});