fix(ir): diagnose non-constant global initializers loudly (issue 0072)

globalInitValue's issue-0071 .identifier arm closed the bare-identifier hole,
but .field_access (and every other non-literal expression shape) still fell
through to `else => null`, so a global like `g : s32 = K.x;` was emitted with
no payload and silently zero-initialized (g=0).

Make the `else` emit a diagnostic — "global '<name>' must be initialized by a
compile-time constant" — instead of a null payload, so no unsupported shape can
silently zero. Two arms added alongside:

- `.null_literal => .null_val`: a `*void = null` global was previously a
  no-payload zero-init; this preserves the exact LLVMConstNull emission (fixes
  3 ffi examples that regressed on the first cut).
- explicit `.enum_literal => null` carve-out: the stdlib's
  `OS : OperatingSystem = .unknown;` zero-init is load-bearing for compile-time
  `inline if OS == .X`; documented, not folded into a silent fallthrough.

Field-access constant *evaluation* (materializing K.x -> 9) is intentionally
not implemented: a typed struct const like K is not registered in
module_const_map, so it would require new plumbing whose writes are read at
runtime — out of scope. The diagnostic is the issue-sanctioned outcome.

Regression: examples/1118-diagnostics-global-non-const-initializer-rejected.sx
(exit 1). Gate: zig build, zig build test, run_examples.sh -> 356/0.
This commit is contained in:
agra
2026-06-02 17:57:17 +03:00
parent ad7200c196
commit b72d49073e
6 changed files with 153 additions and 4 deletions

View File

@@ -1295,6 +1295,7 @@ pub const Lowering = struct {
const v = vd.value orelse return null;
return switch (v.data) {
.undef_literal => .zeroinit,
.null_literal => .null_val,
.int_literal => |il| .{ .int = il.value },
.bool_literal => |bl| .{ .boolean = bl.value },
.float_literal => |fl| .{ .float = fl.value },
@@ -1313,10 +1314,20 @@ pub const Lowering = struct {
d.addFmt(.err, v.span, "global '{s}' must be initialized by a compile-time constant; '{s}' is not a usable constant here", .{ vd.name, id.name });
break :blk null;
},
// Other initializer shapes (enum-literal shorthand, etc.) keep their
// established static-lowering behavior; this pass only closes the
// identifier/module-const hole (issue 0071).
else => null,
// Enum-literal shorthand globals (`OS : OperatingSystem = .unknown;`)
// keep their established zero-init: it is load-bearing for
// compile-time `inline if OS == .X` in the stdlib (issue 0071 scope
// note). Carved out explicitly — not folded into a silent fallthrough.
.enum_literal => null,
// Any other initializer shape (`.field_access` on a const, a call, an
// arithmetic expression, …) is not a static constant the compiler can
// evaluate here. Diagnose loudly rather than emit a null payload that
// silently zero-initializes the global (issues 0071/0072).
else => blk: {
if (self.diagnostics) |d|
d.addFmt(.err, v.span, "global '{s}' must be initialized by a compile-time constant", .{vd.name});
break :blk null;
},
};
}