fix(ir): halt cleanly when a global initializer can't be serialized

The global-init constant serializers in emit_llvm.zig printed a diagnostic
on an unserializable value and then RETURNED an undef/null placeholder and
CONTINUED emitting. For a comptime `#run` global that yields a function
reference (`fp :: #run pick();` where pick returns a function), the build
fell through to the JIT and segfaulted calling through the undef pointer
(exit 134) — a silent miscompile dressed up as a printed error.

Route every genuine bail in the serialization family through a new
`failGlobalInit` helper: it sets `comptime_failed` (so core.generateCode
aborts with a non-zero exit after emit()) and returns an undef placeholder
that never ships, because the halt fires before object emission / JIT. This
covers the comptime func_ref leaf, the require_resolved aggregate func_ref
leaf, the top-level + vtable func_ref globals, the comptime-init catch, and
the remaining heap-walk / aggregate-shape bails. Unresolved-function
diagnostics now name the function instead of its (stdlib-unstable) IR index.

The require_resolved=false Pass-0 placeholder is unchanged (func_map is
empty until Pass 1; the aggregate is re-emitted with require_resolved=true).

Regression: examples/1128-diagnostics-comptime-global-funcref-rejected.sx —
a `#run` global returning a function ref now exits 1 with the diagnostic
(was: exit 134 segfault). Fail-before/pass-after verified.
This commit is contained in:
agra
2026-06-04 05:25:19 +03:00
parent 263333bd26
commit d87bad2ec4
5 changed files with 78 additions and 19 deletions

View File

@@ -0,0 +1,26 @@
// A comptime `#run` global initializer that yields a function reference cannot
// be serialized to a static constant: at global-init time (Pass 0) functions
// are not yet declared, and the comptime serialization path has no later
// re-emit, so the func_ref can never resolve to a real function pointer. The
// compiler must reject this with a diagnostic AND a CLEAN non-zero exit — never
// print the error and then fall through into an undef initializer that crashes
// (pre-fix: the diagnostic printed, emission continued, and the JIT segfaulted
// calling through the undef pointer → exit 134).
// Regression (issue 0079 follow-up): every global-init serialization bail now
// routes through `failGlobalInit`, which sets the halt flag so the driver aborts
// after emit() instead of shipping the placeholder.
// Expected: "comptime init of 'fp' produced a reference to function 'add'…";
// exit 1, no segfault.
#import "modules/std.sx";
add :: (a: s32, b: s32) -> s32 { a + b }
pick :: () -> (s32, s32) -> s32 { return add; }
fp :: #run pick();
main :: () -> s32 {
print("{}\n", fp(3, 4));
return 0;
}

View File

@@ -0,0 +1 @@
error: comptime init of 'fp' produced a reference to function 'add', which cannot be serialized as a static constant (function declarations are not available at global-init time)