Files
sx/examples/1126-diagnostics-global-aggregate-non-const-field-rejected.sx
agra d680b320f4 fix(ir): serialize null pointer fields in global aggregates (issue 0081)
A module-global aggregate initializer rejected a `null` literal in a
pointer (or optional-pointer) field as "must be initialized by a
compile-time constant". `Lowering.constExprValue` had no `.null_literal`
arm, so the null leaf returned no constant and the whole aggregate looked
non-constant — even though `null` is the compile-time zero pointer (a
top-level scalar `p : *s64 = null;` already serialized fine).

Add `.null_literal => .null_val` to constExprValue. While here, make the
two LLVM constant emitters exhaustive: emitConstAggregate and the
top-level init_val switch in emit_llvm.zig previously ended in a silent
`else => LLVMConstNull(...)` catch-all (the silent-arm class CLAUDE.md
mandates rooting out). They now handle every ConstantValue tag explicitly
(.null_val/.zeroinit -> all-zero constant, .undef -> LLVMGetUndef,
.func_ref resolved, nested .vtable is a hard @panic tripwire). The
reject-loud path for genuinely non-constant fields is preserved.

Regression: examples/0138 (array-of-struct null ptr fields, array of
all-null pointers, nested struct-in-struct null ptr) and the negative
examples/1126 (null ptr field beside a non-const field still errors).
Fail-before/pass-after verified.
2026-06-04 04:22:43 +03:00

22 lines
828 B
Plaintext

// A module-global aggregate with a NULL pointer field is fine (null is a
// compile-time constant), but a sibling field initialized from a NON-constant
// expression (here a runtime function call) must still be rejected loudly. The
// presence of an accepted `null` must NOT widen the gate to admit the
// non-constant neighbor.
// Regression (issue 0081): the null-pointer fix must not regress the
// reject-loud behavior for genuinely non-constant initializers (issues
// 0072/0080). Expected: "global 'boxes' must be initialized by a compile-time
// constant"; exit 1.
#import "modules/std.sx";
runtime_marker :: () -> s64 { return 7; }
Box :: struct { p: *s64; marker: s64; }
boxes : [1]Box = .[ .{ p = null, marker = runtime_marker() } ];
main :: () -> s32 {
print("marker={}\n", boxes[0].marker);
return 0;
}