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

@@ -22,6 +22,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#define SX_TRACE_CAP 32
@@ -67,3 +68,28 @@ uint64_t sx_trace_frame_at(uint32_t i) {
uint32_t base = (sx_trace_count == SX_TRACE_CAP) ? sx_trace_head : 0u;
return sx_trace_frames[(base + i) % SX_TRACE_CAP];
}
// The failable-`main` entry-point reporter (ERR step E4.2). Called by the
// emitted main wrapper when an error reaches the function boundary: prints the
// unhandled-error header (with the tag name passed in — the compiler resolves
// it from the always-linked tag-name table) followed by the surviving trace
// frames, all to stderr. `name` is borrowed (a `string` slice, not NUL-
// terminated), so `name_len` bounds the print. The frame format mirrors
// trace.sx's `to_string`; both stay placeholder ("<location pending DWARF>")
// until DWARF line-info (E3.0) lands, after which both gain real file:line:col.
void sx_trace_report_unhandled(uint32_t tag, const char *name, size_t name_len) {
(void)tag;
dprintf(2, "error: unhandled error reached main: error.%.*s\n",
(int)name_len, name ? name : "");
uint32_t n = sx_trace_len();
if (n == 0u) return;
dprintf(2, "error return trace (most recent call last):\n");
if (sx_trace_truncated() != 0u) {
dprintf(2, " ... older frames omitted (buffer full)\n");
}
for (uint32_t i = 0u; i < n; i++) {
uint64_t frame = sx_trace_frame_at(i);
dprintf(2, " frame %u: <location pending DWARF> (raw %llu)\n",
i, (unsigned long long)frame);
}
}