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

@@ -0,0 +1,29 @@
// Failable `-> !` main entry-point wrapper (ERR step E4.2). A pure-failable
// main that lets an error reach the function boundary exits 1 and prints the
// unhandled-error header (with the tag name, via the always-linked tag-name
// table) plus the return trace to stderr — instead of the old behavior of
// returning the raw tag id as the exit code with no diagnostic. A successful
// run (no escaping error) exits 0.
//
// Note: the header + trace go to stderr. The test runner merges stderr+stdout,
// so the snapshot shows them interleaved with the `print` (stdout) lines.
// Frame locations are placeholders until DWARF (ERR E3.0); count + ordering +
// the tag name are already meaningful. Expected exit code: 1.
#import "modules/std.sx";
ParseErr :: error { Empty, BadDigit };
inner :: (n: s32) -> (s32, !ParseErr) {
if n == 0 { raise error.Empty; } // pushes a frame
if n < 0 { raise error.BadDigit; }
return n * 2;
}
main :: () -> !ParseErr {
v := try inner(5); // succeeds → v = 10
print("v = {}\n", v);
w := try inner(0); // raises Empty → propagates to main
print("w = {}\n", w); // never reached
return;
}