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.
This commit is contained in:
agra
2026-06-04 04:22:43 +03:00
parent e93879816d
commit d680b320f4
11 changed files with 215 additions and 2 deletions

View File

@@ -0,0 +1,4 @@
boxes ptrs=true,true markers=11,22
ptr arr nulls=true,true,true
nested q nulls=true,true tags=1,2 labels=100,200
PASS

View File

@@ -0,0 +1,5 @@
error: global 'boxes' must be initialized by a compile-time constant
--> examples/1126-diagnostics-global-aggregate-non-const-field-rejected.sx:16:18
|
16 | boxes : [1]Box = .[ .{ p = null, marker = runtime_marker() } ];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^