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

@@ -8486,30 +8486,46 @@ pub const Lowering = struct {
// infer the global's type from the comptime expression's return
// shape. `resolveType(null)` returns `.s64` for legacy reasons —
// good for primitive helpers, silently wrong for anything else.
const ret_ty: TypeId = if (type_ann) |n|
const expr_ty = self.inferExprType(expr);
// A failable `#run` (bare, no `catch`/`or`): the comptime function
// returns the full failable tuple so the #run site can inspect the
// error slot, but the GLOBAL is typed as the success value. On a
// comptime error the global never materializes — emit halts with a
// diagnostic + trace (E5.2). A handled `#run … catch/or …` already
// strips the error channel, so it lands here as non-failable.
const is_failable = self.errorChannelOf(expr_ty) != null;
const func_ret: TypeId = if (is_failable)
expr_ty
else if (type_ann) |n|
self.resolveTypeWithBindings(n)
else
self.inferExprType(expr);
const func_id = self.createComptimeFunction(name, expr, ret_ty);
expr_ty;
const global_ty: TypeId = if (is_failable) self.failableSuccessType(expr_ty) else func_ret;
const func_id = self.createComptimeFunction(name, expr, func_ret);
// Add a global constant whose initializer will be filled by the interpreter.
const name_id = self.module.types.internString(name);
const gid = self.module.addGlobal(.{
.name = name_id,
.ty = ret_ty,
.ty = global_ty,
.init_val = null, // will be filled by interpreter at emit time
.is_const = true,
.comptime_func = func_id,
});
// Register for runtime lookup: identifier resolution emits global_get
self.global_names.put(name, .{ .id = gid, .ty = ret_ty }) catch {};
self.global_names.put(name, .{ .id = gid, .ty = global_ty }) catch {};
}
/// Lower a standalone `#run expr;` at the top level (side-effect only).
/// Creates a comptime function that the interpreter should execute.
fn lowerComptimeSideEffect(self: *Lowering, expr: *const Node) void {
_ = self.createComptimeFunction("__run", expr, .void);
// A failable side-effect `#run f();` returns the failable tuple so the
// emit-time runner can detect an escaping error and halt (E5.2);
// non-failable side effects stay `void`.
const expr_ty = self.inferExprType(expr);
const ret: TypeId = if (self.errorChannelOf(expr_ty) != null) expr_ty else .void;
_ = self.createComptimeFunction("__run", expr, ret);
}
/// Lower a `#run expr` that appears inline within an expression.