ERR/E5.2: comptime #run of an escaping failable → diagnostic + halt

A bare failable `#run` (no catch/or) whose error escapes used to segfault (const
form `x :: #run f()`) or silently succeed (statement form `#run f();`). Now the
compiler reports the raised tag name + the resolved return trace at the #run site
and halts with a non-zero exit.

- lower.zig: a failable #run's comptime function returns the full failable tuple
  (so the error slot is inspectable) while the global is typed as the success
  value; failable side-effects return the tuple instead of void.
- emit_llvm.zig: read the always-on comptime trace buffer (extern sx_trace_*);
  comptimeErrChannel + checkComptimeFailable split the result (non-zero tag →
  reportComptimeEscape + comptime_failed flag; success → value part). Wired into
  emitGlobals (const) and runComptimeSideEffects (statement, now filtered by the
  __run name; buffer cleared before each eval).
- core.zig: generateCode returns error.ComptimeError when comptime_failed, so the
  driver aborts before JIT/link.

catch / or / onfail compose at comptime exactly as at runtime; a successful bare
#run yields the value. Regressions: examples/1037-errors-comptime-run-escape
(diagnostic, exit 1) + 1038-errors-comptime-run-handled (exit 164). Suite: 326.
This commit is contained in:
agra
2026-06-01 20:04:17 +03:00
parent 9e660f30c2
commit 549f97c731
11 changed files with 215 additions and 21 deletions

View File

@@ -0,0 +1,18 @@
// Comptime `#run` of a failable whose error ESCAPES (no `catch` / `or`): the
// compiler reports the raised tag name + the return trace at the `#run` site and
// halts with a non-zero exit (E5.2). Before this, a bare failable `#run`
// segfaulted (const form) or silently succeeded (statement form).
#import "modules/std.sx";
E :: error { Bad, Empty }
parse :: (n: s32) -> (s32, !E) {
if n < 0 { raise error.Bad; }
if n == 0 { raise error.Empty; }
return n * 2;
}
x :: #run parse(-1); // error.Bad escapes → comptime diagnostic + halt
main :: () -> s32 { return x; }

View File

@@ -0,0 +1,31 @@
// Comptime `#run` of a failable composes with the handlers exactly as at
// runtime: `catch` absorbs, `or` terminates, a successful bare `#run` yields the
// value (error channel stripped), and an `onfail` in the evaluated body still
// runs during comptime unwinding (E5.2).
#import "modules/std.sx";
E :: error { Bad, Empty }
parse :: (n: s32) -> (s32, !E) {
if n < 0 { raise error.Bad; }
if n == 0 { raise error.Empty; }
return n * 2;
}
guard :: (ok: bool) -> !E {
onfail print("comptime cleanup\n");
if !ok { raise error.Bad; }
return;
}
ok_v :: #run parse(5); // success → 10 (value, error stripped)
caught :: #run parse(-1) catch e 99; // Bad → 99
ored :: #run parse(0) or 55; // Empty → 55
#run guard(false) catch e { }; // onfail fires during the comptime unwind
main :: () -> s32 {
print("ok={} caught={} ored={}\n", ok_v, caught, ored);
return ok_v + caught + ored; // 10 + 99 + 55 = 164
}

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,4 @@
error: comptime `#run` (x) raised an unhandled error: error.Bad
error return trace (most recent call last):
parse at 1037-errors-comptime-run-escape.sx:11:17
help: handle it at the `#run` site — `#run <expr> catch e { ... }` or `#run <expr> or <default>`

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@
164

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,3 @@
comptime cleanup
--- build done ---
ok=10 caught=99 ored=55